-
Notifications
You must be signed in to change notification settings - Fork 66
/
Benchmarks.java
1258 lines (1127 loc) · 48.2 KB
/
Benchmarks.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package software.amazon.event.ruler;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.GZIPInputStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
/**
* Runs benchmarks to measure various aspects of Ruler performance with the help of JUnit. Normally, most of
* the tests here are marked @Ignore because they are not required as part of normal unit testing. However, after
* any nontrivial change to Ruler code, the tests should be re-run and the results checked against the performance
* numbers given in README.md to ensure there hasn't been a performance regression.
*
* Several of the benchmarks operate on a large file containing 213068 JSON records averaging about 900 bytes in size.
* There are two slightly varying versions, citylots.json.gz and citylots2.json.gz. Between the two of them they
* total ~400Mbytes, which makes Ruler a little slow to check out from git.
*/
public class Benchmarks {
// original citylots
private static final String CITYLOTS_JSON = "src/test/data/citylots.json.gz";
// revised citylots with structured arrays
private static final String CITYLOTS_2 = "src/test/data/citylots2.json.gz";
public static final String[] EXACT_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ \"1430022\" ]\n" +
" }" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ \"2607117\" ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ \"2607218\" ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ \"3745012\" ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ \"VACSTWIL\" ]\n" +
" }\n" +
"}"
};
private final int[] EXACT_MATCHES = { 1, 101, 35, 655, 1 };
public static final String[] WILDCARD_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ { \"wildcard\": \"143*\" } ]\n" +
" }" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ { \"wildcard\": \"2*0*1*7\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ { \"wildcard\": \"*218\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ { \"wildcard\": \"3*5*2\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ { \"wildcard\": \"VA*IL\" } ]\n" +
" }\n" +
"}"
};
private final int[] WILDCARD_MATCHES = { 490, 713, 43, 2540, 1 };
public static final String[] PREFIX_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"prefix\": \"AC\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"prefix\": \"BL\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"prefix\": \"DR\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"prefix\": \"FU\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"prefix\": \"RH\" } ]\n" +
" }\n" +
"}"
};
private final int[] PREFIX_MATCHES = { 24, 442, 38, 2387, 328 };
public static final String[] PREFIX_EQUALS_IGNORE_CASE_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"prefix\": { \"equals-ignore-case\": \"Ac\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"prefix\": { \"equals-ignore-case\": \"bL\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"prefix\": { \"equals-ignore-case\": \"dR\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"prefix\": { \"equals-ignore-case\": \"Fu\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"prefix\": { \"equals-ignore-case\": \"rH\" } } ]\n" +
" }\n" +
"}"
};
private final int[] PREFIX_EQUALS_IGNORE_CASE_MATCHES = { 24, 442, 38, 2387, 328 };
public static final String[] SUFFIX_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"suffix\": \"ON\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"suffix\": \"KE\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"suffix\": \"MM\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"suffix\": \"ING\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"suffix\": \"GO\" } ]\n" +
" }\n" +
"}"
};
private final int[] SUFFIX_MATCHES = { 17921, 871, 13, 1963, 682 };
public static final String[] SUFFIX_EQUALS_IGNORE_CASE_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"suffix\": { \"equals-ignore-case\": \"oN\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"suffix\": { \"equals-ignore-case\": \"Ke\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"suffix\": { \"equals-ignore-case\": \"mM\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"suffix\": { \"equals-ignore-case\": \"InG\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"suffix\": { \"equals-ignore-case\": \"gO\" } } ]\n" +
" }\n" +
"}"
};
private final int[] SUFFIX_EQUALS_IGNORE_CASE_MATCHES = { 17921, 871, 13, 1963, 682 };
public static final String[] EQUALS_IGNORE_CASE_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"equals-ignore-case\": \"jefferson\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"equals-ignore-case\": \"bEaCh\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"equals-ignore-case\": \"HyDe\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"equals-ignore-case\": \"CHESTNUT\" } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"ST_TYPE\": [ { \"equals-ignore-case\": \"st\" } ]\n" +
" }\n" +
"}"
};
private final int[] EQUALS_IGNORE_CASE_MATCHES = { 131, 211, 1758, 825, 116386 };
public static final String[] COMPLEX_ARRAYS_RULES = {
"{\n" +
" \"geometry\": {\n" +
" \"type\": [ \"Polygon\" ],\n" +
" \"coordinates\": {\n" +
" \"x\": [ { \"numeric\": [ \"=\", -122.42916360922355 ] } ]\n" +
" }\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"type\": [ \"MultiPolygon\" ],\n" +
" \"coordinates\": {\n" +
" \"y\": [ { \"numeric\": [ \"=\", 37.729900216217324 ] } ]\n" +
" }\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"coordinates\": {\n" +
" \"x\": [ { \"numeric\": [ \"<\", -122.41600944012424 ] } ]\n" +
" }\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"coordinates\": {\n" +
" \"x\": [ { \"numeric\": [ \">\", -122.41600944012424 ] } ]\n" +
" }\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"coordinates\": {\n" +
" \"x\": [ { \"numeric\": [ \">\", -122.46471267081272, \"<\", -122.4063085128395 ] } ]\n" +
" }\n" +
" }\n" +
"}"
};
private final int[] COMPLEX_ARRAYS_MATCHES = { 218, 1, 149446, 64368, 127485 };
public static final String[] NUMERIC_RULES = {
"{\n" +
" \"geometry\": {\n" +
" \"type\": [ \"Polygon\" ],\n" +
" \"firstCoordinates\": {\n" +
" \"x\": [ { \"numeric\": [ \"=\", -122.42916360922355 ] } ]\n" +
" }\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"type\": [ \"MultiPolygon\" ],\n" +
" \"firstCoordinates\": {\n" +
" \"z\": [ { \"numeric\": [ \"=\", 0 ] } ]\n" +
" }\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"firstCoordinates\": {\n" +
" \"x\": [ { \"numeric\": [ \"<\", -122.41600944012424 ] } ]\n" +
" }\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"firstCoordinates\": {\n" +
" \"x\": [ { \"numeric\": [ \">\", -122.41600944012424 ] } ]\n" +
" }\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"firstCoordinates\": {\n" +
" \"x\": [ { \"numeric\": [ \">\", -122.46471267081272, \"<\", -122.4063085128395 ] } ]\n" +
" }\n" +
" }\n" +
"}"
};
private final int[] NUMERIC_MATCHES = { 2, 120, 148948, 64120, 127053 };
public static final String[] ANYTHING_BUT_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"anything-but\": [ \"FULTON\" ] } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"anything-but\": [ \"MASON\" ] } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"ST_TYPE\": [ { \"anything-but\": [ \"ST\" ] } ]\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"type\": [ {\"anything-but\": [ \"Polygon\" ] } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"FROM_ST\": [ { \"anything-but\": [ \"441\" ] } ]\n" +
" }\n" +
"}"
};
private final int[] ANYTHING_BUT_MATCHES = { 211158, 210411, 96682, 120, 210615 };
public static final String[] ANYTHING_BUT_IGNORE_CASE_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"anything-but\": {\"equals-ignore-case\": [ \"Fulton\" ] } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"anything-but\": {\"equals-ignore-case\": [ \"Mason\" ] } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"ST_TYPE\": [ { \"anything-but\": {\"equals-ignore-case\": [ \"st\" ] } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"type\": [ {\"anything-but\": {\"equals-ignore-case\": [ \"polygon\" ] } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"FROM_ST\": [ { \"anything-but\": {\"equals-ignore-case\": [ \"441\" ] } } ]\n" +
" }\n" +
"}"
};
private final int[] ANYTHING_BUT_IGNORE_CASE_MATCHES = { 211158, 210411, 96682, 120, 210615 };
public static final String[] ANYTHING_BUT_PREFIX_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"anything-but\": {\"prefix\": \"FULTO\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"anything-but\": { \"prefix\": \"MASO\"} } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"ST_TYPE\": [ { \"anything-but\": {\"prefix\": \"S\"} } ]\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"type\": [ {\"anything-but\": {\"prefix\": \"Poly\"} } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"FROM_ST\": [ { \"anything-but\": {\"prefix\": \"44\"} } ]\n" +
" }\n" +
"}"
};
private final int[] ANYTHING_BUT_PREFIX_MATCHES = { 211158, 210118, 96667, 120, 209091 };
public static final String[] ANYTHING_BUT_SUFFIX_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"anything-but\": {\"suffix\": \"ULTON\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"STREET\": [ { \"anything-but\": { \"suffix\": \"ASON\"} } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"ST_TYPE\": [ { \"anything-but\": {\"suffix\": \"T\"} } ]\n" +
" }\n" +
"}",
"{\n" +
" \"geometry\": {\n" +
" \"type\": [ {\"anything-but\": {\"suffix\": \"olygon\"} } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"FROM_ST\": [ { \"anything-but\": {\"suffix\": \"41\"} } ]\n" +
" }\n" +
"}"
};
private final int[] ANYTHING_BUT_SUFFIX_MATCHES = { 211136, 210411, 94908, 0, 209055 };
public static final String[] ANYTHING_BUT_WILDCARD_RULES = {
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ { \"anything-but\": {\"wildcard\": \"143*\" } } ]\n" +
" }" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ { \"anything-but\": {\"wildcard\": \"2*0*1*7\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ { \"anything-but\": {\"wildcard\": \"*218\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ { \"anything-but\": {\"wildcard\": \"3*5*2\" } } ]\n" +
" }\n" +
"}",
"{\n" +
" \"properties\": {\n" +
" \"MAPBLKLOT\": [ { \"anything-but\": {\"wildcard\": \"VA*IL\" } } ]\n" +
" }\n" +
"}"
};
private final int[] ANYTHING_BUT_WILDCARD_MATCHES = { 212578, 212355, 213025, 210528, 213067 };
// This needs to be run with -Xms2g -Xmx2g (Dunno about the 2g but the same Xms and Xmx, and big enough
// to hold several copies of the 200M in citylots2
private final static int NUMBER_OF_RULES = 400000;
private final static int NUMBER_OF_FIELD_NAMES = 10;
@Test
public void ruleMemoryBenchmark() throws Exception {
// 10 random field names
String[] fieldNames = new String[NUMBER_OF_FIELD_NAMES];
for (int i = 0; i < NUMBER_OF_FIELD_NAMES; i++) {
fieldNames[i] = randomAscii(12);
}
// now we're going to add 10K random rules and see how much memory it uses
String ruleTemplate = "{\n" +
" \"Abernathy\": {\n" +
" \"-FIELD1\": [ -NUMBER1 ]\n" +
" },\n" +
" \"Barnard\": {\n" +
" \"Oliver\": [ { \"prefix\": \"-PREFIX\" } ]\n" +
" }\n" +
"}";
List<String> rules = new ArrayList<>();
Machine mm = new Machine();
for (int i = 0; i < NUMBER_OF_RULES; i++) {
String rule = ruleTemplate;
rule = rule.replace("-FIELD1", fieldNames[i % NUMBER_OF_FIELD_NAMES]);
//rule = rule.replace("-NUMBER1", Double.toString(1000000.0 * rand.nextDouble()));
rule = rule.replace("-NUMBER1", Integer.toString(rand.nextInt(100)));
rule = rule.replace("-PREFIX", randomAscii(6));
rules.add(rule);
// handy when fooling with rule templates
// if (i < 12) {
// System.out.println("R: " + rule);
// }
}
System.gc();
long memBefore = Runtime.getRuntime().freeMemory();
int sizeBefore = mm.approximateObjectCount(Integer.MAX_VALUE);
System.out.printf("Before: %.1f (%d)\n", 1.0 * memBefore / 1000000, sizeBefore);
for (int i = 0; i < rules.size(); i++) {
mm.addRule("mm" + i, rules.get(i));
}
System.gc();
long memAfter = Runtime.getRuntime().freeMemory();
int sizeAfter = mm.approximateObjectCount(Integer.MAX_VALUE);
System.out.printf("After: %.1f (%d)\n", 1.0 * memAfter / 1000000, sizeAfter);
int perRuleMem = (int) ((1.0 * (memAfter - memBefore)) / rules.size());
int perRuleSize = (int) ((1.0 * (sizeAfter - sizeBefore)) / rules.size());
System.out.println("Per rule: " + perRuleMem + " (" + perRuleSize + ")");
rules.clear();
}
// This needs to be run with -Xms2g -Xmx2g
// Only use exact match to verify exact match memory optimization change.
@Test
public void exactRuleMemoryBenchmark() throws Exception {
// 10 random field names
String[] fieldNames = new String[NUMBER_OF_FIELD_NAMES];
for (int i = 0; i < NUMBER_OF_FIELD_NAMES; i++) {
fieldNames[i] = randomAscii(12);
}
// now we're going to add 10K random rules and see how much memory it uses
String ruleTemplate = "{\n" +
" \"Abernathy\": {\n" +
" \"-FIELD1\": [ -STR1 ]\n" +
" },\n" +
" \"Barnard\": {\n" +
" \"Oliver\": [ -STR2 ]\n" +
" }\n" +
"}";
List<String> rules = new ArrayList<>();
Machine mm = new Machine();
for (int i = 0; i < NUMBER_OF_RULES; i++) {
String rule = ruleTemplate;
rule = rule.replace("-FIELD1", fieldNames[i % NUMBER_OF_FIELD_NAMES]);
rule = rule.replace("-STR1", "\"" + randomAscii(20) + "\"");
rule = rule.replace("-STR2", "\"" + randomAscii(20) + "\"");
rules.add(rule);
}
System.gc();
long memBefore = Runtime.getRuntime().freeMemory();
int sizeBefore = mm.approximateObjectCount();
System.out.printf("Before: %.1f (%d)\n", 1.0 * memBefore / 1000000, sizeBefore);
for (int i = 0; i < rules.size(); i++) {
mm.addRule("mm" + i, rules.get(i));
}
System.gc();
long memAfter = Runtime.getRuntime().freeMemory();
int sizeAfter = mm.approximateObjectCount();
System.out.printf("After: %.1f (%d)\n", 1.0 * memAfter / 1000000, sizeAfter);
int perRuleMem = (int) ((1.0 * (memAfter - memBefore)) / rules.size());
int perRuleSize = (int) ((1.0 * (sizeAfter - sizeBefore)) / rules.size());
System.out.println("Per rule: " + perRuleMem + " (" + perRuleSize + ")");
rules.clear();
}
@Test
public void lowNameStateReuseMemoryBenchmark() throws Exception {
Machine machine = new Machine();
System.out.println("Low NameState Reuse Memory Benchmark");
nameStateReuseMemoryBenchmark(machine);
}
@Test
public void highNameStateReuseMemoryBenchmark() throws Exception {
Machine machine = Machine.builder().withAdditionalNameStateReuse(true).build();
System.out.println("High NameState Reuse Memory Benchmark");
nameStateReuseMemoryBenchmark(machine);
}
private void nameStateReuseMemoryBenchmark(Machine machine) throws Exception {
int maxKeys = 256;
System.gc();
long memBefore = Runtime.getRuntime().freeMemory();
int sizeBefore = machine.approximateObjectCount();
System.out.printf("Before: %.1f (%d)\n", 1.0 * memBefore / 1000000, sizeBefore);
// For a readable version with a similar setup to the rules being added here, see
// MachineTest.testApproximateObjectCountEachKeyHasThreePatternsAddedOneAtATime. By adding one pattern at a time
// for each key, we create three different branches in the low NameState reuse test, but a single branch in the
// high NameState reuse test. So with low NameState reuse, Machine size grows exponentially with number of keys.
for (int i = 0; i < maxKeys; i++) {
StringBuilder prefix = new StringBuilder();
for (int j = 0; j < i; j++) {
int k = 3 * j;
prefix.append("\"key" + k + "\": [\"" + k + "\", \"" + (k + 1) + "\", \"" + (k + 2) + "\"], ");
}
int k = 3 * i;
machine.addRule("" + k, "{" + prefix + "\"key" + i + "\": [\"" + k + "\"]}");
machine.addRule("" + k + 1, "{" + prefix + "\"key" + i + "\": [\"" + (k + 1) + "\"]}");
machine.addRule("" + k + 2, "{" + prefix + "\"key" + i + "\": [\"" + (k + 2) + "\"]}");
}
System.gc();
long memAfter = Runtime.getRuntime().freeMemory();
int sizeAfter = machine.approximateObjectCount();
System.out.printf("After: %.1f (%d)\n", 1.0 * memAfter / 1000000, sizeAfter);
int perRuleMem = (int) ((1.0 * (memAfter - memBefore)) / (maxKeys * 3));
int perRuleSize = (int) ((1.0 * (sizeAfter - sizeBefore)) / (maxKeys * 3));
System.out.println("Per rule: " + perRuleMem + " (" + perRuleSize + ")");
}
@Test
public void AnythingButPerformanceBenchmark() throws Exception {
readCityLots2();
final Machine m = new Machine();
String rule = "{\n" +
" \"properties\": {\n" +
" \"ODD_EVEN\": [ {\"anything-but\":\"E\"} ],\n" +
" \"ST_TYPE\": [ {\"anything-but\":\"ST\"} ]\n" +
" },\n" +
" \"geometry\": {\n" +
" \"coordinates\": {\n" +
" \"z\": [ {\"anything-but\": 1} ] \n" +
" }\n" +
" }\n" +
"}";
m.addRule("r", rule);
int count = 0;
int matched = 0;
long before = System.currentTimeMillis();
for (String lot : citylots2) {
count++;
if ((count % 10000) == 0) {
System.out.println("Lots: " + count);
}
if (m.rulesForJSONEvent(lot).size() != 0) {
matched++;
}
}
assertEquals(52527, matched); // from grep
System.out.println("Matched: " + matched);
long after = System.currentTimeMillis();
System.out.println("Lines: " + citylots2.size() +
", Msec: " + (after - before));
double eps = (1000.0 / (1.0 * (after - before) / citylots2.size()));
System.out.println("Events/sec: " + String.format("%.1f", eps));
}
@Test
public void CL2NoCompileBenchmark() throws Exception {
readCityLots2();
final Machine staticMachine = new Machine();
Map<String, Integer> expected = setRules(staticMachine);
int count = 0;
Map<String, AtomicInteger> received = new HashMap<>();
Set<String> rules = expected.keySet();
System.out.println("Finding Rules...");
long before = System.currentTimeMillis();
for (String lot : citylots2) {
for (String rule : rules) {
if (Ruler.matchesRule(lot, ruleJSON.get(rule))) {
incrRuleCount(rule, received);
}
}
count++;
if ((count % 10000) == 0) {
System.out.println("Lots: " + count);
}
}
long after = System.currentTimeMillis();
System.out.println("Lines: " + citylots2.size() +
", Msec: " + (after - before));
double eps = (1000.0 / (1.0 * (after - before) / citylots2.size()));
System.out.println("Events/sec: " + String.format("%.1f", eps));
System.out.println(" Rules/sec: " + String.format("%.1f", eps * rules.size()));
for (String rule : rules) {
assertEquals(rule, (long) expected.get(rule), received.get(rule).get());
}
}
@Test
public void CL2Benchmark() throws Exception {
readCityLots2();
Benchmarker bm;
// initial run to stabilize memory
bm = new Benchmarker();
bm.addRules(EXACT_RULES, EXACT_MATCHES);
bm.run(citylots2);
bm = new Benchmarker();
bm.addRules(EXACT_RULES, EXACT_MATCHES);
bm.run(citylots2);
System.out.println("EXACT events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(WILDCARD_RULES, WILDCARD_MATCHES);
bm.run(citylots2);
System.out.println("WILDCARD events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(PREFIX_RULES, PREFIX_MATCHES);
bm.run(citylots2);
System.out.println("PREFIX events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(PREFIX_EQUALS_IGNORE_CASE_RULES, PREFIX_EQUALS_IGNORE_CASE_MATCHES);
bm.run(citylots2);
System.out.println("PREFIX_EQUALS_IGNORE_CASE_RULES events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(SUFFIX_RULES, SUFFIX_MATCHES);
bm.run(citylots2);
System.out.println("SUFFIX events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(SUFFIX_EQUALS_IGNORE_CASE_RULES, SUFFIX_EQUALS_IGNORE_CASE_MATCHES);
bm.run(citylots2);
System.out.println("SUFFIX_EQUALS_IGNORE_CASE_RULES events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(EQUALS_IGNORE_CASE_RULES, EQUALS_IGNORE_CASE_MATCHES);
bm.run(citylots2);
System.out.println("EQUALS_IGNORE_CASE events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(NUMERIC_RULES, NUMERIC_MATCHES);
bm.run(citylots2);
System.out.println("NUMERIC events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(ANYTHING_BUT_RULES, ANYTHING_BUT_MATCHES);
bm.run(citylots2);
System.out.println("ANYTHING-BUT events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(ANYTHING_BUT_IGNORE_CASE_RULES, ANYTHING_BUT_IGNORE_CASE_MATCHES);
bm.run(citylots2);
System.out.println("ANYTHING-BUT-IGNORE-CASE events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(ANYTHING_BUT_PREFIX_RULES, ANYTHING_BUT_PREFIX_MATCHES);
bm.run(citylots2);
System.out.println("ANYTHING-BUT-PREFIX events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(ANYTHING_BUT_SUFFIX_RULES, ANYTHING_BUT_SUFFIX_MATCHES);
bm.run(citylots2);
System.out.println("ANYTHING-BUT-SUFFIX events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(ANYTHING_BUT_WILDCARD_RULES, ANYTHING_BUT_WILDCARD_MATCHES);
bm.run(citylots2);
System.out.println("ANYTHING-BUT-WILDCARD events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(COMPLEX_ARRAYS_RULES, COMPLEX_ARRAYS_MATCHES);
bm.run(citylots2);
System.out.println("COMPLEX_ARRAYS events/sec: " + String.format("%.1f", bm.getEPS()));
// skips complex arrays matchers because their slowness can hide improvements
// and regressions for other matchers. Remove this once we find ways to make
// arrays fast enough to others matchers
bm = new Benchmarker();
bm.addRules(NUMERIC_RULES, NUMERIC_MATCHES);
bm.addRules(EXACT_RULES, EXACT_MATCHES);
bm.addRules(PREFIX_RULES, PREFIX_MATCHES);
bm.addRules(ANYTHING_BUT_RULES, ANYTHING_BUT_MATCHES);
bm.addRules(ANYTHING_BUT_IGNORE_CASE_RULES, ANYTHING_BUT_IGNORE_CASE_MATCHES);
bm.addRules(ANYTHING_BUT_PREFIX_RULES, ANYTHING_BUT_PREFIX_MATCHES);
bm.addRules(ANYTHING_BUT_SUFFIX_RULES, ANYTHING_BUT_SUFFIX_MATCHES);
bm.addRules(ANYTHING_BUT_WILDCARD_RULES, ANYTHING_BUT_WILDCARD_MATCHES);
bm.run(citylots2);
System.out.println("PARTIAL_COMBO events/sec: " + String.format("%.1f", bm.getEPS()));
bm = new Benchmarker();
bm.addRules(NUMERIC_RULES, NUMERIC_MATCHES);
bm.addRules(EXACT_RULES, EXACT_MATCHES);
bm.addRules(PREFIX_RULES, PREFIX_MATCHES);
bm.addRules(ANYTHING_BUT_RULES, ANYTHING_BUT_MATCHES);
bm.addRules(ANYTHING_BUT_IGNORE_CASE_RULES, ANYTHING_BUT_IGNORE_CASE_MATCHES);
bm.addRules(ANYTHING_BUT_PREFIX_RULES, ANYTHING_BUT_PREFIX_MATCHES);
bm.addRules(ANYTHING_BUT_SUFFIX_RULES, ANYTHING_BUT_SUFFIX_MATCHES);
bm.addRules(ANYTHING_BUT_WILDCARD_RULES, ANYTHING_BUT_WILDCARD_MATCHES);
bm.addRules(COMPLEX_ARRAYS_RULES, COMPLEX_ARRAYS_MATCHES);
bm.run(citylots2);
System.out.println("COMBO events/sec: " + String.format("%.1f", bm.getEPS()));
}
// make sure we can handle nasty deep events
@Test
public void DeepEventBenchmark() throws Exception {
// how many levels deep we want to go
int maxLevel = 100;
// we create a rule every time the number of events is a multiple of this number
int ruleEveryNEvents = 10;
ObjectMapper m = new ObjectMapper();
Map<String, Object> root = new HashMap<>();
Map<String, Object> ruleRoot = new HashMap<>();
List<String> deepEvents = new ArrayList<>();
List<String> deepRules = new ArrayList<>();
List<Integer> deepExpected = new ArrayList<>();
Map<String, Object> currentLevel = root;
Map<String, Object> currentRule = ruleRoot;
for (int i = 0; i < maxLevel; i++) {
currentLevel.put("numeric" + i, i * i);
currentLevel.put("string" + i, "value" + i);
if (i % ruleEveryNEvents == 0) {
currentRule.put("string" + i, Collections.singletonList("value" + i));
deepRules.add(m.writeValueAsString(ruleRoot));
currentRule.remove("string" + i);
// all the events generated below this point will match this rule.
deepExpected.add(maxLevel - i);
}
deepEvents.add(m.writeValueAsString(root));
HashMap<String, Object> newLevel = new HashMap<>();
currentLevel.put("level" + i, newLevel);
currentLevel = newLevel;
HashMap<String, Object> newRuleLevel = new HashMap<>();
currentRule.put("level" + i, newRuleLevel);
currentRule = newRuleLevel;
}
// warm up
Benchmarker bm = new Benchmarker();
bm.addRules(deepRules.toArray(new String[0]), deepExpected.stream().mapToInt(Integer::intValue).toArray());
bm.run(deepEvents);
// exercise
bm = new Benchmarker();
bm.addRules(deepRules.toArray(new String[0]), deepExpected.stream().mapToInt(Integer::intValue).toArray());
bm.run(deepEvents);
System.out.println("DEEP EXACT events/sec: " + String.format("%.1f", bm.getEPS()));
}
private final List<String> citylots2 = new ArrayList<>();
private static class Benchmarker {
int ruleCount = 0;
Map<String, Integer> wanted = new HashMap<>();
Machine machine = new Machine();
double eps = 0.0;
void addRules(String[] rules, int[] wanted) throws Exception {
for (int i = 0; i < rules.length; i++) {
String rname = String.format("r%d", ruleCount++);
machine.addRule(rname, rules[i]);
this.wanted.put(rname, wanted[i]);
}
}
void run(List<String> events) throws Exception {
final Map<String, Integer> gotMatches = new HashMap<>();
long before = System.currentTimeMillis();
for (String event : events) {
List<String> matches = machine.rulesForJSONEvent(event);
for (String match : matches) {
Integer got = gotMatches.get(match);
if (got == null) {
got = 1;
} else {
got = got + 1;
}
gotMatches.put(match, got);
}
}
long after = System.currentTimeMillis();
eps = (1000.0 / (1.0 * (after - before) / events.size()));
for (String got : gotMatches.keySet()) {
assertEquals(wanted.get(got), gotMatches.get(got));
}
for (String want : wanted.keySet()) {
assertEquals(wanted.get(want), gotMatches.get(want) == null ? (Integer) 0 : gotMatches.get(want));
}
}
double getEPS() {
return eps;
}
}
private final Random rand = new Random(234345);
private String randomAscii(final int len) {
final String abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
sb.append(abc.charAt(rand.nextInt(abc.length())));
}
return sb.toString();
}
private void readCityLots2() {
System.out.println("Reading citylots2");
readCityLots2(citylots2);
System.out.println("Read " + citylots2.size() + " events");
}
public static void readCityLots2(List<String> citylots2) {
try {
final FileInputStream fileInputStream = new FileInputStream(CITYLOTS_2);
final GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
BufferedReader cl2Reader = new BufferedReader(new InputStreamReader(gzipInputStream));
String line = cl2Reader.readLine();
while (line != null) {
citylots2.add(line);
line = cl2Reader.readLine();
}
cl2Reader.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/////////////////////////// Old benchmarks start here ////////////////////////
private BufferedReader cityLotsReader;
private final Machine machine = new Machine();
private final Map<String, String> ruleJSON = new HashMap<>();
@Test
public void RulebaseBenchmark() throws Exception {
File dir = new File("data");
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
String name = file.getName();
if (name.startsWith("rules")) {
BufferedReader reader = new BufferedReader(new FileReader(file));
List<String[]> lines = linesFrom(reader);
reader.close();
doRules(lines);
}
}
}
}
private void doRules(List<String[]> lines) {
Machine machine = new Machine();
String[] rnames = new String[lines.size()];
for (int i = 0; i < rnames.length; i++) {
rnames[i] = "R" + String.format("%03d", i);
}
List<Map<String, List<String>>> rules = new ArrayList<>();
int size = 0;
for (String[] line : lines) {
Map<String, List<String>> rule = new HashMap<>();
for (int i = 0; i < line.length; i += 2) {
List<String> l = new ArrayList<>();
l.add(line[i + 1]);
rule.put(line[i], l);
size += l.size();
}
rules.add(rule);
}
long before = System.currentTimeMillis();
for (int i = 0; i < rules.size(); i++) {