@@ -41,6 +41,18 @@ public void setUp() {
4141 btree = new Btree (keyType , valueType , btreeFilePath , Globals .INVALID_PAGE_ID , bufferPool );
4242 }
4343
44+ public void setUp2 () {
45+ CleanUp ();
46+ // Initialize the DiskManager
47+ diskManager = new BasicDiskManager ();
48+ // Initialize the buffer pool with a size of 10 pages
49+ bufferPool = new BufferPool (MAX_PAGES , K , diskManager );
50+ keyType = new Template (Integer .class );
51+ valueType = new Template (Integer .class );
52+ // Initialize the B-tree
53+ btree = new Btree (keyType , valueType , btreeFilePath , Globals .INVALID_PAGE_ID , bufferPool );
54+ }
55+
4456 @ After
4557 public void CleanUp () {
4658 // monitor.interrupt();
@@ -60,6 +72,7 @@ private Compositekey makeCompositekey(int age, int salary, Template type) {
6072 return key ;
6173 }
6274
75+
6376 private Compositekey makeCompositekey (int val , Template type ) {
6477 Compositekey key = new Compositekey (type );
6578 key .set (0 , val , Integer .class );
@@ -554,4 +567,156 @@ public void testBtreeBenchmark3() throws Exception {
554567 Arrays .stream (overallThroughputs ).average ().getAsDouble ());
555568 System .out .println ("└───────────────────────────────────────────────────────────" );
556569 }
570+
571+
572+ @ Test
573+ public void testBtreeBenchmark4 () throws Exception {
574+ int keysnumber = 2_000_000 ;
575+ double [] totalExecutionTimes = new double [ITERATIONS ];
576+ double [] totalInsertTimes = new double [ITERATIONS ];
577+ double [] avgInsertTimes = new double [ITERATIONS ];
578+ double [] insertThroughputs = new double [ITERATIONS ];
579+ double [] totalSearchTimes = new double [ITERATIONS ];
580+ double [] avgSearchTimes = new double [ITERATIONS ];
581+ double [] searchThroughputs = new double [ITERATIONS ];
582+ double [] overallThroughputs = new double [ITERATIONS ];
583+
584+ for (int iteration = 0 ; iteration < ITERATIONS ; iteration ++) {
585+ setUp2 (); // Reset the environment for each iteration
586+ int writersCnt = 200 ;
587+ int readersCnt = 200 ;
588+ List <Thread > threads = new ArrayList <>();
589+ int op = 10000 ;
590+
591+
592+ double [] insertTimes = new double [2 * keysnumber ];
593+ double [] searchTimes = new double [keysnumber ];
594+
595+ threads = new ArrayList <>();
596+ double startTime = (double ) System .currentTimeMillis ();
597+ double startTimeW = (double ) System .currentTimeMillis ();
598+ // Insert operations
599+ for (int i = 0 ; i < writersCnt ; i ++) {
600+ final int end = op * (i + 1 );
601+ Thread writer =
602+ new Thread (
603+ () -> {
604+ for (int key = end - op ; key < end ; key ++) {
605+ try {
606+ long insertStartTime = System .currentTimeMillis ();
607+ btree .insert (
608+ makeCompositekey (key , keyType ),
609+ makeCompositekey (key , valueType ));
610+ long insertEndTime = System .currentTimeMillis ();
611+ insertTimes [key ] = (insertEndTime - insertStartTime );
612+ } catch (Exception e ) {
613+ e .printStackTrace ();
614+ fail ();
615+ }
616+ }
617+ });
618+ threads .add (writer );
619+ }
620+
621+ Collections .shuffle (threads );
622+ for (Thread thread : threads ) {
623+ thread .start ();
624+ }
625+
626+ for (Thread thread : threads ) {
627+ thread .join ();
628+ }
629+
630+ double insertTotalTime = System .currentTimeMillis () - startTimeW ;
631+ totalInsertTimes [iteration ] = insertTotalTime ;
632+ avgInsertTimes [iteration ] = Arrays .stream (insertTimes ).average ().getAsDouble ();
633+ insertThroughputs [iteration ] = 2 * keysnumber / insertTotalTime * 1000 ;
634+
635+ // Search operations
636+ threads = new ArrayList <>();
637+ double startTime2 = System .currentTimeMillis ();
638+
639+ for (int i = 0 ; i < readersCnt ; i ++) {
640+ final int end = op * (i + 1 );
641+ Thread reader =
642+ new Thread (
643+ () -> {
644+ for (int key = end - op ; key < end ; key ++) {
645+ try {
646+ long searchStartTime = System .currentTimeMillis ();
647+ Compositekey val =
648+ btree .get (makeCompositekey (key , keyType ));
649+ long searchEndTime = System .currentTimeMillis ();
650+ searchTimes [key ] = (searchEndTime - searchStartTime );
651+ // assertEquals(0, val.compareTo(makeCompositekey(key, valueType)));
652+ } catch (Exception e ) {
653+ System .out .println ("thread " + end / op + ": expected ->" + key );
654+ e .printStackTrace ();
655+ fail ();
656+ }
657+ }
658+ });
659+ threads .add (reader );
660+ }
661+
662+ Collections .shuffle (threads );
663+ for (Thread thread : threads ) {
664+ thread .start ();
665+ }
666+
667+ for (Thread thread : threads ) {
668+ thread .join ();
669+ }
670+
671+ double searchTotalTime = System .currentTimeMillis () - startTime2 ;
672+ totalSearchTimes [iteration ] = searchTotalTime ;
673+ avgSearchTimes [iteration ] = Arrays .stream (searchTimes ).average ().getAsDouble ();
674+ searchThroughputs [iteration ] = keysnumber / searchTotalTime * 1000 ;
675+
676+ double endTime = System .currentTimeMillis ();
677+ totalExecutionTimes [iteration ] = (endTime - startTime ) / 1000.0 ;
678+ overallThroughputs [iteration ] = (keysnumber * 3 ) / (insertTotalTime + searchTotalTime ) * 1000 ;
679+
680+ CleanUp (); // Clean up after each iteration
681+ System .out .println ("Iteration " + (iteration + 1 ) + " completed" );
682+ }
683+
684+ // Calculate and display averages
685+ System .out .println ("┌───────────────────────────────────────────────────────────" );
686+ System .out .printf (
687+ "\033 [1;36m B-TREE BENCHMARK RESULTS (%d ITERATIONS) \033 [0m\n " ,
688+ ITERATIONS );
689+ System .out .println ("┬───────────────────────────────────────────────────────────" );
690+ System .out .printf (
691+ " \033 [1mAverage total execution time:\033 [0m %8.2f seconds\n " ,
692+ Arrays .stream (totalExecutionTimes ).average ().getAsDouble ());
693+ System .out .println ("┼───────────────────────────────────────────────────────────" );
694+ System .out .printf (" \033 [1;32mInsert+Delete operations:\033 [0m\n " );
695+ System .out .printf (
696+ " - Average total time: %8.2f seconds\n " ,
697+ Arrays .stream (totalInsertTimes ).average ().getAsDouble () / 1000 );
698+ System .out .printf (
699+ " - Average operation time: %8.2f ms per operation\n " ,
700+ Arrays .stream (avgInsertTimes ).average ().getAsDouble ());
701+ System .out .printf (
702+ " - Average throughput: %8.2f operations per second\n " ,
703+ Arrays .stream (insertThroughputs ).average ().getAsDouble ());
704+ System .out .println ("┼───────────────────────────────────────────────────────────" );
705+ System .out .printf (" \033 [1;33mSearch operations:\033 [0m\n " );
706+ System .out .printf (
707+ " - Average total time: %8.2f seconds\n " ,
708+ Arrays .stream (totalSearchTimes ).average ().getAsDouble () / 1000 );
709+ System .out .printf (
710+ " - Average operation time: %8.2f ms per operation\n " ,
711+ Arrays .stream (avgSearchTimes ).average ().getAsDouble ());
712+ System .out .printf (
713+ " - Average throughput: %8.2f operations per second\n " ,
714+ Arrays .stream (searchThroughputs ).average ().getAsDouble ());
715+ System .out .println ("┼───────────────────────────────────────────────────────────" );
716+ System .out .printf (
717+ " \033 [1;35mAverage overall throughput:\033 [0m %8.2f operations per second\n " ,
718+ Arrays .stream (overallThroughputs ).average ().getAsDouble ());
719+ System .out .println ("└───────────────────────────────────────────────────────────" );
720+ }
721+
557722}
0 commit comments