package org.mitallast.queue.raft.log;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mitallast.queue.common.BaseTest;
import org.mitallast.queue.common.unit.ByteSizeUnit;

public class SegmentIndexBenchmark extends BaseTest {

    private final static int max = 1000000;

    private SegmentIndex emptySegmentIndex;
    private SegmentIndex fullSegmentIndex;

    @Before
    public void setUp() throws Exception {
        emptySegmentIndex = new SegmentIndex(testFolder.newFile(), (int) ByteSizeUnit.MB.toBytes(1));
        fullSegmentIndex = new SegmentIndex(testFolder.newFile(), (int) ByteSizeUnit.MB.toBytes(1));

        for (int i = 0; i < max; i++) {
            fullSegmentIndex.index(i, i * 100l, 100);
        }
    }

    @After
    public void tearDown() throws Exception {
        if (emptySegmentIndex != null) {
            emptySegmentIndex.close();
            emptySegmentIndex = null;
        }
        if (fullSegmentIndex != null) {
            fullSegmentIndex.close();
            fullSegmentIndex = null;
        }
    }

    @Test
    public void benchIndex() throws Exception {
        for (int i = 0; i < max; i++) {
            emptySegmentIndex.index(i, i * 100, 100);
        }
    }

    @Test
    public void benchContains() throws Exception {
        for (int i = 0; i < max; i++) {
            fullSegmentIndex.contains(i);
        }
    }

    @Test
    public void benchPosition() throws Exception {
        for (int i = 0; i < max; i++) {
            fullSegmentIndex.position(i);
        }
    }

    @Test
    public void benchLength() throws Exception {
        for (int i = 0; i < max; i++) {
            fullSegmentIndex.length(i);
        }
    }
}

@@ -261,53 +261,8 @@
  %7B%0A%0A
-    private final static int max = 1000000;%0A%0A
     
@@ -345,16 +345,88 @@
 Index;%0A%0A
+    @Override%0A    protected int max() %7B%0A        return 10000000;%0A    %7D%0A%0A
     @Bef
@@ -566,24 +566,25 @@
 MB.toBytes(1
+0
 ));%0A        
@@ -672,16 +672,17 @@
 oBytes(1
+0
 ));%0A%0A   
@@ -701,32 +701,34 @@
 t i = 0; i %3C max
+()
 ; i++) %7B%0A       
@@ -1158,32 +1158,81 @@
 ows Exception %7B%0A
+        long start = System.currentTimeMillis();%0A
         for (int
@@ -1238,32 +1238,34 @@
 t i = 0; i %3C max
+()
 ; i++) %7B%0A       
@@ -1313,32 +1313,125 @@
 100);%0A        %7D%0A
+        long end = System.currentTimeMillis();%0A        printQps(%22index%22, max(), start, end);%0A
     %7D%0A%0A    @Test
@@ -1474,32 +1474,81 @@
 ows Exception %7B%0A
+        long start = System.currentTimeMillis();%0A
         for (int
@@ -1554,32 +1554,34 @@
 t i = 0; i %3C max
+()
 ; i++) %7B%0A       
@@ -1617,32 +1617,128 @@
 s(i);%0A        %7D%0A
+        long end = System.currentTimeMillis();%0A        printQps(%22contains%22, max(), start, end);%0A
     %7D%0A%0A    @Test
@@ -1781,32 +1781,81 @@
 ows Exception %7B%0A
+        long start = System.currentTimeMillis();%0A
         for (int
@@ -1861,32 +1861,34 @@
 t i = 0; i %3C max
+()
 ; i++) %7B%0A       
@@ -1924,32 +1924,128 @@
 n(i);%0A        %7D%0A
+        long end = System.currentTimeMillis();%0A        printQps(%22position%22, max(), start, end);%0A
     %7D%0A%0A    @Test
@@ -2086,32 +2086,81 @@
 ows Exception %7B%0A
+        long start = System.currentTimeMillis();%0A
         for (int
@@ -2174,16 +2174,18 @@
  i %3C max
+()
 ; i++) %7B
@@ -2231,20 +2231,114 @@
 ;%0A        %7D%0A
+        long end = System.currentTimeMillis();%0A        printQps(%22length%22, max(), start, end);%0A
     %7D%0A%7D%0A

add log to segment index benchmark