aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/benchmark/com/google/common/collect/MapMakerSingleThreadBenchmark.java
blob: b6c3e30c3c478388836910f108320ab7d4c1e25b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) 2010 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect;

import com.google.caliper.Param;
import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;
import com.google.common.base.Function;
import com.google.common.collect.MapMaker;
import com.google.common.primitives.Ints;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Simple single-threaded benchmark for a computing map with maximum size.
 *
 * @author Charles Fry
 */
public class MapMakerSingleThreadBenchmark extends SimpleBenchmark {
  @Param({"1000", "2000"}) int maximumSize;
  @Param("5000") int distinctKeys;
  @Param("4") int segments;

  // 1 means uniform likelihood of keys; higher means some keys are more popular
  // tweak this to control hit rate
  @Param("2.5") double concentration;

  Random random = new Random();

  Map<Integer, Integer> cache;

  int max;

  static AtomicLong requests = new AtomicLong(0);
  static AtomicLong misses = new AtomicLong(0);

  @Override protected void setUp() {
    // random integers will be generated in this range, then raised to the
    // power of (1/concentration) and floor()ed
    max = Ints.checkedCast((long) Math.pow(distinctKeys, concentration));

    cache = new MapMaker()
        .concurrencyLevel(segments)
        .maximumSize(maximumSize)
        .makeComputingMap(
            new Function<Integer, Integer>() {
              @Override public Integer apply(Integer from) {
                return (int) misses.incrementAndGet();
              }
            });

    // To start, fill up the cache.
    // Each miss both increments the counter and causes the map to grow by one,
    // so until evictions begin, the size of the map is the greatest return
    // value seen so far
    while (cache.get(nextRandomKey()) < maximumSize) {}

    requests.set(0);
    misses.set(0);
  }

  public int time(int reps) {
    int dummy = 0;
    for (int i = 0; i < reps; i++) {
      dummy += cache.get(nextRandomKey());
    }
    requests.addAndGet(reps);
    return dummy;
  }

  private int nextRandomKey() {
    int a = random.nextInt(max);

    /*
     * For example, if concentration=2.0, the following takes the square root of
     * the uniformly-distributed random integer, then truncates any fractional
     * part, so higher integers would appear (in this case linearly) more often
     * than lower ones.
     */
    return (int) Math.pow(a, 1.0 / concentration);
  }

  @Override protected void tearDown() {
    double req = requests.get();
    double hit = req - misses.get();

    // Currently, this is going into /dev/null, but I'll fix that
    System.out.println("hit rate: " + hit / req);
  }

  public static void main(String[] args) {
    Runner.main(MapMakerSingleThreadBenchmark.class, args);
  }

  // for proper distributions later:
  // import JSci.maths.statistics.ProbabilityDistribution;
  // int key = (int) dist.inverse(random.nextDouble());
}