package com.spakai.index;


import com.spakai.index.*;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class IndexBenchMarkTest {

    PrimaryHashIndex<String> hashIndex;
    PrimaryTreeIndex<String> treeIndex;
    ExecutorService pool;

    @Rule
    public ExpectedException thrown = ExpectedException.none();


    @Before
    public void setup() {
        pool = Executors.newFixedThreadPool(3);
        
        hashIndex = new PrimaryHashIndex<>(pool);
        for(Integer i =0 ; i < 1000001 ; i++) {
            hashIndex.load(i.toString(), i.toString());
        }
        
        treeIndex = new PrimaryTreeIndex<>(pool);
        for(Integer i = 0; i < 1000001 ; i++) {
            treeIndex.load(i.toString(), i.toString());
        }
    }

    @Test
    public void performanceTestExactSync() {
        Instant start = Instant.now();
        for(Integer i=0; i< 50001 ; i++) {
            hashIndex.syncExactMatch(i.toString());
        }
        
        Duration between = Duration.between(start, Instant.now());
        System.out.println("Time taken in ms:: " +  between.toMillis());
    }
    
    @Test
    public void performanceTestExactASync() {
        List<CompletableFuture<Set<String>>> hold = new ArrayList<>(50000);
        Instant start = Instant.now();
        for(Integer i=0; i< 50001 ; i++) {
            hold.add(hashIndex.asyncExactMatch(i.toString()));
        }
        
        Duration between = Duration.between(start, Instant.now());
        System.out.println("Time taken in ms:: " +  between.toMillis());
    }
    
      @Test
    public void performanceTestBestSync() {
        Instant start = Instant.now();
        for(Integer i=0; i< 50001 ; i++) {
            treeIndex.syncBestMatch(i.toString());
        }
        
        Duration between = Duration.between(start, Instant.now());
        System.out.println("Time taken in ms:: " +  between.toMillis());
    }
    
}


@@ -530,24 +530,49 @@
  treeIndex;%0A
+    TrieIndex trieIndex;%0A
     Executor
@@ -585,16 +585,16 @@
 e pool;%0A
-
 %0A    @Ru
@@ -1090,24 +1090,202 @@
 ;%0A        %7D%0A
+        %0A        trieIndex = new TrieIndex();%0A        for(Integer i = 0; i %3C 1000001 ; i++) %7B%0A            treeIndex.load(i.toString(), i.toString());%0A        %7D%0A        %0A        %0A
     %7D%0A%0A    @
@@ -2081,26 +2081,24 @@
 %0A    %7D%0A    %0A
-  
     @Test%0A  
@@ -2087,32 +2087,32 @@
 %0A    %0A    @Test%0A
+
     public void 
@@ -2213,32 +2213,32 @@
  50001 ; i++) %7B%0A
-
             tree
@@ -2442,11 +2442,379 @@
  %7D%0A    %0A
+    %0A    @Test%0A    public void performanceTestBestSyncUsingTrie() %7B%0A        Instant start = Instant.now();%0A        for(Integer i=0; i%3C 50001 ; i++) %7B%0A            trieIndex.bestSearch(i.toString());%0A        %7D%0A        %0A        Duration between = Duration.between(start, Instant.now());%0A        System.out.println(%22Time taken in ms:: %22 +  between.toMillis());%0A    %7D%0A    %0A
 %7D%0A%0A

Add benchmark test for Trie