11package org .reactivecommons .async .impl .utils .matcher ;
22
3- import org . databene .contiperf .PerfTest ;
4- import org . databene .contiperf .Required ;
5- import org . databene .contiperf .junit .ContiPerfRule ;
3+ import com . github . javatlacati .contiperf .PerfTest ;
4+ import com . github . javatlacati .contiperf .Required ;
5+ import com . github . javatlacati .contiperf .junit .ContiPerfRule ;
66import org .junit .Before ;
77import org .junit .Rule ;
88import org .junit .Test ;
9+ import org .junit .jupiter .api .BeforeAll ;
910import org .junit .runner .RunWith ;
1011import org .springframework .test .context .ContextConfiguration ;
1112import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
1213
1314import java .io .File ;
1415import java .io .IOException ;
16+ import java .net .URI ;
17+ import java .net .URISyntaxException ;
1518import java .nio .file .Files ;
1619import java .nio .file .Paths ;
17- import java .util .HashMap ;
18- import java .util .HashSet ;
19- import java .util .Map ;
20- import java .util .Set ;
20+ import java .util .*;
21+ import java .util .concurrent .atomic .AtomicBoolean ;
22+ import java .util .concurrent .atomic .AtomicReference ;
2123import java .util .stream .Collectors ;
2224
23- @ ContextConfiguration (classes = KeyMatcher .class )
24- @ RunWith (SpringJUnit4ClassRunner .class )
2525public class KeyMatcherPerformanceTest {
26-
2726 @ Rule
2827 public ContiPerfRule contiPerfRule = new ContiPerfRule ();
29- Map <String , String > candidates = new HashMap <>();
30-
28+ public static AtomicReference < Map <String , String >> candidates = new AtomicReference <>();
29+ public static AtomicBoolean initialized = new AtomicBoolean ( false );
3130 private KeyMatcher keyMatcher = new KeyMatcher ();
32-
31+ private String DATASET_FILENAME = "candidateNamesForMatching.txt" ;
3332
3433 @ Before
3534 public void init () {
36- ClassLoader classLoader = getClass ().getClassLoader ();
37- File file = new File (classLoader .getResource ("candidateNamesForMatching.txt" ).getFile ());
38- try {
39- Set <String > names = new HashSet <>(Files
40- .readAllLines (Paths .get (file .getAbsolutePath ())));
41- candidates = names .stream ()
42- .collect (Collectors .toMap (name -> name , name -> name ));
43- } catch (IOException e ) {
44- e .printStackTrace ();
35+ if (!initialized .get ()) {
36+ initialized .set (true );
37+ try {
38+ ClassLoader classLoader = KeyMatcherPerformanceTest .class .getClassLoader ();
39+ File file = new File (Objects .requireNonNull (classLoader .getResource (DATASET_FILENAME ))
40+ .getFile ());
41+ Set <String > names = new HashSet <>(Files
42+ .readAllLines (Paths .get (file .getAbsolutePath ())));
43+ candidates .set (names .stream ()
44+ .collect (Collectors .toMap (name -> name , name -> name )));
45+ } catch (IOException e ) {
46+ e .printStackTrace ();
47+ candidates .set (new HashMap <>());
48+ }
4549 }
4650 }
4751
@@ -50,14 +54,13 @@ public void init() {
5054 @ Required (average = 200 , max = 10000 , throughput = 200 )
5155 public void getFromMapTest () {
5256 String existentKey = "System.segment.3073.event.action" ;
53- candidates .get (existentKey );
57+ candidates .get (). get ( existentKey );
5458 }
5559
56-
5760 @ Test
5861 @ PerfTest (threads = 4 , duration = 4000 )
5962 @ Required (average = 200 , max = 10000 , throughput = 200 )
6063 public void matchNameBeforeTest () {
61- keyMatcher .match (candidates .keySet (), "System.segment.1.event.action" );
64+ keyMatcher .match (candidates .get (). keySet (), "System.segment.1.event.action" );
6265 }
6366}
0 commit comments