-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark2.java
More file actions
154 lines (138 loc) · 5.2 KB
/
benchmark2.java
File metadata and controls
154 lines (138 loc) · 5.2 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
151
152
153
154
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.WaitFreeQueue;
public class benchmark2 {
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 WaitFreeQueue<Integer> queues = new WaitFreeQueue<Integer>();
System.out.println("\ntestWaitFreeQueue");
for(int t = 0; t < rounds; ++t) {
queues.initRingTail();
int thread_num = MetaDATAs.thread_num;
final int counts = MetaDATAs.counts / thread_num;
System.out.println("\n" + t + " " + (MetaDATAs.counts) + " Ops times Hello world! " + Thread.currentThread().getName());
Thread.sleep(2000);
long start = System.currentTimeMillis();
Thread[] threads = new Thread[thread_num];
final int[] ints = new int[MetaDATAs.counts / 2];
for(int i = 0; i < thread_num; ++i) {
final int i_ = i;
(threads[i] = new Thread() {
public void run() {
WaitFreeQueue.Handle<Integer> h = queues.register();
for(int j = 0; j < counts; ++j) {
if( j % 2 == 0)
queues.enqueue((i_ * counts + j) / 2, h);
else
ints[queues.dequeue(h)] = 1;
}
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 / 2; ++i) {
if(ints[i] != 1) {
System.out.println("Error: ints[" + i + "]");
verify = false;
}
}
if(verify)
System.out.println("ints[0-" + (MetaDATAs.counts / 2-1) + "] has been Verify through");
System.out.println((MetaDATAs.counts / 2) + " put & pop " + "cost times(seconds): " + ((System.currentTimeMillis() - start) / 1000.0) + " seconds");
Thread.sleep(2000);
}
}
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) + " Ops times Hello world! " + Thread.currentThread().getName());
Thread.sleep(2000);
long start = System.currentTimeMillis();
Thread[] threads = new Thread[thread_num];
final int[] ints = new int[MetaDATAs.counts / 2];
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) {
if( j % 2 == 0)
queues.add((i_ * counts + j) / 2);
else
ints[queues.poll()] = 1;
}
}
}).start();
}
for(int i = 0; i < thread_num; ++i)
threads[i].join();
boolean verify = true;
for(int i = 0; i < MetaDATAs.counts / 2; ++i) {
if(ints[i] != 1) {
System.out.println("Error: ints[" + i + "]");
verify = false;
}
}
if(verify)
System.out.println("ints[0-" + (MetaDATAs.counts / 2-1) + "] has been Verify through");
System.out.println((MetaDATAs.counts / 2) + " put & pop " + "cost times(seconds): " + ((System.currentTimeMillis() - start) / 1000.0) + " seconds");
Thread.sleep(2000);
}
}
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) + " Ops times Hello world! " + Thread.currentThread().getName());
Thread.sleep(2000);
long start = System.currentTimeMillis();
Thread[] threads = new Thread[thread_num];
final int[] ints = new int[MetaDATAs.counts / 2];
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) {
if( j % 2 == 0)
queues.add((i_ * counts + j) / 2);
else
ints[queues.poll()] = 1;
}
}
}).start();
}
for(int i = 0; i < thread_num; ++i)
threads[i].join();
boolean verify = true;
for(int i = 0; i < MetaDATAs.counts / 2; ++i) {
if(ints[i] != 1) {
System.out.println("Error: ints[" + i + "]");
verify = false;
}
}
if(verify)
System.out.println("ints[0-" + (MetaDATAs.counts / 2-1) + "] has been Verify through");
System.out.println((MetaDATAs.counts / 2) + " put & pop " + "cost times(seconds): " + ((System.currentTimeMillis() - start) / 1000.0) + " seconds");
Thread.sleep(2000);
}
}
}