|
| 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