1 package org.apache.maven.surefire.util.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.LinkedList;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.FutureTask;
26 import java.util.concurrent.LinkedBlockingQueue;
27
28 import junit.framework.TestCase;
29
30
31
32
33 public class TwoThreadBlockingQueueTest
34 extends TestCase
35 {
36 final int num = 100000;
37
38 public void testPut()
39 throws Exception
40 {
41 BlockingQueue twoThreadBlockingQueue = new TwoThreadBlockingQueue();
42
43 String[] items = generate( num );
44 long start = System.currentTimeMillis();
45 for ( int i = 0; i < num; i++ )
46 {
47 twoThreadBlockingQueue.add( items[i] );
48 }
49 long elapsed = System.currentTimeMillis() - start;
50
51 System.gc();
52 }
53
54 public void testFunkyPut()
55 throws Exception
56 {
57 FunkyTwoThreadBlockingQueue twoThreadBlockingQueue = new FunkyTwoThreadBlockingQueue();
58
59 String[] items = generate( num );
60 long start = System.currentTimeMillis();
61 for ( int i = 0; i < num; i++ )
62 {
63 twoThreadBlockingQueue.put( items[i] );
64 }
65 long elapsed = System.currentTimeMillis() - start;
66
67 System.gc();
68 }
69
70
71 public void testPutAndTake()
72 throws Exception
73 {
74 final FunkyTwoThreadBlockingQueue twoThreadBlockingQueue = new FunkyTwoThreadBlockingQueue();
75
76 Callable consumer = new Callable()
77 {
78 public Object call()
79 throws Exception
80 {
81 int num = 0;
82 Object taken;
83 do
84 {
85 taken = twoThreadBlockingQueue.take();
86 if (taken != TwoThreadBlockingQueue.poison) {
87 assertEquals( "item" + num++, taken );
88 }
89 }
90 while ( taken != TwoThreadBlockingQueue.poison);
91 return taken;
92 }
93 };
94
95 FutureTask futureTask = new FutureTask( consumer );
96 Thread thread = new Thread( futureTask );
97 thread.start();
98
99 String[] items = generate( num );
100 long start = System.currentTimeMillis();
101 for ( int i = 0; i < num; i++ )
102 {
103 twoThreadBlockingQueue.put( items[i] );
104 }
105 twoThreadBlockingQueue.put( TwoThreadBlockingQueue.poison );
106 long elapsed = System.currentTimeMillis() - start;
107
108 futureTask.get();
109
110
111 System.gc();
112 }
113
114 public void testLBQPut()
115 throws Exception
116 {
117 LinkedBlockingQueue twoThreadBlockingQueue = new LinkedBlockingQueue();
118
119 String[] items = generate( num );
120 long start = System.currentTimeMillis();
121 for ( int i = 0; i < num; i++ )
122 {
123 twoThreadBlockingQueue.put( items[i] );
124 }
125 long elapsed = System.currentTimeMillis() - start;
126
127 System.gc();
128 }
129
130 public void testArrayList()
131 throws Exception
132 {
133 ArrayList twoThreadBlockingQueue = new ArrayList( num);
134
135 String[] items = generate( num );
136 long start = System.currentTimeMillis();
137 for ( int i = 0; i < num; i++ )
138 {
139 twoThreadBlockingQueue.add( items[i] );
140 }
141 long elapsed = System.currentTimeMillis() - start;
142
143 System.gc();
144 }
145
146 public void testLinkedList()
147 throws Exception
148 {
149 LinkedList twoThreadBlockingQueue = new LinkedList( );
150
151 String[] items = generate( num );
152 long start = System.currentTimeMillis();
153 for ( int i = 0; i < num; i++ )
154 {
155 twoThreadBlockingQueue.add( items[i] );
156 }
157 long elapsed = System.currentTimeMillis() - start;
158
159 System.gc();
160 }
161
162
163 public void testTake()
164 throws Exception
165 {
166
167 }
168
169 String[] generate( int num )
170 {
171 String[] result = new String[num];
172 for ( int i = 0; i < num; i++ )
173 {
174 result[i] = "item" + i;
175 }
176 return result;
177 }
178 }