Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,39 @@
// THE SOFTWARE.

description = 'Interfaces and utilities to report metrics to M3'

sourceSets {
jmh {
java.srcDirs = ['src/jmh/java']
resources.srcDirs = ['src/jmh/resources']
compileClasspath += sourceSets.main.runtimeClasspath
compileClasspath += sourceSets.test.runtimeClasspath
}
}

dependencies {
jmhImplementation 'org.openjdk.jmh:jmh-core:1.23'
jmhImplementation 'org.openjdk.jmh:jmh-generator-annprocess:1.23'
}

task jmh(type: JavaExec, dependsOn: jmhClasses) {
main = 'org.openjdk.jmh.Main'
classpath = sourceSets.jmh.compileClasspath + sourceSets.jmh.runtimeClasspath
}

classes.finalizedBy(jmhClasses)

//jmh {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why comment this out? i'd prefer if you either add in or remove entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cleaned up in #74

// benchmarkMode = ['Throughput']
//
// warmupIterations = 5
//
// iterations = 5
// batchSize = 1
//
// fork = 1
// forceGC = true
// includeTests = true
//
// duplicateClassesStrategy = 'warn'
//}
119 changes: 119 additions & 0 deletions core/src/jmh/java/com/uber/m3/tally/ScopeImplBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.uber.m3.tally;

