package com.yahoo.omid.tso;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Random;

public class CacheEvaluation {

    final int entries = 1000000;
    Histogram hist = new Histogram(entries * 100);

    // @Test
    public void testEntriesAge(int asoc, PrintWriter writer) {

        LongCache cache = new LongCache(entries, asoc);
        Random random = new Random();

        long seed = random.nextLong();

        writer.println("# Random seed: " + seed);
        random.setSeed(seed);
        int removals = 0;
        long totalAge = 0;
        double tempStdDev = 0;
        double tempAvg = 0;

        int i = 0;
        int largestDeletedTimestamp = 0;
        for (; i < entries * 10; ++i) {
            long removed = cache.set(random.nextLong(), i);
            if (removed > largestDeletedTimestamp) {
                largestDeletedTimestamp = (int) removed;
            }
        }

        long time = System.nanoTime();
        for (; i < entries * 100; ++i) {
            long removed = cache.set(random.nextLong(), i);
            if (removed > largestDeletedTimestamp) {
                largestDeletedTimestamp = (int) removed;
            }
            int gap = i - largestDeletedTimestamp;
            removals++;
            totalAge += gap;
            double oldAvg = tempAvg;
            tempAvg += (gap - tempAvg) / removals;
            tempStdDev += (gap - oldAvg) * (gap - tempAvg);
            hist.add(gap);
        }
        long elapsed = System.nanoTime() - time;
        double elapsedSeconds =  (elapsed / (double) 1000000000);
        writer.println("# Elapsed (s): " + elapsedSeconds);
        writer.println("# Elapsed per 100 ops (ms): " + (elapsed / (double) entries / (double) 1000000));
        writer.println("# Ops per s : " + ((entries * 100) / elapsedSeconds));

        double avgAge = totalAge / (double) removals;
        // LOG.info("Avg gap: " + (avgAge ));
        writer.println("# Avg gap: " + (tempAvg));
        writer.println("# Std dev gap: " + Math.sqrt((tempStdDev / entries)));
        hist.print(writer);
        // System.out.println(hist.toString());
        // assertThat(avgAge, is(greaterThan(entries * .9 )));
    }

    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        int[] asoc = new int[] { 1, 2, 4, 8, 16, 32 };
        for (int i = 0; i < asoc.length; ++i) {
            PrintWriter writer = new PrintWriter(asoc[i] + ".txt", "UTF-8");
            new CacheEvaluation().testEntriesAge(asoc[i], writer);
            writer.close();
        }
    }
}

@@ -205,28 +205,110 @@
 nal 
-int entries = 100000
+static int ENTRIES = 1000000;%0A    final static int WARMUP_ROUNDS = 20;%0A    final static int ROUNDS = 2
 0;%0A 
@@ -341,23 +341,23 @@
 stogram(
-entries
+ENTRIES
  * 100);
@@ -362,21 +362,8 @@
 );%0A%0A
-    // @Test%0A
     
@@ -462,23 +462,23 @@
 ngCache(
-entries
+ENTRIES
 , asoc);
@@ -829,28 +829,39 @@
  (; i %3C 
-entries * 10
+ENTRIES * WARMUP_ROUNDS
 ; ++i) %7B
@@ -1037,32 +1037,204 @@
 ;%0A            %7D%0A
+            if (i %25 ENTRIES == 0) %7B%0A                int round = i / ENTRIES + 1;%0A                System.err.format(%22Warmup %5B%25d/%25d%5D%5Cn%22, round, WARMUP_ROUNDS);%0A            %7D%0A
         %7D%0A%0A     
@@ -1286,29 +1286,50 @@
  (; i %3C 
-entries * 100
+ENTRIES * (WARMUP_ROUNDS + ROUNDS)
 ; ++i) %7B
@@ -1792,16 +1792,199 @@
 d(gap);%0A
+            if (i %25 ENTRIES == 0) %7B%0A                int round = i / ENTRIES - WARMUP_ROUNDS + 1;%0A                System.err.format(%22Progress %5B%25d/%25d%5D%5Cn%22, round, ROUNDS);%0A            %7D%0A
         
@@ -2065,17 +2065,16 @@
 econds =
- 
  (elapse
@@ -2099,16 +2099,58 @@
 00000);%0A
+        long totalOps = ENTRIES * ROUNDS;%0A
         
@@ -2277,23 +2277,30 @@
 double) 
-entries
+totalOps / 100
  / (doub
@@ -2361,23 +2361,16 @@
  + (
-(entries * 100)
+totalOps
  / e
@@ -2605,23 +2605,23 @@
 tdDev / 
-entries
+ENTRIES
 )));%0A   
@@ -3035,10 +3035,10 @@
 + %22.
-tx
+ou
 t%22, 

Improve CacheEvaluation