20
20
import org .junit .Ignore ;
21
21
import org .junit .Test ;
22
22
23
+ import java .io .File ;
24
+ import java .io .IOException ;
25
+ import java .net .URI ;
26
+ import java .net .URISyntaxException ;
27
+ import java .nio .file .Files ;
28
+ import java .nio .file .Path ;
23
29
import java .util .ArrayList ;
24
30
import java .util .Arrays ;
31
+ import java .util .HashMap ;
25
32
import java .util .List ;
26
33
import java .util .Locale ;
27
- import java .util .SortedMap ;
28
- import java .util .TreeMap ;
34
+ import java .util .Map ;
35
+ import java .util .stream . Stream ;
29
36
30
37
import static org .junit .Assert .assertEquals ;
31
38
import static org .junit .Assert .fail ;
@@ -187,9 +194,6 @@ public void test_subHash()
187
194
assertArraysEquals (new byte [] { 15 , 14 , 13 }, HashUtils .sub (new byte [] { 0 , 0 , 0 }, new byte [] { 1 , 2 , 3 }));
188
195
assertArraysEquals (new byte [] { 15 , 14 , 13 }, HashUtils .sub (null , new byte [] { 1 , 2 , 3 }));
189
196
190
- // "0f0e0d" - "000000" = "0f0e0d"
191
- assertArraysEquals (new byte [] { 15 , 14 , 13 }, HashUtils .sub (new byte [] { 15 , 14 , 13 }, new byte [] { 0 , 0 , 0 }));
192
-
193
197
// normal cases
194
198
assertArraysEquals (new byte [] { 0 , 0 , 0 }, HashUtils .sub (new byte [] { 1 , 2 , 3 }, new byte [] { 1 , 2 , 3 }));
195
199
// "7f" - "ff" = "80"
@@ -242,7 +246,7 @@ public void test_addHash()
242
246
}
243
247
244
248
@ Test
245
- public void test_sumIterable ()
249
+ public void test_sumHashes ()
246
250
{
247
251
// given
248
252
final List <String > hashes = Arrays .asList (
@@ -260,23 +264,49 @@ public void test_sumIterable()
260
264
261
265
@ Test
262
266
@ Ignore ("This test case is for manual performance check only" )
263
- public void test_performance_buildHashTree ()
267
+ public void hashesLoadingTest ()
264
268
{
265
- long time = System .currentTimeMillis ();
266
- SortedMap <String , String > data = new TreeMap <>();
267
- for (int i = 0 ; i < 500_000 ; i ++)
269
+ final double time = Profiler .measureInMsec (() ->
268
270
{
269
- String id = HashUtils .generateGuid ();
270
- String hash = HashUtils .generateGuid ();
271
- data .put (id , hash );
272
- }
273
- time = System .currentTimeMillis () - time ;
274
- System .out .println (String .format ("%d items prepared in %d ms" , data .size (), time ));
271
+ try
272
+ {
273
+ loadMap ("merkletree/input_hashes.csv" );
274
+ }
275
+ catch (Exception e )
276
+ {
277
+ throw new RuntimeException (e );
278
+ }
279
+ }, 100 );
280
+
281
+ System .out .printf (Locale .US , "loadHashes(): %.3f ms%n" , time );
282
+ // unsorted: 73.64 ms
283
+ // sorted: 41.668 ms (44% faster than unsorted)
284
+ }
275
285
276
- time = System .currentTimeMillis ();
277
- SortedMap <String , String > tree = HashUtils .buildHashTree (data );
278
- time = System .currentTimeMillis () - time ;
286
+ private static Map <String , String > loadMap (String resourceName ) throws IOException , URISyntaxException
287
+ {
288
+ final URI uri = TestHashUtils .class .getClassLoader ()
289
+ .getResource (resourceName )
290
+ .toURI ();
291
+ return loadMap (new File (uri ).toPath ());
292
+ }
293
+
294
+ private static Map <String , String > loadMap (Path fileName ) throws IOException
295
+ {
296
+ final Map <String , String > data = new HashMap <>();
297
+
298
+ try (Stream <String > lines = Files .lines (fileName ))
299
+ {
300
+ lines .skip (1 ) // header
301
+ .forEach (line ->
302
+ {
303
+ final int k = line .indexOf ('\t' );
304
+ final String id = line .substring (0 , k );
305
+ final String hash = line .substring (k + 1 );
306
+ data .put (id , hash );
307
+ });
308
+ }
279
309
280
- System . out . println ( String . format ( "Tree with %d items build in %d ms" , tree . size (), time )) ;
310
+ return data ;
281
311
}
282
312
}
0 commit comments