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 org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.testng.annotations.Test;
23  
24  import java.util.Random;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  import static org.testng.Assert.assertEquals;
29  import static org.testng.Assert.assertTrue;
30  
31  public class TestLongCache {
32  
33      private static final Logger LOG = LoggerFactory.getLogger(TestLongCache.class);
34  
35      private static final long TEST_VALUE = 1000;
36  
37      private Random random = new Random(System.currentTimeMillis());
38  
39      @Test(timeOut = 10_000)
40      public void testAddAndGetElems() {
41  
42          // Cache configuration
43          final int CACHE_SIZE = 10_000_000;
44          final int CACHE_ASSOCIATIVITY = 32;
45          LongCache cache = new LongCache(CACHE_SIZE, CACHE_ASSOCIATIVITY);
46  
47          // After creation, cache values should be the default
48          for (int i = 0; i < 1000; i++) {
49              long position = random.nextLong();
50              assertEquals(cache.get(position), 0L);
51          }
52  
53          Set<Long> testedKeys = new TreeSet<>();
54          // Populate some of the values
55          for (int i = 0; i < 1000; i++) {
56              long position = random.nextLong();
57              cache.set(position, TEST_VALUE);
58              testedKeys.add(position);
59          }
60  
61          // Get the values and check them
62          for (long key : testedKeys) {
63              assertEquals(cache.get(key), TEST_VALUE);
64          }
65  
66      }
67  
68      @Test(timeOut = 10_000)
69      public void testEntriesAge() {
70  
71          final int entries = 1000;
72  
73          LongCache cache = new LongCache(entries, 16);
74  
75          int removals = 0;
76          long totalAge = 0;
77          double tempStdDev = 0;
78          double tempAvg = 0;
79  
80          int i = 0;
81          int largestDeletedTimestamp = 0;
82          for (; i < entries * 10; ++i) {
83              long removed = cache.set(random.nextLong(), i);
84              if (removed > largestDeletedTimestamp) {
85                  largestDeletedTimestamp = (int) removed;
86              }
87          }
88  
89          long time = System.nanoTime();
90          for (; i < entries * 100; ++i) {
91              long removed = cache.set(random.nextLong(), i);
92              if (removed > largestDeletedTimestamp) {
93                  largestDeletedTimestamp = (int) removed;
94              }
95              int gap = i - largestDeletedTimestamp;
96              removals++;
97              totalAge += gap;
98              double oldAvg = tempAvg;
99              tempAvg += (gap - tempAvg) / removals;
100             tempStdDev += (gap - oldAvg) * (gap - tempAvg);
101         }
102         long elapsed = System.nanoTime() - time;
103         LOG.info("Elapsed (ms): " + (elapsed / (double) 1000));
104 
105         double avgGap = totalAge / (double) removals;
106         LOG.info("Avg gap: " + (tempAvg));
107         LOG.info("Std dev gap: " + Math.sqrt((tempStdDev / entries)));
108         assertTrue(avgGap > entries * 0.6, "avgGap should be greater than entries * 0.6");
109 
110     }
111 }