-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathThroughputTestUsingSocket.java
More file actions
150 lines (124 loc) · 4 KB
/
Copy pathThroughputTestUsingSocket.java
File metadata and controls
150 lines (124 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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();
}
}
}