-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark3.java
202 lines (182 loc) · 6.35 KB
/
benchmark3.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main.java.com.psly.test;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedTransferQueue;
import main.java.com.psly.concurrent.WaitFreeQueueV1;
public class benchmark3 {
private static final int rounds = 1024;
public static void main(String[] args) throws InterruptedException {
if(args.length == 0 || args[0].trim().equals("WaitFreeQueue"))
testWaitFreeQueue();
else if(args[0].trim().equals("ConcurrentLinkedQueue")) {
testConcurrentLinkedQueue();
}
else if(args[0].trim().equals("LinkedTransferQueue")) {
testLinkedTransferQueue();
}
}
private static void testWaitFreeQueue() throws InterruptedException {
final WaitFreeQueueV1<Integer> queues = new WaitFreeQueueV1<Integer>();
System.out.println("\ntestWaitFreeQueue");
for(int t = 0; ; ++t) {
queues.initRingTail();
int thread_num = MetaDATAs.thread_num;
final int counts = MetaDATAs.counts / thread_num;
System.out.println("\n" + t + " " + (MetaDATAs.counts * 2) + " Ops Hello world! " + Thread.currentThread().getName());
Thread.sleep(1000);
long start = System.currentTimeMillis();
Thread[] threads = new Thread[thread_num];
final int[] ints = new int[MetaDATAs.counts];
for(int i = 0; i < thread_num; ++i) {
final int i_ = i;
(threads[i] = new Thread() {
public void run() {
WaitFreeQueueV1.Handle<Integer> h = queues.register();
for(int j = 0; j < counts; ++j) {
queues.enqueue(i_ * counts + j, h);
}
queues.unregister(h);
}
}).start();
}
for(int i = 0; i < thread_num; ++i)
threads[i].join();
long now = System.currentTimeMillis();
System.out.println(MetaDATAs.counts + " puts times(seconds): " + (now - start) / 1000.0);
for(int i = 0; i < thread_num; ++i) {
(threads[i] = new Thread() {
public void run() {
WaitFreeQueueV1.Handle<Integer> h = queues.register();
for(;;) {
Integer val = queues.dequeue(h);
if(val != null)
ints[val.intValue()] = 1;
else
break;
}
queues.unregister(h);
}
}).start();
}
for(int i = 0; i < thread_num; ++i)
threads[i].join();
boolean verify = true;
for(int i = 0; i < MetaDATAs.counts; ++i) {
if(ints[i] != 1) {
System.out.println("Error: ints[" + i + "]");
verify = false;
}
}
if(verify)
System.out.println("ints[0-" + (MetaDATAs.counts-1) + "] has been Verify through");
System.out.println(MetaDATAs.counts + " pops times(seconds): " + (System.currentTimeMillis() - now) / 1000.0);
Thread.sleep(1000);
}
}
private static void testConcurrentLinkedQueue() throws InterruptedException {
final Queue<Integer> queues = new ConcurrentLinkedQueue<Integer>();
System.out.println("\ntestConcurrentLinkedQueue");
for(int t = 0; t < rounds; ++t) {
int thread_num = MetaDATAs.thread_num;
final int counts = MetaDATAs.counts / thread_num;
System.out.println("\n" + t + " " + (MetaDATAs.counts * 2) + " Ops Hello world! " + Thread.currentThread().getName());
Thread.sleep(1000);
long start = System.currentTimeMillis();
Thread[] threads = new Thread[thread_num];
final int[] ints = new int[MetaDATAs.counts];
for(int i = 0; i < thread_num; ++i) {
final int i_ = i;
(threads[i] = new Thread() {
public void run() {
for(int j = 0; j < counts; ++j) {
queues.add(i_ * counts + j);
}
}
}).start();
}
for(int i = 0; i < thread_num; ++i)
threads[i].join();
long now = System.currentTimeMillis();
System.out.println(MetaDATAs.counts + " puts times(seconds): " + (now - start) / 1000.0);
for(int i = 0; i < thread_num; ++i) {
(threads[i] = new Thread() {
public void run() {
for(;;) {
Integer val = queues.poll();
if(val != null)
ints[val.intValue()] = 1;
else
break;
}
}
}).start();
}
for(int i = 0; i < thread_num; ++i)
threads[i].join();
boolean verify = true;
for(int i = 0; i < MetaDATAs.counts; ++i) {
if(ints[i] != 1) {
System.out.println("Error: ints[" + i + "]");
verify = false;
}
}
if(verify)
System.out.println("ints[0-" + (MetaDATAs.counts-1) + "] has been Verify through");
System.out.println(MetaDATAs.counts + " pops times(seconds): " + (System.currentTimeMillis() - now) / 1000.0);
Thread.sleep(1000);
}
}
private static void testLinkedTransferQueue() throws InterruptedException {
final Queue<Integer> queues = new LinkedTransferQueue<Integer>();
System.out.println("\ntestLinkedTransferQueue");
for(int t = 0; t < rounds; ++t) {
int thread_num = MetaDATAs.thread_num;
final int counts = MetaDATAs.counts / thread_num;
System.out.println("\n" + t + " " + (MetaDATAs.counts * 2) + " Ops Hello world! " + Thread.currentThread().getName());
Thread.sleep(1000);
long start = System.currentTimeMillis();
Thread[] threads = new Thread[thread_num];
final int[] ints = new int[MetaDATAs.counts];
for(int i = 0; i < thread_num; ++i) {
final int i_ = i;
(threads[i] = new Thread() {
public void run() {
for(int j = 0; j < counts; ++j) {
queues.add(i_ * counts + j);
}
}
}).start();
}
for(int i = 0; i < thread_num; ++i)
threads[i].join();
long now = System.currentTimeMillis();
System.out.println(MetaDATAs.counts + " puts times(seconds): " + (now - start) / 1000.0);
for(int i = 0; i < thread_num; ++i) {
(threads[i] = new Thread() {
public void run() {
for(;;) {
Integer val = queues.poll();
if(val != null)
ints[val.intValue()] = 1;
else
break;
}
}
}).start();
}
for(int i = 0; i < thread_num; ++i)
threads[i].join();
boolean verify = true;
for(int i = 0; i < MetaDATAs.counts; ++i) {
if(ints[i] != 1) {
System.out.println("Error: ints[" + i + "]");
verify = false;
}
}
if(verify)
System.out.println("ints[0-" + (MetaDATAs.counts-1) + "] has been Verify through");
System.out.println(MetaDATAs.counts + " pops times(seconds): " + (System.currentTimeMillis() - now) / 1000.0);
Thread.sleep(1000);
}
}
}