/
RailWayTest.java
57 lines (46 loc) · 1.6 KB
/
RailWayTest.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
package com.asyncj.core.api.article.onetrain;
/**
*
* @author Aliaksei Papou
* @since 23.11.13
*/
public class RailwayTest {
public static void main(String[] args) {
new RailwayTest().testRailWay();
}
public void testRailWay() {
final Railway railway = new Railway();
final long n = 20000000000l;
new Thread() {
long lastValue = 0;
@Override
public void run() {
while (lastValue < n) {
Train train = railway.waitTrainOnStation(1);
int count = train.goodsCount();
for (int i = 0; i < count; i++) {
lastValue = train.getGoods(i);
}
railway.sendTrain();
}
}
}.start();
final long start = System.nanoTime();
long i = 0;
while (i < n) {
Train train = railway.waitTrainOnStation(0);
int capacity = train.getCapacity();
for (int j = 0; j < capacity; j++) {
train.addGoods((int)i++);
}
railway.sendTrain();
if (i % 1000000 == 0) {
final long duration = System.nanoTime() - start;
final long ops = (i * 1000L * 1000L * 1000L) / duration;
System.out.format("ops/sec = %,d\n", ops);
System.out.format("trains/sec = %,d\n", ops / Train.CAPACITY);
System.out.format("latency nanos = %.3f%n\n", duration / (float)(i) * (float) Train.CAPACITY);
}
}
}
}