diff --git a/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ProcessUnitTest.java b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ProcessUnitTest.java new file mode 100644 index 000000000..c483f0384 --- /dev/null +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ProcessUnitTest.java @@ -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)); + } +} diff --git a/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/SocketUnitTest.java b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/SocketUnitTest.java new file mode 100644 index 000000000..06725a078 --- /dev/null +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/SocketUnitTest.java @@ -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)); + } +} diff --git a/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingProcess.java b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingProcess.java new file mode 100644 index 000000000..284fd7993 --- /dev/null +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingProcess.java @@ -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(); + } + + } +} diff --git a/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingSocket.java b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingSocket.java new file mode 100644 index 000000000..b1429627e --- /dev/null +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingSocket.java @@ -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(); + } + + } +}