forked from theonlyanson/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaytimeClient.java
63 lines (39 loc) · 1.4 KB
/
DaytimeClient.java
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
/**
*
* @author fxowl97
*/
import java.io.DataInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
public class DaytimeClient {
public static final int SERVICE_PORT = 13;
public static void main(String[] args) {
System.out.println("Warming up...");
connectThreadedServer(10);
System.out.println("Measuring performance...");
connectThreadedServer(100);
}
public static void connectThreadedServer(int n) {
double totalTime = 0, min = 0, max = 0;
for (int i = 0; i < n; i++) {
long timeStamp = System.nanoTime();
try {
Socket client = new Socket(InetAddress.getLocalHost(), SERVICE_PORT);
DataInputStream in = new DataInputStream(client.getInputStream());
in.readUTF();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
double timeTaken = (System.nanoTime() - timeStamp) / 1000000.0;
if (max < timeTaken)
max = timeTaken;
if (min > timeTaken || min == 0)
min = timeTaken;
totalTime += timeTaken;
}
System.out.printf("Average time for %d requests is: %.2f ms\nminimum time is : %.2f ms\nMaximum time: %.2f ms\n\n", n,
(totalTime / n), min, max);
}
}