View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.omid.tso;
19  
20  import java.io.FileNotFoundException;
21  import java.io.PrintWriter;
22  import java.io.UnsupportedEncodingException;
23  import java.util.Random;
24  
25  public class CacheEvaluation {
26  
27      private final static int ENTRIES = 1000000;
28      private final static int WARMUP_ROUNDS = 2;
29      private final static int ROUNDS = 4;
30      private final static double HOT_PERC = 1;
31  
32      public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
33          int[] asoc = new int[]{8, 16, 32};
34          for (int anAsoc : asoc) {
35              PrintWriter writer = new PrintWriter(anAsoc + ".out", "UTF-8");
36              new CacheEvaluation().testEntriesAge(new LongCache(ENTRIES, anAsoc), writer);
37              writer.close();
38          }
39          PrintWriter writer = new PrintWriter("guava.out", "UTF-8");
40          writer.close();
41      }
42  
43      private void testEntriesAge(LongCache cache, PrintWriter writer) {
44          Random random = new Random();
45  
46          long seed = random.nextLong();
47  
48          writer.println("# Random seed: " + seed);
49          random.setSeed(seed);
50          int removals = 0;
51          double tempStdDev = 0;
52          double tempAvg = 0;
53  
54          int i = 0;
55          int largestDeletedTimestamp = 0;
56          long hotItem = random.nextLong();
57  
58          Runtime.getRuntime().gc();
59  
60          for (; i < ENTRIES * WARMUP_ROUNDS; ++i) {
61              long toInsert = random.nextInt(100) < HOT_PERC ? hotItem : random.nextLong();
62              long removed = cache.set(toInsert, i);
63              if (removed > largestDeletedTimestamp) {
64                  largestDeletedTimestamp = (int) removed;
65              }
66              if (removed > largestDeletedTimestamp) {
67                  largestDeletedTimestamp = (int) removed;
68              }
69              if (i % ENTRIES == 0) {
70                  int round = i / ENTRIES + 1;
71                  System.err.format("Warmup [%d/%d]%n", round, WARMUP_ROUNDS);
72              }
73          }
74  
75          long time = System.nanoTime();
76          for (; i < ENTRIES * (WARMUP_ROUNDS + ROUNDS); ++i) {
77              long toInsert = random.nextInt(100) < HOT_PERC ? hotItem : random.nextLong();
78              long removed = cache.set(toInsert, i);
79              if (removed > largestDeletedTimestamp) {
80                  largestDeletedTimestamp = (int) removed;
81              }
82              int gap = i - largestDeletedTimestamp;
83              removals++;
84              double oldAvg = tempAvg;
85              tempAvg += (gap - tempAvg) / removals;
86              tempStdDev += (gap - oldAvg) * (gap - tempAvg);
87              if (i % ENTRIES == 0) {
88                  int round = i / ENTRIES - WARMUP_ROUNDS + 1;
89                  System.err.format("Progress [%d/%d]%n", round, ROUNDS);
90              }
91          }
92          long elapsed = System.nanoTime() - time;
93          double elapsedSeconds = (elapsed / (double) 1000000000);
94          long totalOps = ENTRIES * ROUNDS;
95          writer.println("# Free mem before GC (MB) :" + (Runtime.getRuntime().freeMemory() / (double) (1024 * 1024)));
96          Runtime.getRuntime().gc();
97          writer.println("# Free mem (MB) :" + (Runtime.getRuntime().freeMemory() / (double) (1024 * 1024)));
98          writer.println("# Elapsed (s): " + elapsedSeconds);
99          writer.println("# Elapsed per 100 ops (ms): " + (elapsed / (double) totalOps / 100 / (double) 1000000));
100         writer.println("# Ops per s : " + (totalOps / elapsedSeconds));
101         writer.println("# Avg gap: " + (tempAvg));
102         writer.println("# Std dev gap: " + Math.sqrt((tempStdDev / ENTRIES)));
103     }
104 
105 }