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.Tuple;
32 import org.apache.directory.mavibot.btree.TupleCursor;
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 MultiThreadedInMemoryBtreeTest
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 = BTreeFactory.createInMemoryBTree( "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 create50KBTree() throws IOException
78 {
79 Random random = new Random( System.nanoTime() );
80
81 int nbElems = 50000;
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 % 10000 == 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 TupleCursor<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 cursor.close();
135
136 return nb;
137 }
138
139
140
141
142
143
144
145
146 @Test
147 public void testBrowseMultiThreads() throws InterruptedException
148 {
149 int nbThreads = 100;
150 final CountDownLatch latch = new CountDownLatch( nbThreads );
151
152 Thread writer = new Thread()
153 {
154 public void run()
155 {
156 try
157 {
158 create50KBTree();
159 }
160 catch ( Exception e )
161 {
162 }
163 }
164 };
165
166 long t0 = System.currentTimeMillis();
167
168
169 writer.start();
170
171 for ( int i = 0; i < nbThreads; i++ )
172 {
173 Thread test = new Thread()
174 {
175 public void run()
176 {
177 try
178 {
179 int res = 0;
180 int previous = -1;
181
182 while ( previous < res )
183 {
184 previous = res;
185 res = testBrowse();
186 Thread.sleep( 500 );
187 }
188
189 latch.countDown();
190 }
191 catch ( Exception e )
192 {
193 }
194 }
195 };
196
197
198 test.start();
199 }
200
201
202 latch.await();
203
204 long t1 = System.currentTimeMillis();
205
206 System.out.println( " Time to create 50K entries and to have " + nbThreads + " threads reading them : "
207 + ( ( t1 - t0 ) / 1000 ) + " seconds" );
208 }
209
210
211
212
213
214
215 @Test
216 public void testInsertMultiThreads() throws InterruptedException, IOException
217 {
218 int nbThreads = 100;
219 final CountDownLatch latch = new CountDownLatch( nbThreads );
220
221
222
223 long t0 = System.currentTimeMillis();
224
225 for ( int i = 0; i < nbThreads; i++ )
226 {
227 final long prefix = i;
228 Thread test = new Thread()
229 {
230 public void run()
231 {
232 try
233 {
234
235 for ( int j = 0; j < 1000; j++ )
236 {
237 long value = prefix * 1000 + j;
238 btree.insert( value, Long.toString( value ) );
239
240
241
242
243
244
245
246
247 }
248
249 latch.countDown();
250 }
251 catch ( Exception e )
252 {
253 }
254 }
255 };
256
257
258 test.start();
259 }
260
261
262 latch.await();
263
264 long t1 = System.currentTimeMillis();
265
266
267 try
268 {
269 for ( long i = 0L; i < 10000L; i++ )
270 {
271 assertEquals( Long.toString( i ), btree.get( i ) );
272 }
273 }
274 catch ( KeyNotFoundException knfe )
275 {
276 fail();
277 }
278
279 System.out.println( " Time to create 1M entries : "
280 + ( ( t1 - t0 ) / 1000 ) + " seconds" );
281 }
282 }