Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.shimmerresearch.javacsharp.throughput;

import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;

public class ProcessUnitTest {

@Before
public void Setup() {

}

//steps
//1.set IsSendDataFromJavaToCSharp variable to true and period to 5 in c sharp
//2.run the test
@Test
public void ReadAndWriteDataUsingProcessTest() {
final ThroughputTestUsingProcess test = new ThroughputTestUsingProcess();

test.InitializeProcess();

test.MeasureLatency();

Thread ReadDataUsingProcess = new Thread(){
public void run(){
test.ReadDataUsingProcess();
}
};

ReadDataUsingProcess.start();

Thread WriteDataUsingProcess = new Thread(){
public void run(){
test.WriteDataUsingProcess();
}
};

WriteDataUsingProcess.start();

try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

assertTrue (test.maximumThroughput > 0);
assertTrue (!test.writeToCSharpString.equals(null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.shimmerresearch.javacsharp.throughput;

import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;

public class SocketUnitTest {

@Before
public void Setup() {

}

//steps
//1.set IsSendDataFromJavaToCSharp variable to true and period to 5 in c sharp
//2.run the test
//3.run the c# application
@Test
public void ReadAndWriteDataUsingSocketTest() {
final ThroughputTestUsingSocket test = new ThroughputTestUsingSocket();

//initialize socket
test.InitializeSocket();

test.MeasureLatency();

Thread ReadDataUsingSocket = new Thread(){
public void run(){
test.ReadDataUsingSocket();
}
};

ReadDataUsingSocket.start();

Thread WriteDataUsingSocket = new Thread(){
public void run(){
test.WriteDataUsingSocket();
}
};

WriteDataUsingSocket.start();

try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

assertTrue (test.maximumThroughput != 0);
assertTrue (!test.writeToCSharpString.equals(null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.shimmerresearch.javacsharp.throughput;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Calendar;

//steps
//1.change the executablePath to the path to the exe file of the c# application
//2.make sure the period and IsSendDataFromJavaToCSharp are the same in both c# and java application
//3.run the ThroughputTestUsingProcess.java

public class ThroughputTestUsingProcess {

static Process p;
static String executablePath = "C:\\Users\\weiwe\\source\\repos\\ShimmerCSharpBLEAPI\\ThroughputTestUsingProcess\\bin\\Debug\\netcoreapp3.1\\ThroughputTestUsingProcess.exe";
BufferedWriter writer;
static int bytePerLine = 20;
static boolean IsSendDataFromJavaToCSharp = true;
static int period = 5;
static Timer writeDataTimer;
static String unixTimeStampWhenSend;
static long unixTimeStampWhenReceive;
int maximumThroughput = 0;
String writeToCSharpString = null;

public ThroughputTestUsingProcess() {

}

public void ReadDataUsingProcess() {
try {
int count = 0;
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;

while ((line = reader.readLine()) != null) {
count ++;
System.out.println(line);
if(line.length() == 1) {
writeToCSharpString = line;
}
}
System.out.println("Total kilobytes received in " + period + " seconds = " + count * bytePerLine / 1000);
maximumThroughput = (((count * bytePerLine) / 1000) / period);
System.out.println("Maximum throughput = " + maximumThroughput + "kb per second");
Long delay = unixTimeStampWhenReceive - Long.parseLong(unixTimeStampWhenSend);
System.out.println("Latency = " + delay + " milliseconds");
}
catch (IOException e) {
e.printStackTrace();
}
}

public void MeasureLatency() {
//long unixTime = Instant.now().getEpochSecond();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
unixTimeStampWhenSend = line;
unixTimeStampWhenReceive = Calendar.getInstance().getTime().getTime();
break;
}
}
catch (IOException e) {
e.printStackTrace();
}
}

public void WriteDataUsingProcess() {
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writeDataTimer = new Timer();
writeDataTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
writer.write("a", 0, 1);
writer.newLine();
writer.flush();
} catch (IOException e)
{
writeDataTimer.cancel();
writeDataTimer.purge();
System.out.println("Disconnected");
}
}
}, 0, 1000);
}

public void InitializeProcess() {
Runtime runTime = Runtime.getRuntime();

try {
p = runTime.exec(executablePath);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {

final ThroughputTestUsingProcess test = new ThroughputTestUsingProcess();

//run the c sharp exe file
test.InitializeProcess();

test.MeasureLatency();

Thread ReadDataUsingProcess = new Thread(){
public void run(){
test.ReadDataUsingProcess();
}
};

ReadDataUsingProcess.start();

if(IsSendDataFromJavaToCSharp) {
Thread WriteDataUsingProcess = new Thread(){
public void run(){
test.WriteDataUsingProcess();
}
};

WriteDataUsingProcess.start();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.shimmerresearch.javacsharp.throughput;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

//steps
//1.make sure the period and IsSendDataFromJavaToCSharp are the same in both c# and java application
//2.run the ThroughputTestUsingProcess.java
//3.run the c# application

public class ThroughputTestUsingSocket {

static ServerSocket serversocket;
static Socket socket;
static boolean IsSendDataFromJavaToCSharp = true;
static int period = 5;
static String unixTimeStampWhenSend;
static long unixTimeStampWhenReceive;
int maximumThroughput = 0;
String writeToCSharpString = null;

public ThroughputTestUsingSocket() {

}

public void ReadDataUsingSocket() {
int count = 0;
String line = null;
int bytePerLine = 20;

try {
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
System.out.println("Using socket");

while ((line = reader.readLine()) != null) {
System.out.println(line);
count++;

if(line.length() == 1) {
writeToCSharpString = line;
}
}
System.out.println("Total kilobytes received in " + period + " seconds = " + count * bytePerLine / 1000);
maximumThroughput = (((count * bytePerLine) / 1000) / period);
System.out.println("Maximum throughput = " + maximumThroughput + "kb per second");
Long delay = unixTimeStampWhenReceive - Long.parseLong(unixTimeStampWhenSend);
System.out.println("Latency = " + delay + " milliseconds");
}
catch (IOException e)
{
e.printStackTrace();
}
}

public void WriteDataUsingSocket() {
try {
final byte[] bytes = new byte[1];
new Random().nextBytes(bytes);
final OutputStream output = socket.getOutputStream();

final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
output.write(bytes);
} catch (IOException e)
{
//e.printStackTrace();
System.out.println("Client Disconnected");
timer.cancel();
}
}
}, 0, 1000);
}
catch (IOException e)
{
e.printStackTrace();
}
}

public void MeasureLatency() {
//long unixTime = Instant.now().getEpochSecond();
try {
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = null;
while ((line = reader.readLine()) != null) {
unixTimeStampWhenSend = line;
unixTimeStampWhenReceive = Calendar.getInstance().getTime().getTime();
break;
}
}
catch (IOException e) {
e.printStackTrace();
}
}

public void InitializeSocket() {
try {
serversocket = new ServerSocket(1133, 10);
socket = serversocket.accept();

} catch (IOException e1) {
e1.printStackTrace();
}
}

public static void main(String[] args) {

System.out.println("Waiting for C# application to connect.");

final ThroughputTestUsingSocket test = new ThroughputTestUsingSocket();

//initialize socket
test.InitializeSocket();

test.MeasureLatency();

Thread ReadDataUsingSocket = new Thread(){
public void run(){
test.ReadDataUsingSocket();
}
};

ReadDataUsingSocket.start();

if(IsSendDataFromJavaToCSharp) {

Thread WriteDataUsingSocket = new Thread(){
public void run(){
test.WriteDataUsingSocket();
}
};

WriteDataUsingSocket.start();
}

}
}