import com.uber.m3.util.Duration;
import com.uber.m3.util.ImmutableMap;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Fork(value = 2, jvmArgsAppend = { "-server", "-XX:+UseG1GC" })
public class ScopeImplBenchmark {

private static final DurationBuckets EXPONENTIAL_BUCKETS = DurationBuckets.linear(Duration.ofMillis(1), Duration.ofMillis(10), 128);

private static final String[] COUNTER_NAMES = {
"first-counter",
"second-counter",
"third-counter",
"fourth-counter",
"fifth-counter",
};

private static final String[] GAUGE_NAMES = {
"first-gauge",
"second-gauge",
"third-gauge",
"fourth-gauge",
"fifth-gauge",
};

private static final String[] HISTOGRAM_NAMES = {
"first-histogram",
"second-histogram",
"third-histogram",
"fourth-histogram",
"fifth-histogram",
};

@Benchmark
public void scopeReportingBenchmark(BenchmarkState state) {
state.scope.reportLoopIteration();
}

@State(org.openjdk.jmh.annotations.Scope.Benchmark)
public static class BenchmarkState {

private ScopeImpl scope;

@Setup
public void setup() {
this.scope =
(ScopeImpl) new RootScopeBuilder()
.reporter(new TestStatsReporter())
.tags(
ImmutableMap.of(
"service", "some-service",
"application", "some-application",
"instance", "some-instance"
)
)
.reportEvery(Duration.MAX_VALUE);

for (String counterName : COUNTER_NAMES) {
scope.counter(counterName).inc(1);
}

for (String gaugeName : GAUGE_NAMES) {
scope.gauge(gaugeName).update(0.);
}

for (String histogramName : HISTOGRAM_NAMES) {
Histogram h = scope.histogram(histogramName, EXPONENTIAL_BUCKETS);

Random r = new Random();

// Populate at least 20% of the buckets
int bucketsCount = EXPONENTIAL_BUCKETS.buckets.size();
for (int i = 0; i < bucketsCount / 5; ++i) {
h.recordDuration(EXPONENTIAL_BUCKETS.buckets.get(r.nextInt(bucketsCount)));
}
}
}

@TearDown
public void teardown() {
scope.close();
}

}
}
11 changes: 10 additions & 1 deletion core/src/main/java/com/uber/m3/tally/CounterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,24 @@
/**
* Default implementation of a {@link Counter}.
*/
class CounterImpl implements Counter {
class CounterImpl extends MetricBase implements Counter {
private AtomicLong prev = new AtomicLong(0);
private AtomicLong curr = new AtomicLong(0);

protected CounterImpl(String fqn) {
super(fqn);
}

@Override
public void inc(long delta) {
curr.getAndAdd(delta);
}

@Override
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't live in Java code so this more a curiosity: why override this method if you're going to only call the parent's method? Is it required by the Java type system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a refactoring aberration, cleaned up in #74

public String getQualifiedName() {
return super.getQualifiedName();
}

long value() {
long current = curr.get();
long previous = prev.get();
Expand Down
11 changes: 10 additions & 1 deletion core/src/main/java/com/uber/m3/tally/GaugeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,25 @@
/**
* Default implementation of a {@link Gauge}.
*/
class GaugeImpl implements Gauge {
class GaugeImpl extends MetricBase implements Gauge {
private AtomicBoolean updated = new AtomicBoolean(false);
private AtomicLong curr = new AtomicLong(0);

protected GaugeImpl(String fqn) {
super(fqn);
}

@Override
public void update(double value) {
curr.set(Double.doubleToLongBits(value));
updated.set(true);
}

@Override
public String getQualifiedName() {
return super.getQualifiedName();
}

double value() {
return Double.longBitsToDouble(curr.get());
}
Expand Down
53 changes: 20 additions & 33 deletions core/src/main/java/com/uber/m3/tally/HistogramImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@
/**
* Default implementation of a {@link Histogram}.
*/
class HistogramImpl implements Histogram, StopwatchRecorder {
class HistogramImpl extends MetricBase implements Histogram, StopwatchRecorder {
private Type type;
private String name;
private ImmutableMap<String, String> tags;
private Buckets specification;
private List<HistogramBucket> buckets;
private List<Double> lookupByValue;
private List<Duration> lookupByDuration;

HistogramImpl(
String name,
String fqn,
ImmutableMap<String, String> tags,
StatsReporter reporter,
Buckets buckets
) {
super(fqn);

if (buckets instanceof DurationBuckets) {
type = Type.DURATION;
} else {
Expand All @@ -56,12 +56,12 @@ class HistogramImpl implements Histogram, StopwatchRecorder {
BucketPair[] pairs = BucketPairImpl.bucketPairs(buckets);
int pairsLen = pairs.length;

this.name = name;
this.tags = tags;
specification = buckets;
this.specification = buckets;

this.buckets = new ArrayList<>(pairsLen);
lookupByValue = new ArrayList<>(pairsLen);
lookupByDuration = new ArrayList<>(pairsLen);
this.lookupByValue = new ArrayList<>(pairsLen);
this.lookupByDuration = new ArrayList<>(pairsLen);

for (BucketPair pair : pairs) {
addBucket(new HistogramBucket(
Expand All @@ -81,55 +81,42 @@ private void addBucket(HistogramBucket bucket) {

@Override
public void recordValue(double value) {
int index = Collections.binarySearch(lookupByValue, value);

if (index < 0) {
// binarySearch returns the index of the search key if it is contained in the list;
// otherwise, (-(insertion point) - 1).
index = -(index + 1);
}

// binarySearch can return collections.size(), guarding against that.
// pointing to last bucket is fine in that case because it's [_,infinity).
if (index >= buckets.size()) {
index = buckets.size() - 1;
}

int index = toBucketIndex(Collections.binarySearch(lookupByValue, value));
buckets.get(index).samples.inc(1);
}

@Override
public void recordDuration(Duration duration) {
int index = Collections.binarySearch(lookupByDuration, duration);
int index = toBucketIndex(Collections.binarySearch(lookupByDuration, duration));
buckets.get(index).samples.inc(1);
}

if (index < 0) {
private int toBucketIndex(int binarySearchResult) {
if (binarySearchResult < 0) {
// binarySearch returns the index of the search key if it is contained in the list;
// otherwise, (-(insertion point) - 1).
index = -(index + 1);
binarySearchResult = -(binarySearchResult + 1);
}

// binarySearch can return collections.size(), guarding against that.
// pointing to last bucket is fine in that case because it's [_,infinity).
if (index >= buckets.size()) {
index = buckets.size() - 1;
if (binarySearchResult >= buckets.size()) {
binarySearchResult = buckets.size() - 1;
}

buckets.get(index).samples.inc(1);
return binarySearchResult;
}

@Override
public Stopwatch start() {
return new Stopwatch(System.nanoTime(), this);
}

String getName() {
return name;
}

ImmutableMap<String, String> getTags() {
return tags;
}

@Override
void report(String name, ImmutableMap<String, String> tags, StatsReporter reporter) {
for (HistogramBucket bucket : buckets) {
long samples = bucket.samples.value();
Expand Down Expand Up @@ -199,7 +186,7 @@ class HistogramBucket {
Duration durationLowerBound,
Duration durationUpperBound
) {
samples = new CounterImpl();
this.samples = new CounterImpl(getQualifiedName());
this.valueLowerBound = valueLowerBound;
this.valueUpperBound = valueUpperBound;
this.durationLowerBound = durationLowerBound;
Expand Down
38 changes: 38 additions & 0 deletions core/src/main/java/com/uber/m3/tally/MetricBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.uber.m3.tally;

import com.uber.m3.util.ImmutableMap;

abstract class MetricBase {

private final String fullyQualifiedName;

protected MetricBase(String fqn) {
this.fullyQualifiedName = fqn;
}

String getQualifiedName() {
return fullyQualifiedName;
}

abstract void report(String name, ImmutableMap<String, String> tags, StatsReporter reporter);
}
Loading