Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit e6c6aee

Browse files
authored
Adds Stats microbenchmarks. (#1834)
1 parent b494b40 commit e6c6aee

8 files changed

Lines changed: 1015 additions & 0 deletions
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.benchmarks.stats;
18+
19+
import io.opencensus.benchmarks.tags.TagsBenchmarksUtil;
20+
import io.opencensus.stats.MeasureMap;
21+
import io.opencensus.stats.StatsRecorder;
22+
import io.opencensus.stats.ViewManager;
23+
import io.opencensus.tags.TagContext;
24+
import io.opencensus.tags.Tagger;
25+
import java.util.concurrent.TimeUnit;
26+
import org.openjdk.jmh.annotations.Benchmark;
27+
import org.openjdk.jmh.annotations.BenchmarkMode;
28+
import org.openjdk.jmh.annotations.Mode;
29+
import org.openjdk.jmh.annotations.OutputTimeUnit;
30+
import org.openjdk.jmh.annotations.Param;
31+
import org.openjdk.jmh.annotations.Setup;
32+
import org.openjdk.jmh.annotations.State;
33+
34+
/** Benchmarks for {@link io.opencensus.stats.StatsRecorder}. */
35+
public class RecordBatchedBenchmark {
36+
@State(org.openjdk.jmh.annotations.Scope.Benchmark)
37+
public static class Data {
38+
@Param({"0", "1", "2", "3", "6", "8"})
39+
int numValues;
40+
41+
@Param({"impl", "impl-lite"})
42+
String implementation;
43+
44+
private StatsRecorder recorder;
45+
private Tagger tagger;
46+
private TagContext tags;
47+
48+
@Setup
49+
public void setup() throws Exception {
50+
ViewManager manager = StatsBenchmarksUtil.getViewManager(implementation);
51+
recorder = StatsBenchmarksUtil.getStatsRecorder(implementation);
52+
tagger = TagsBenchmarksUtil.getTagger(implementation);
53+
tags = TagsBenchmarksUtil.createTagContext(tagger.emptyBuilder(), 1);
54+
for (int i = 0; i < numValues; i++) {
55+
manager.registerView(StatsBenchmarksUtil.DOUBLE_COUNT_VIEWS[i]);
56+
manager.registerView(StatsBenchmarksUtil.LONG_COUNT_VIEWS[i]);
57+
manager.registerView(StatsBenchmarksUtil.DOUBLE_SUM_VIEWS[i]);
58+
manager.registerView(StatsBenchmarksUtil.LONG_SUM_VIEWS[i]);
59+
manager.registerView(StatsBenchmarksUtil.DOUBLE_DISTRIBUTION_VIEWS[i]);
60+
manager.registerView(StatsBenchmarksUtil.LONG_DISTRIBUTION_VIEWS[i]);
61+
manager.registerView(StatsBenchmarksUtil.DOUBLE_LASTVALUE_VIEWS[i]);
62+
manager.registerView(StatsBenchmarksUtil.LONG_LASTVALUE_VIEWS[i]);
63+
}
64+
}
65+
}
66+
67+
/** Record batched double count measures. */
68+
@Benchmark
69+
@BenchmarkMode(Mode.AverageTime)
70+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
71+
public MeasureMap recordBatchedDoubleCount(Data data) {
72+
MeasureMap map = data.recorder.newMeasureMap();
73+
for (int i = 0; i < data.numValues; i++) {
74+
map.put(StatsBenchmarksUtil.DOUBLE_COUNT_MEASURES[i], (double) i);
75+
}
76+
map.record(data.tags);
77+
return map;
78+
}
79+
80+
/** Record batched long count measures. */
81+
@Benchmark
82+
@BenchmarkMode(Mode.AverageTime)
83+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
84+
public MeasureMap recordBatchedLongCount(Data data) {
85+
MeasureMap map = data.recorder.newMeasureMap();
86+
for (int i = 0; i < data.numValues; i++) {
87+
map.put(StatsBenchmarksUtil.LONG_COUNT_MEASURES[i], i);
88+
}
89+
map.record(data.tags);
90+
return map;
91+
}
92+
93+
/** Record batched double sum measures. */
94+
@Benchmark
95+
@BenchmarkMode(Mode.AverageTime)
96+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
97+
public MeasureMap recordBatchedDoubleSum(Data data) {
98+
MeasureMap map = data.recorder.newMeasureMap();
99+
for (int i = 0; i < data.numValues; i++) {
100+
map.put(StatsBenchmarksUtil.DOUBLE_SUM_MEASURES[i], (double) i);
101+
}
102+
map.record(data.tags);
103+
return map;
104+
}
105+
106+
/** Record batched long sum measures. */
107+
@Benchmark
108+
@BenchmarkMode(Mode.AverageTime)
109+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
110+
public MeasureMap recordBatchedLongSum(Data data) {
111+
MeasureMap map = data.recorder.newMeasureMap();
112+
for (int i = 0; i < data.numValues; i++) {
113+
map.put(StatsBenchmarksUtil.LONG_SUM_MEASURES[i], i);
114+
}
115+
map.record(data.tags);
116+
return map;
117+
}
118+
119+
/** Record batched double distribution measures. */
120+
@Benchmark
121+
@BenchmarkMode(Mode.AverageTime)
122+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
123+
public MeasureMap recordBatchedDoubleDistribution(Data data) {
124+
MeasureMap map = data.recorder.newMeasureMap();
125+
for (int i = 0; i < data.numValues; i++) {
126+
map.put(StatsBenchmarksUtil.DOUBLE_DISTRIBUTION_MEASURES[i], (double) i);
127+
}
128+
map.record(data.tags);
129+
return map;
130+
}
131+
132+
/** Record batched ling distribution measures. */
133+
@Benchmark
134+
@BenchmarkMode(Mode.AverageTime)
135+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
136+
public MeasureMap recordBatchedLongDistribution(Data data) {
137+
MeasureMap map = data.recorder.newMeasureMap();
138+
for (int i = 0; i < data.numValues; i++) {
139+
map.put(StatsBenchmarksUtil.DOUBLE_DISTRIBUTION_MEASURES[i], i);
140+
}
141+
map.record(data.tags);
142+
return map;
143+
}
144+
145+
/** Record batched double last value measures. */
146+
@Benchmark
147+
@BenchmarkMode(Mode.AverageTime)
148+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
149+
public MeasureMap recordBatchedDoubleLastValue(Data data) {
150+
MeasureMap map = data.recorder.newMeasureMap();
151+
for (int i = 0; i < data.numValues; i++) {
152+
map.put(StatsBenchmarksUtil.DOUBLE_LASTVALUE_MEASURES[i], (double) i);
153+
}
154+
map.record(data.tags);
155+
return map;
156+
}
157+
158+
/** Record batched long last value measures. */
159+
@Benchmark
160+
@BenchmarkMode(Mode.AverageTime)
161+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
162+
public MeasureMap recordBatchedLongLastValue(Data data) {
163+
MeasureMap map = data.recorder.newMeasureMap();
164+
for (int i = 0; i < data.numValues; i++) {
165+
map.put(StatsBenchmarksUtil.LONG_LASTVALUE_MEASURES[i], i);
166+
}
167+
map.record(data.tags);
168+
return map;
169+
}
170+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.benchmarks.stats;
18+
19+
import io.opencensus.benchmarks.tags.TagsBenchmarksUtil;
20+
import io.opencensus.stats.Measure;
21+
import io.opencensus.stats.MeasureMap;
22+
import io.opencensus.stats.StatsRecorder;
23+
import io.opencensus.stats.ViewManager;
24+
import io.opencensus.tags.TagContext;
25+
import io.opencensus.tags.Tagger;
26+
import java.util.Arrays;
27+
import java.util.List;
28+
import java.util.concurrent.TimeUnit;
29+
import org.openjdk.jmh.annotations.Benchmark;
30+
import org.openjdk.jmh.annotations.BenchmarkMode;
31+
import org.openjdk.jmh.annotations.Mode;
32+
import org.openjdk.jmh.annotations.OutputTimeUnit;
33+
import org.openjdk.jmh.annotations.Param;
34+
import org.openjdk.jmh.annotations.Setup;
35+
import org.openjdk.jmh.annotations.State;
36+
37+
/** Benchmarks for {@link io.opencensus.trace.Tagger}. */
38+
public class RecordDifferentTagValuesBenchmark {
39+
@State(org.openjdk.jmh.annotations.Scope.Benchmark)
40+
public static class Data {
41+
@Param({"0", "1", "2", "3", "6", "8"})
42+
int numTags;
43+
44+
@Param({"impl", "impl-lite"})
45+
String implementation;
46+
47+
private StatsRecorder recorder;
48+
private ViewManager manager;
49+
private Tagger tagger;
50+
private List<TagContext> contexts;
51+
52+
@Setup
53+
public void setup() throws Exception {
54+
manager = StatsBenchmarksUtil.getViewManager(implementation);
55+
recorder = StatsBenchmarksUtil.getStatsRecorder(implementation);
56+
tagger = TagsBenchmarksUtil.getTagger(implementation);
57+
contexts = createContexts(numTags);
58+
manager.registerView(StatsBenchmarksUtil.DOUBLE_COUNT_VIEWS[0]);
59+
manager.registerView(StatsBenchmarksUtil.LONG_COUNT_VIEWS[0]);
60+
manager.registerView(StatsBenchmarksUtil.DOUBLE_SUM_VIEWS[0]);
61+
manager.registerView(StatsBenchmarksUtil.LONG_SUM_VIEWS[0]);
62+
manager.registerView(StatsBenchmarksUtil.DOUBLE_DISTRIBUTION_VIEWS[0]);
63+
manager.registerView(StatsBenchmarksUtil.LONG_DISTRIBUTION_VIEWS[0]);
64+
manager.registerView(StatsBenchmarksUtil.DOUBLE_LASTVALUE_VIEWS[0]);
65+
manager.registerView(StatsBenchmarksUtil.LONG_LASTVALUE_VIEWS[0]);
66+
}
67+
68+
// creates 'size' tag contexts mapping "key0" -> "valueN"
69+
private List<TagContext> createContexts(int size) {
70+
TagContext[] contexts = new TagContext[size];
71+
for (int i = 0; i < size; i++) {
72+
contexts[i] =
73+
tagger
74+
.emptyBuilder()
75+
.put(
76+
TagsBenchmarksUtil.TAG_KEYS.get(0),
77+
TagsBenchmarksUtil.TAG_VALUES.get(i),
78+
TagsBenchmarksUtil.UNLIMITED_PROPAGATION)
79+
.build();
80+
}
81+
return Arrays.asList(contexts);
82+
}
83+
}
84+
85+
@Benchmark
86+
@BenchmarkMode(Mode.AverageTime)
87+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
88+
public MeasureMap recordDoubleCount(Data data) {
89+
return record(data, StatsBenchmarksUtil.DOUBLE_COUNT_MEASURES[0], (double) 11);
90+
}
91+
92+
@Benchmark
93+
@BenchmarkMode(Mode.AverageTime)
94+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
95+
public MeasureMap recordLongCount(Data data) {
96+
return record(data, StatsBenchmarksUtil.LONG_COUNT_MEASURES[0], 11);
97+
}
98+
99+
@Benchmark
100+
@BenchmarkMode(Mode.AverageTime)
101+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
102+
public MeasureMap recordDoubleSum(Data data) {
103+
return record(data, StatsBenchmarksUtil.DOUBLE_SUM_MEASURES[0], (double) 11);
104+
}
105+
106+
@Benchmark
107+
@BenchmarkMode(Mode.AverageTime)
108+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
109+
public MeasureMap recordLongSum(Data data) {
110+
return record(data, StatsBenchmarksUtil.LONG_SUM_MEASURES[0], 11);
111+
}
112+
113+
@Benchmark
114+
@BenchmarkMode(Mode.AverageTime)
115+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
116+
public MeasureMap recordDoubleDistribution(Data data) {
117+
return record(data, StatsBenchmarksUtil.DOUBLE_DISTRIBUTION_MEASURES[0], (double) 11);
118+
}
119+
120+
@Benchmark
121+
@BenchmarkMode(Mode.AverageTime)
122+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
123+
public MeasureMap recordLongDistribution(Data data) {
124+
return record(data, StatsBenchmarksUtil.DOUBLE_DISTRIBUTION_MEASURES[0], 11);
125+
}
126+
127+
@Benchmark
128+
@BenchmarkMode(Mode.AverageTime)
129+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
130+
public MeasureMap recordDoubleLastValue(Data data) {
131+
return record(data, StatsBenchmarksUtil.DOUBLE_LASTVALUE_MEASURES[0], (double) 11);
132+
}
133+
134+
@Benchmark
135+
@BenchmarkMode(Mode.AverageTime)
136+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
137+
public MeasureMap recordLongLastValue(Data data) {
138+
return record(data, StatsBenchmarksUtil.LONG_LASTVALUE_MEASURES[0], 11);
139+
}
140+
141+
private static MeasureMap record(Data data, Measure.MeasureLong measure, int value) {
142+
MeasureMap map = data.recorder.newMeasureMap();
143+
map.put(measure, value);
144+
for (TagContext tags : data.contexts) {
145+
map.record(tags);
146+
}
147+
return map;
148+
}
149+
150+
private static MeasureMap record(Data data, Measure.MeasureDouble measure, double value) {
151+
MeasureMap map = data.recorder.newMeasureMap();
152+
map.put(measure, value);
153+
for (TagContext tags : data.contexts) {
154+
map.record(tags);
155+
}
156+
return map;
157+
}
158+
}

0 commit comments

Comments
 (0)