Skip to content

Yet another labels-to-Child lookup optimization #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions benchmark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.3.2</version>
<version>1.22</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.3.2</version>
<version>1.22</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.prometheus.benchmark;

import io.prometheus.client.Counter;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 1500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 1500, timeUnit = TimeUnit.MILLISECONDS)
@Fork(2)
public class LabelsToChildLookupBenchmark {

private static final String LABEL1 = "label1", LABEL2 = "label2", LABEL3 = "label3";
private static final String LABEL4 = "label4", LABEL5 = "label5";

private Counter noLabelsCollector, oneLabelCollector, twoLabelsCollector, threeLabelsCollector;
private Counter fourLabelsCollector, fiveLabelsCollector;

@Setup
public void setup() {
Counter.Builder builder = new Counter.Builder().name("testCollector").help("testHelp");
noLabelsCollector = builder.create();
oneLabelCollector = builder.labelNames("name1").create();
twoLabelsCollector = builder.labelNames("name1", "name2").create();
threeLabelsCollector = builder.labelNames("name1", "name2", "name3").create();
fourLabelsCollector = builder.labelNames("name1", "name2", "name3", "name4").create();
fiveLabelsCollector = builder.labelNames("name1", "name2", "name3", "name4", "name5").create();
}

@Benchmark
public void baseline(LabelsToChildLookupBenchmark state) {
noLabelsCollector.inc();
}

@Benchmark
public void oneLabel(LabelsToChildLookupBenchmark state) {
oneLabelCollector.labels(LABEL1).inc();
}

@Benchmark
public void twoLabels(LabelsToChildLookupBenchmark state) {
twoLabelsCollector.labels(LABEL1, LABEL2).inc();
}

@Benchmark
public void threeLabels(LabelsToChildLookupBenchmark state) {
threeLabelsCollector.labels(LABEL1, LABEL2, LABEL3).inc();
}

@Benchmark
public void fourLabels(LabelsToChildLookupBenchmark state) {
fourLabelsCollector.labels(LABEL1, LABEL2, LABEL3, LABEL4).inc();
}

@Benchmark
public void fiveLabels(LabelsToChildLookupBenchmark state) {
fiveLabelsCollector.labels(LABEL1, LABEL2, LABEL3, LABEL4, LABEL5).inc();
}

public static void main(String[] args) throws RunnerException {
new Runner(new OptionsBuilder()
.include(LabelsToChildLookupBenchmark.class.getSimpleName())
.build()).run();
}
}
15 changes: 9 additions & 6 deletions simpleclient/src/main/java/io/prometheus/client/Counter.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ public static class Child {
* Increment the counter by 1.
*/
public void inc() {
inc(1);
inc(1.0);
}
/**
* Increment the counter by the given amount.
* @throws IllegalArgumentException If amt is negative.
*/
public void inc(double amt) {
if (amt < 0) {
if (amt < 0.0) {
throw new IllegalArgumentException("Amount to increment must be non-negative.");
}
value.add(amt);
Expand All @@ -137,7 +137,7 @@ public double get() {
* Increment the counter with no labels by 1.
*/
public void inc() {
inc(1);
inc(1.0);
}
/**
* Increment the counter with no labels by the given amount.
Expand All @@ -156,9 +156,12 @@ public double get() {

@Override
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.size());
for(Map.Entry<List<String>, Child> c: children.entrySet()) {
samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get()));
final Map.Entry<List<String>, Child>[] children = children();
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.length);
for(Map.Entry<List<String>, Child> c : children) {
if (c != null) {
samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get()));
}
}
return familySamplesList(Type.COUNTER, samples);
}
Expand Down
17 changes: 10 additions & 7 deletions simpleclient/src/main/java/io/prometheus/client/Gauge.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static class Child {
* Increment the gauge by 1.
*/
public void inc() {
inc(1);
inc(1.0);
}
/**
* Increment the gauge by the given amount.
Expand All @@ -155,7 +155,7 @@ public void inc(double amt) {
* Decrement the gauge by 1.
*/
public void dec() {
dec(1);
dec(1.0);
}
/**
* Decrement the gauge by the given amount.
Expand Down Expand Up @@ -238,7 +238,7 @@ public double get() {
* Increment the gauge with no labels by 1.
*/
public void inc() {
inc(1);
inc(1.0);
}
/**
* Increment the gauge with no labels by the given amount.
Expand All @@ -250,7 +250,7 @@ public void inc(double amt) {
* Decrement the gauge with no labels by 1.
*/
public void dec() {
dec(1);
dec(1.0);
}
/**
* Decrement the gauge with no labels by the given amount.
Expand Down Expand Up @@ -312,9 +312,12 @@ public double get() {

@Override
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.size());
for(Map.Entry<List<String>, Child> c: children.entrySet()) {
samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get()));
final Map.Entry<List<String>, Child>[] children = children();
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.length);
for(Map.Entry<List<String>, Child> c : children) {
if (c != null) {
samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get()));
}
}
return familySamplesList(Type.GAUGE, samples);
}
Expand Down
10 changes: 7 additions & 3 deletions simpleclient/src/main/java/io/prometheus/client/Histogram.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void observe(double amt) {
for (int i = 0; i < upperBounds.length; ++i) {
// The last bucket is +Inf, so we always increment.
if (amt <= upperBounds[i]) {
cumulativeCounts[i].add(1);
cumulativeCounts[i].add(1.0);
break;
}
}
Expand Down Expand Up @@ -323,8 +323,12 @@ public <E> E time(Callable<E> timeable){

@Override
public List<MetricFamilySamples> collect() {
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>();
for(Map.Entry<List<String>, Child> c: children.entrySet()) {
final Map.Entry<List<String>, Child>[] children = children();
List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(children.length);
for(Map.Entry<List<String>,Child> c : children) {
if (c == null) {
continue;
}
Child.Value v = c.getValue().get();
List<String> labelNamesWithLe = new ArrayList<String>(labelNames);
labelNamesWithLe.add("le");
Expand Down
Loading