View Javadoc

1   package org.apache.maven.surefire.util.internal;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
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   * @author Kristian Rosenvold
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          //System.out.println( "TwoThreadBlockingQueue insert " + num + " elements in  = " + elapsed );
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          //System.out.println( "FunkyTwoThreadBlockingQueue insert " + num + " elements in  = " + elapsed );
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        // System.out.println( "TwoThreadBlockingQueue produced and taken " + num + " elements in  = " + elapsed );
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         //System.out.println( "LinkedBlockingQueue insert " + num + " elements in  = " + elapsed );
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         //System.out.println( "ArrayList insert " + num + " elements in  = " + elapsed );
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         //System.out.println( "LinkedList insert " + num + " elements in  = " + elapsed );
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 }