From 6af10b43e8dc31953484a345750298844fcb3158 Mon Sep 17 00:00:00 2001 From: Mas Azalya Date: Fri, 6 Aug 2021 16:45:42 +0800 Subject: [PATCH 1/2] java csharp throughput test --- .../throughput/ProcessUnitTest.java | 26 ++++ .../javacsharp/throughput/SocketUnitTest.java | 57 +++++++++ .../ThroughputTestUsingProcess.java | 96 ++++++++++++++ .../throughput/ThroughputTestUsingSocket.java | 120 ++++++++++++++++++ 4 files changed, 299 insertions(+) create mode 100644 ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ProcessUnitTest.java create mode 100644 ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/SocketUnitTest.java create mode 100644 ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingProcess.java create mode 100644 ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingSocket.java 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..584b3d6bd --- /dev/null +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ProcessUnitTest.java @@ -0,0 +1,26 @@ +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() { + + } + + @Test + public void ReadDataUsingProcess() { + ThroughputTestUsingProcess a = new ThroughputTestUsingProcess(); + a.InitializeProcess(); + int count = a.ReadDataUsingProcess(); + assertTrue (count != 0); + } + + @Test + public void WriteDataUsingProcess() { + //TODO + } +} 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..49e167763 --- /dev/null +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/SocketUnitTest.java @@ -0,0 +1,57 @@ +package com.shimmerresearch.javacsharp.throughput; + +import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.Test; + +class SocketUnitTest { + + @Before + public void Setup() { + + } + + //need to run c# application manually + @Test + public void WriteDataUsingSocket() { + final ThroughputTestUsingSocket test = new ThroughputTestUsingSocket(); + + test.InitializeSocket(); + + Thread ReadDataUsingSocket = new Thread(){ + public void run(){ + test.ReadDataUsingSocket(); + } + }; + + Thread WriteDataUsingSocket = new Thread(){ + public void run(){ + test.WriteDataUsingSocket(); + } + }; + + ReadDataUsingSocket.start(); + WriteDataUsingSocket.start(); + + try { + ReadDataUsingSocket.join(); + WriteDataUsingSocket.join(); + + assertTrue (test.writeResult); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + //need to run c# application manually + @Test + public void ReadDataUsingSocket() { + ThroughputTestUsingSocket test = new ThroughputTestUsingSocket(); + + test.InitializeSocket(); + + int count = test.ReadDataUsingSocket(); + + assertTrue (count != 0); + } +} 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..ad9b8b3be --- /dev/null +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingProcess.java @@ -0,0 +1,96 @@ +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; + +public class ThroughputTestUsingProcess { + + static Process p; + static String executablePath; + BufferedWriter writer; + static boolean IsSendDataFromJavaToCSharp = false; + + public ThroughputTestUsingProcess() { + + } + + public int ReadDataUsingProcess() { + int count = 0; + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); + int bytePerLine = 20; + int totalSeconds = 60; + String line = null; + + while ((line = reader.readLine()) != null) { + count ++; + System.out.println(line); + } + System.out.println("Total kilobytes received in " + totalSeconds + " seconds = " + count * bytePerLine / 1000); + System.out.println("Maximum throughput = " + (((count * bytePerLine) / 1000) / totalSeconds) + "kb per second"); + } + catch (IOException e) { + e.printStackTrace(); + } + return count; + } + + public void WriteDataUsingProcess() { + final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); + Timer writeDataTimer = new Timer(); + writeDataTimer.schedule(new TimerTask() { + @Override + public void run() { + try { + writer.write("testing", 0, 7); + writer.flush(); + } catch (IOException e) + { + e.printStackTrace(); + } + } + }, 0, 1000); + } + + public void InitializeProcess() { + Runtime runTime = Runtime.getRuntime(); + executablePath = "C:\\Users\\Mas Azalya\\source\\repos\\ShimmerCSharpBLEAPI\\ThroughputTestUsingProcess\\bin\\Debug\\netcoreapp3.1\\ThroughputTestUsingProcess.exe"; + + try { + p = runTime.exec(executablePath); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + + final ThroughputTestUsingProcess test = new ThroughputTestUsingProcess(); + + test.InitializeProcess(); + + 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..0f4cca6e3 --- /dev/null +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingSocket.java @@ -0,0 +1,120 @@ +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.Random; +import java.util.Timer; +import java.util.TimerTask; + +public class ThroughputTestUsingSocket { + + static ServerSocket serversocket; + static Socket socket; + public boolean writeResult; + static boolean IsSendDataFromJavaToCSharp = true; + public ThroughputTestUsingSocket() { + writeResult = false; + } + + public int ReadDataUsingSocket() { + + String line = null; + int count = 0; + int bytePerLine = 20; + int totalSeconds = 60; + + 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) { + writeResult = true; + } + } + System.out.println(writeResult); + System.out.println("Total kilobytes received in " + totalSeconds + " seconds = " + count * bytePerLine / 1000); + System.out.println("Maximum throughput = " + (((count * bytePerLine) / 1000) / totalSeconds) + "kb per second"); + + } + catch (IOException e) + { + e.printStackTrace(); + } + + return count; + } + + public void WriteDataUsingSocket() { + try { + final byte[] bytes = new byte[1]; + new Random().nextBytes(bytes); + final OutputStream output = socket.getOutputStream(); + + final Timer timer1 = new Timer(); + timer1.schedule(new TimerTask() { + @Override + public void run() { + try { + output.write(bytes); + } catch (IOException e) + { + //e.printStackTrace(); + System.out.println("Client Disconnected"); + timer1.cancel(); + } + } + }, 0, 1000); + } + 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) { + + final ThroughputTestUsingSocket test = new ThroughputTestUsingSocket(); + + //initialize socket + test.InitializeSocket(); + + Thread ReadDataUsingSocket = new Thread(){ + public void run(){ + test.ReadDataUsingSocket(); + } + }; + + ReadDataUsingSocket.start(); + + if(IsSendDataFromJavaToCSharp) { + + Thread WriteDataUsingSocket = new Thread(){ + public void run(){ + test.WriteDataUsingSocket(); + } + }; + + WriteDataUsingSocket.start(); + } + + } +} From fd4adc458a9cdb8e3bf707ff607dbed5831ae150 Mon Sep 17 00:00:00 2001 From: weiwentan23 Date: Wed, 11 Aug 2021 14:54:06 +0800 Subject: [PATCH 2/2] Minor updates, add latency test, add steps on running the programs and modify test --- .../throughput/ProcessUnitTest.java | 45 ++++++++++--- .../javacsharp/throughput/SocketUnitTest.java | 42 ++++++------ .../ThroughputTestUsingProcess.java | 65 +++++++++++++++---- .../throughput/ThroughputTestUsingSocket.java | 62 +++++++++++++----- 4 files changed, 151 insertions(+), 63 deletions(-) diff --git a/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ProcessUnitTest.java b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ProcessUnitTest.java index 584b3d6bd..c483f0384 100644 --- a/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ProcessUnitTest.java +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ProcessUnitTest.java @@ -11,16 +11,41 @@ public void Setup() { } + //steps + //1.set IsSendDataFromJavaToCSharp variable to true and period to 5 in c sharp + //2.run the test @Test - public void ReadDataUsingProcess() { - ThroughputTestUsingProcess a = new ThroughputTestUsingProcess(); - a.InitializeProcess(); - int count = a.ReadDataUsingProcess(); - assertTrue (count != 0); - } - - @Test - public void WriteDataUsingProcess() { - //TODO + 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 index 49e167763..06725a078 100644 --- a/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/SocketUnitTest.java +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/SocketUnitTest.java @@ -4,19 +4,25 @@ import org.junit.Before; import org.junit.Test; -class SocketUnitTest { +public class SocketUnitTest { @Before public void Setup() { } - //need to run c# application manually + //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 WriteDataUsingSocket() { + public void ReadAndWriteDataUsingSocketTest() { final ThroughputTestUsingSocket test = new ThroughputTestUsingSocket(); - test.InitializeSocket(); + //initialize socket + test.InitializeSocket(); + + test.MeasureLatency(); Thread ReadDataUsingSocket = new Thread(){ public void run(){ @@ -24,34 +30,24 @@ public void run(){ } }; - Thread WriteDataUsingSocket = new Thread(){ + ReadDataUsingSocket.start(); + + Thread WriteDataUsingSocket = new Thread(){ public void run(){ test.WriteDataUsingSocket(); } }; - - ReadDataUsingSocket.start(); - WriteDataUsingSocket.start(); + WriteDataUsingSocket.start(); + try { - ReadDataUsingSocket.join(); - WriteDataUsingSocket.join(); - - assertTrue (test.writeResult); + Thread.sleep(6000); } catch (InterruptedException e) { + // TODO Auto-generated catch block e.printStackTrace(); } - } - - //need to run c# application manually - @Test - public void ReadDataUsingSocket() { - ThroughputTestUsingSocket test = new ThroughputTestUsingSocket(); - - test.InitializeSocket(); - - int count = test.ReadDataUsingSocket(); - assertTrue (count != 0); + 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 index ad9b8b3be..284fd7993 100644 --- a/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingProcess.java +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingProcess.java @@ -7,51 +7,86 @@ 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; + static String executablePath = "C:\\Users\\weiwe\\source\\repos\\ShimmerCSharpBLEAPI\\ThroughputTestUsingProcess\\bin\\Debug\\netcoreapp3.1\\ThroughputTestUsingProcess.exe"; BufferedWriter writer; - static boolean IsSendDataFromJavaToCSharp = false; + 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 int ReadDataUsingProcess() { - int count = 0; + public void ReadDataUsingProcess() { try { + int count = 0; BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); - int bytePerLine = 20; - int totalSeconds = 60; 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; } - System.out.println("Total kilobytes received in " + totalSeconds + " seconds = " + count * bytePerLine / 1000); - System.out.println("Maximum throughput = " + (((count * bytePerLine) / 1000) / totalSeconds) + "kb per second"); } catch (IOException e) { e.printStackTrace(); } - return count; } public void WriteDataUsingProcess() { final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); - Timer writeDataTimer = new Timer(); + writeDataTimer = new Timer(); writeDataTimer.schedule(new TimerTask() { @Override public void run() { try { - writer.write("testing", 0, 7); + writer.write("a", 0, 1); + writer.newLine(); writer.flush(); } catch (IOException e) { - e.printStackTrace(); + writeDataTimer.cancel(); + writeDataTimer.purge(); + System.out.println("Disconnected"); } } }, 0, 1000); @@ -59,8 +94,7 @@ public void run() { public void InitializeProcess() { Runtime runTime = Runtime.getRuntime(); - executablePath = "C:\\Users\\Mas Azalya\\source\\repos\\ShimmerCSharpBLEAPI\\ThroughputTestUsingProcess\\bin\\Debug\\netcoreapp3.1\\ThroughputTestUsingProcess.exe"; - + try { p = runTime.exec(executablePath); } catch (IOException e) { @@ -72,8 +106,11 @@ 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(); diff --git a/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingSocket.java b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingSocket.java index 0f4cca6e3..b1429627e 100644 --- a/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingSocket.java +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/javacsharp/throughput/ThroughputTestUsingSocket.java @@ -7,26 +7,35 @@ 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; - public boolean writeResult; static boolean IsSendDataFromJavaToCSharp = true; + static int period = 5; + static String unixTimeStampWhenSend; + static long unixTimeStampWhenReceive; + int maximumThroughput = 0; + String writeToCSharpString = null; + public ThroughputTestUsingSocket() { - writeResult = false; + } - public int ReadDataUsingSocket() { - - String line = null; + public void ReadDataUsingSocket() { int count = 0; + String line = null; int bytePerLine = 20; - int totalSeconds = 60; try { InputStream input = socket.getInputStream(); @@ -36,21 +45,21 @@ public int ReadDataUsingSocket() { while ((line = reader.readLine()) != null) { System.out.println(line); count++; + if(line.length() == 1) { - writeResult = true; + writeToCSharpString = line; } } - System.out.println(writeResult); - System.out.println("Total kilobytes received in " + totalSeconds + " seconds = " + count * bytePerLine / 1000); - System.out.println("Maximum throughput = " + (((count * bytePerLine) / 1000) / totalSeconds) + "kb per second"); - + 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(); } - - return count; } public void WriteDataUsingSocket() { @@ -59,8 +68,8 @@ public void WriteDataUsingSocket() { new Random().nextBytes(bytes); final OutputStream output = socket.getOutputStream(); - final Timer timer1 = new Timer(); - timer1.schedule(new TimerTask() { + final Timer timer = new Timer(); + timer.schedule(new TimerTask() { @Override public void run() { try { @@ -69,7 +78,7 @@ public void run() { { //e.printStackTrace(); System.out.println("Client Disconnected"); - timer1.cancel(); + timer.cancel(); } } }, 0, 1000); @@ -80,6 +89,23 @@ public void run() { } } + 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); @@ -92,10 +118,14 @@ public void InitializeSocket() { 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(){