-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTest.java
More file actions
133 lines (117 loc) · 6.21 KB
/
Test.java
File metadata and controls
133 lines (117 loc) · 6.21 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
package ticketingsystem;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
public class Test {
private final static int ROUTE_NUM = 10;
private final static int COACH_NUM = 10;
private final static int SEAT_NUM = 100;
private final static int STATION_NUM = 20;
private final static int TEST_NUM = 100000;
private final static int refund = 10;
private final static int buy = 30;
private final static int query = 100;
private final static int thread = 128;
private final static long[] buyTicketTime = new long[thread];
private final static long[] refundTime = new long[thread];
private final static long[] inquiryTime = new long[thread];
private final static long[] buyTotal = new long[thread];
private final static long[] refundTotal = new long[thread];
private final static long[] inquiryTotal = new long[thread];
private final static AtomicInteger threadId = new AtomicInteger(0);
static String passengerName() {
Random rand = new Random();
long uid = rand.nextInt(TEST_NUM);
return "passenger" + uid;
}
public static void main(String[] args) throws InterruptedException {
final int[] threadNums = { 4, 8, 16, 32, 64 };
int p;
for (p = 0; p < threadNums.length; ++p) {
final TicketingDS tds = new TicketingDS(ROUTE_NUM, COACH_NUM, SEAT_NUM, STATION_NUM, threadNums[p]);
Thread[] threads = new Thread[threadNums[p]];
for (int i = 0; i < threadNums[p]; i++) {
threads[i] = new Thread(new Runnable() {
public void run() {
Random rand = new Random();
Ticket ticket = new Ticket();
int id = threadId.getAndIncrement();
ArrayList<Ticket> soldTicket = new ArrayList<>();
for (int i = 0; i < TEST_NUM; i++) {
int sel = rand.nextInt(query);
if (0 <= sel && sel < refund && soldTicket.size() > 0) { // refund ticket 0-10
int select = rand.nextInt(soldTicket.size());
if ((ticket = soldTicket.remove(select)) != null) {
long s = System.currentTimeMillis();
tds.refundTicket(ticket);
long e = System.currentTimeMillis();
refundTime[id] += e - s;
refundTotal[id] += 1;
} else {
System.out.println("ErrOfRefund2");
}
} else if (refund <= sel && sel < buy) { // buy ticket 10-30
String passenger = passengerName();
int route = rand.nextInt(ROUTE_NUM) + 1;
int departure = rand.nextInt(STATION_NUM - 1) + 1;
int arrival = departure + rand.nextInt(STATION_NUM - departure) + 1;
long s = System.currentTimeMillis();
ticket = tds.buyTicket(passenger, route, departure, arrival);
long e = System.currentTimeMillis();
buyTicketTime[id] += e - s;
buyTotal[id] += 1;
if (ticket != null) {
soldTicket.add(ticket);
}
} else if (buy <= sel && sel < query) { // inquiry ticket 30-100
int route = rand.nextInt(ROUTE_NUM) + 1;
int departure = rand.nextInt(STATION_NUM - 1) + 1;
int arrival = departure + rand.nextInt(STATION_NUM - departure) + 1;
long s = System.currentTimeMillis();
tds.inquiry(route, departure, arrival);
long e = System.currentTimeMillis();
inquiryTime[id] += e - s;
inquiryTotal[id] += 1;
}
}
}
});
}
long start = System.currentTimeMillis();
for (int i = 0; i < threadNums[p]; ++i)
threads[i].start();
for (int i = 0; i < threadNums[p]; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
long buyTotalTime = calculateTotal(buyTicketTime, threadNums[p]);
long refundTotalTime = calculateTotal(refundTime, threadNums[p]);
long inquiryTotalTime = calculateTotal(inquiryTime, threadNums[p]);
long bTotal = calculateTotal(buyTotal, threadNums[p]);
long rTotal = calculateTotal(refundTotal, threadNums[p]);
long iTotal = calculateTotal(inquiryTotal, threadNums[p]);
double buyAvgTime = (double) (buyTotalTime) / bTotal;
double refundAvgTime = (double) (refundTotalTime) / rTotal;
double inquiryAvgTime = (double) (inquiryTotalTime) / iTotal;
long time = end - start;
long t = (long) (threadNums[p] * TEST_NUM / (double) time); // 1000是从ms转换为s
System.out.println(String.format(
"ThreadNum: %d BuyAvgTime(ms): %.5f RefundAvgTime(ms): %.5f InquiryAvgTime(ms): %.5f ThroughOut(op/ms): %d",
threadNums[p], buyAvgTime, refundAvgTime, inquiryAvgTime, t));
clear();
}
}
private static long calculateTotal(long[] array, int threadNums) {
long res = 0;
for (int i = 0; i < threadNums; ++i)
res += array[i];
return res;
}
private static void clear() {
threadId.set(0);
long[][] arrays = { buyTicketTime, refundTime, inquiryTime, buyTotal, refundTotal, inquiryTotal };
for (int i = 0; i < arrays.length; ++i)
for (int j = 0; j < arrays[i].length; ++j)
arrays[i][j] = 0;
}
}