• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.caliper.runner;
16 
17 import static org.junit.Assert.assertEquals;
18 
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Benchmark;
21 import com.google.caliper.config.VmConfig;
22 import com.google.caliper.model.Measurement;
23 import com.google.caliper.model.Trial;
24 import com.google.caliper.platform.Platform;
25 import com.google.caliper.platform.jvm.JvmPlatform;
26 import com.google.common.collect.ImmutableListMultimap;
27 import com.google.common.collect.ImmutableMap;
28 import com.google.common.collect.ImmutableSet;
29 import com.google.common.collect.Iterables;
30 import com.google.common.collect.Lists;
31 
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.JUnit4;
36 
37 import java.io.File;
38 import java.util.List;
39 
40 /**
41  * Tests {@link AllocationInstrument}.
42  */
43 
44 @RunWith(JUnit4.class)
45 public class AllocationInstrumentTest {
46 
47   @Rule public CaliperTestWatcher runner = new CaliperTestWatcher();
48 
getExtraCommandLineArgs()49   @Test public void getExtraCommandLineArgs() throws Exception {
50     AllocationInstrument instrument = new AllocationInstrument();
51     File fakeJar = File.createTempFile("fake", "jar");
52     fakeJar.deleteOnExit();
53     instrument.setOptions(ImmutableMap.of("allocationAgentJar", fakeJar.getAbsolutePath()));
54     ImmutableSet<String> expected = new ImmutableSet.Builder<String>()
55         .addAll(JvmPlatform.INSTRUMENT_JVM_ARGS)
56         .add("-Xint")
57         .add("-javaagent:" + fakeJar.getAbsolutePath())
58         .add("-Xbootclasspath/a:" + fakeJar.getAbsolutePath())
59         .add("-Dsun.reflect.inflationThreshold=0")
60         .build();
61     Platform platform = new JvmPlatform();
62     VmConfig vmConfig = new VmConfig.Builder(platform, new File(System.getProperty("java.home")))
63         .build();
64     assertEquals(expected, instrument.getExtraCommandLineArgs(vmConfig));
65     fakeJar.delete();
66   }
67 
68   @Test
intrinsics()69   public void intrinsics() throws Exception {
70     runner.forBenchmark(ArrayListGrowthBenchmark.class)
71         .instrument("allocation")
72         .run();
73     Trial trial = Iterables.getOnlyElement(runner.trials());
74     ImmutableListMultimap<String, Measurement> measurementsByDescription =
75         Measurement.indexByDescription(trial.measurements());
76     // 14 objects and 1960 bytes are the known values for growing an ArrayList from 1 element to 100
77     // elements
78     for (Measurement objectMeasurement : measurementsByDescription.get("objects")) {
79       assertEquals(14.0, objectMeasurement.value().magnitude() / objectMeasurement.weight(), 0.001);
80     }
81     for (Measurement byteMeasurement : measurementsByDescription.get("bytes")) {
82       assertEquals(1960.0, byteMeasurement.value().magnitude() / byteMeasurement.weight(), 0.001);
83     }
84   }
85 
86   public static class TestBenchmark {
87     List<Object> list = Lists.newLinkedList();
compressionSize(int reps)88     @Benchmark public int compressionSize(int reps) {
89       for (int i = 0; i < reps; i++) {
90         list.add(new Object());
91       }
92       int hashCode = list.hashCode();
93       list.clear();
94       return hashCode;
95     }
96   }
97 
98   public static class ArrayListGrowthBenchmark {
warmUp()99     @BeforeExperiment void warmUp() {
100       // ensure that hotspot has compiled this code
101       benchmarkGrowth(100000);
102     }
103 
benchmarkGrowth(int reps)104     @Benchmark void benchmarkGrowth(int reps) {
105       for (int i = 0; i < reps; i++) {
106         List<String> list = Lists.newArrayListWithCapacity(1);
107         for (int j = 0; j < 100; j++) {
108           list.add("");
109         }
110       }
111     }
112   }
113 }
114