1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.mavibot.btree;
21
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import java.io.IOException;
27 import java.util.Random;
28 import java.util.concurrent.CountDownLatch;
29
30 import org.apache.directory.mavibot.btree.BTree;
31 import org.apache.directory.mavibot.btree.Cursor;
32 import org.apache.directory.mavibot.btree.Tuple;
33 import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
34 import org.apache.directory.mavibot.btree.serializer.LongSerializer;
35 import org.apache.directory.mavibot.btree.serializer.StringSerializer;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39
40
41
42
43
44
45
46 public class MultiThreadedBtreeTest
47 {
48
49 private static BTree<Long, String> btree;
50
51
52
53
54
55
56 @BeforeClass
57 public static void setup() throws IOException
58 {
59 btree = new BTree<Long, String>( "test", new LongSerializer(), new StringSerializer() );
60 }
61
62
63
64
65
66 @AfterClass
67 public static void shutdown() throws IOException
68 {
69 btree.close();
70 }
71
72
73
74
75
76
77 private void create500KBTree() throws IOException
78 {
79 Random random = new Random( System.nanoTime() );
80
81 int nbElems = 500000;
82
83
84 btree.setPageSize( 32 );
85
86 for ( int i = 0; i < nbElems; i++ )
87 {
88 Long key = ( long ) random.nextLong();
89 String value = Long.toString( key );
90
91 try
92 {
93 btree.insert( key, value );
94
95 if ( i % 100000 == 0 )
96 {
97 System.out.println( "Written " + i + " elements" );
98 }
99 }
100 catch ( Exception e )
101 {
102 e.printStackTrace();
103 System.out.println( btree );
104 System.out.println( "Error while adding " + value );
105 return;
106 }
107 }
108 }
109
110
111
112
113
114
115
116 private int testBrowse() throws IOException
117 {
118 Cursor<Long, String> cursor = btree.browse();
119
120 int nb = 0;
121 long elem = Long.MIN_VALUE;
122
123 while ( cursor.hasNext() )
124 {
125 Tuple<Long, String> res = cursor.next();
126
127 if ( res.getKey() > elem )
128 {
129 elem = res.getKey();
130 nb++;
131 }
132 }
133
134 long revision = cursor.getRevision();
135
136 cursor.close();
137
138
139
140
141 return nb;
142 }
143
144
145
146
147
148
149
150
151 @Test
152 public void testBrowseMultiThreads() throws InterruptedException
153 {
154 int nbThreads = 100;
155 final CountDownLatch latch = new CountDownLatch( nbThreads );
156
157 Thread writer = new Thread()
158 {
159 public void run()
160 {
161 try
162 {
163 create500KBTree();
164 }
165 catch ( Exception e )
166 {
167 }
168 }
169 };
170
171 long t0 = System.currentTimeMillis();
172
173
174 writer.start();
175
176 for ( int i = 0; i < nbThreads; i++ )
177 {
178 Thread test = new Thread()
179 {
180 public void run()
181 {
182 try
183 {
184 int res = 0;
185 int previous = -1;
186
187 while ( previous < res )
188 {
189 previous = res;
190 res = testBrowse();
191 Thread.sleep( 500 );
192 }
193
194 latch.countDown();
195 }
196 catch ( Exception e )
197 {
198 }
199 }
200 };
201
202
203 test.start();
204 }
205
206
207 latch.await();
208
209 long t1 = System.currentTimeMillis();
210
211 System.out.println( " Time to create 500K entries and to have " + nbThreads + " threads reading them : "
212 + ( ( t1 - t0 ) / 1000 ) + " seconds" );
213 }
214
215
216
217
218
219
220 @Test
221 public void testInsertMultiThreads() throws InterruptedException, IOException
222 {
223 int nbThreads = 100;
224 final CountDownLatch latch = new CountDownLatch( nbThreads );
225
226
227
228 long t0 = System.currentTimeMillis();
229
230 for ( int i = 0; i < nbThreads; i++ )
231 {
232 final long prefix = i;
233 Thread test = new Thread()
234 {
235 public void run()
236 {
237 try
238 {
239
240 for ( int j = 0; j < 10000; j++ )
241 {
242 long value = prefix * 10000 + j;
243 btree.insert( value, Long.toString( value ) );
244
245
246
247
248
249
250
251
252 }
253
254 latch.countDown();
255 }
256 catch ( Exception e )
257 {
258 }
259 }
260 };
261
262
263 test.start();
264 }
265
266
267 latch.await();
268
269 long t1 = System.currentTimeMillis();
270
271
272 try
273 {
274 for ( long i = 0L; i < 100000L; i++ )
275 {
276 assertEquals( Long.toString( i ), btree.get( i ) );
277 }
278 }
279 catch ( KeyNotFoundException knfe )
280 {
281 fail();
282 }
283
284 System.out.println( " Time to create 1M entries : "
285 + ( ( t1 - t0 ) / 1000 ) + " seconds" );
286 }
287 }