mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
implement windowed summary gauge
This commit is contained in:
parent
3990b42022
commit
ddc4dfe12b
@ -99,12 +99,37 @@ public class NBCreators {
|
||||
}
|
||||
|
||||
|
||||
public WindowSummaryGauge windowSummaryGauge(
|
||||
String name,
|
||||
int window,
|
||||
List<String> statspecs,
|
||||
MetricCategory category,
|
||||
String description
|
||||
) {
|
||||
List<WindowSummaryGauge.Stat> stats =
|
||||
statspecs.stream().map(WindowSummaryGauge.Stat::valueOf).toList();
|
||||
DoubleSummaryStatistics reservoir = new DoubleSummaryStatistics();
|
||||
WindowSummaryGauge anyGauge = null;
|
||||
for (WindowSummaryGauge.Stat stat : stats) {
|
||||
anyGauge = new WindowSummaryGauge(
|
||||
window,
|
||||
base.getLabels().and(NBLabels.forKV("name", name+"_w"+window, "stat", stat)),
|
||||
stat,
|
||||
description,
|
||||
category
|
||||
);
|
||||
base.addComponentMetric(anyGauge, category, description);
|
||||
}
|
||||
return anyGauge;
|
||||
|
||||
}
|
||||
|
||||
public DoubleSummaryGauge summaryGauge(String name, List<String> statspecs, MetricCategory category, String description) {
|
||||
List<DoubleSummaryGauge.Stat> stats = statspecs.stream().map(DoubleSummaryGauge.Stat::valueOf).toList();
|
||||
DoubleSummaryStatistics reservoir = new DoubleSummaryStatistics();
|
||||
DoubleSummaryGauge anyGauge = null;
|
||||
for (DoubleSummaryGauge.Stat stat : stats) {
|
||||
anyGauge = new DoubleSummaryGauge(base.getLabels().and(NBLabels.forKV("name",name,"stat", stat)), stat, reservoir, description, category);
|
||||
anyGauge = new DoubleSummaryGauge(base.getLabels().and(NBLabels.forKV("name", name, "stat", stat)), stat, reservoir, description, category);
|
||||
base.addComponentMetric(anyGauge, category, description);
|
||||
}
|
||||
return anyGauge;
|
||||
@ -113,6 +138,7 @@ public class NBCreators {
|
||||
public NBMetricHistogram histogram(String metricFamilyName, MetricCategory category, String description) {
|
||||
return histogram(metricFamilyName,4, category, description);
|
||||
}
|
||||
|
||||
public NBMetricHistogram histogram(String metricFamilyName, int hdrdigits, MetricCategory category, String description) {
|
||||
NBLabels labels = base.getLabels().and("name", metricFamilyName);
|
||||
NBMetricHistogram histogram = new NBMetricHistogram(labels, new DeltaHdrHistogramReservoir(labels, hdrdigits), description, category);
|
||||
@ -120,7 +146,7 @@ public class NBCreators {
|
||||
return histogram;
|
||||
}
|
||||
|
||||
// public AttachedMetricsSummaryReporter summaryReporter(long millis, String... labelspecs) {
|
||||
// public AttachedMetricsSummaryReporter summaryReporter(long millis, String... labelspecs) {
|
||||
// logger.debug("attaching summary reporter to " + base.description());
|
||||
// NBLabels extraLabels = NBLabels.forKV((Object[]) labelspecs);
|
||||
// AttachedMetricsSummaryReporter reporter = new AttachedMetricsSummaryReporter(base, extraLabels, millis);
|
||||
@ -198,7 +224,7 @@ public class NBCreators {
|
||||
private Logger logger = LogManager.getLogger(Log4JMetricsReporter.class);
|
||||
private Log4JMetricsReporter.LoggingLevel loggingLevel = Log4JMetricsReporter.LoggingLevel.INFO;
|
||||
private Marker marker;
|
||||
private MetricFilter filter= new MetricInstanceFilter();
|
||||
private MetricFilter filter = new MetricInstanceFilter();
|
||||
private boolean oneLastTime = false;
|
||||
private NBLabels labels;
|
||||
private long millis = 1000;
|
||||
@ -206,34 +232,42 @@ public class NBCreators {
|
||||
public Log4jReporterBuilder(NBComponent component) {
|
||||
this.component = component;
|
||||
}
|
||||
|
||||
public Log4jReporterBuilder oneLastTime(final boolean oneLastTime) {
|
||||
this.oneLastTime = oneLastTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Log4jReporterBuilder interval(final int interval) {
|
||||
this.millis = interval;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Log4jReporterBuilder outputTo(final Logger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Log4jReporterBuilder markWith(final Marker marker) {
|
||||
this.marker = marker;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Log4jReporterBuilder labels(final NBLabels labels) {
|
||||
this.labels = labels;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Log4jReporterBuilder filter(final MetricFilter filter) {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Log4jReporterBuilder withLoggingLevel(final Log4JMetricsReporter.LoggingLevel loggingLevel) {
|
||||
this.loggingLevel = loggingLevel;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Log4JMetricsReporter build() {
|
||||
final LoggerProxy loggerProxy = switch (this.loggingLevel) {
|
||||
case TRACE -> new TraceLoggerProxy(this.logger);
|
||||
@ -245,6 +279,7 @@ public class NBCreators {
|
||||
return new Log4JMetricsReporter(this.component, loggerProxy, this.marker, this.filter, this.labels, this.millis, this.oneLastTime);
|
||||
}
|
||||
}
|
||||
|
||||
/* private class to allow logger configuration */
|
||||
public abstract static class LoggerProxy {
|
||||
protected final Logger logger;
|
||||
@ -356,22 +391,27 @@ public class NBCreators {
|
||||
this.component = component;
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
public ConsoleReporterBuilder labels(NBLabels labels) {
|
||||
this.labels = labels;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ConsoleReporterBuilder interval(int interval) {
|
||||
this.interval = interval;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ConsoleReporterBuilder oneLastTime(boolean oneLastTime) {
|
||||
this.oneLastTime = oneLastTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ConsoleReporterBuilder disabledMetricAttributes(Set<MetricAttribute> disabledMetricAttributes) {
|
||||
this.disabledMetricAttributes = disabledMetricAttributes;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ConsoleReporter build() {
|
||||
return new ConsoleReporter(component, labels, interval, oneLastTime, output, disabledMetricAttributes);
|
||||
}
|
||||
@ -387,10 +427,12 @@ public class NBCreators {
|
||||
this.component = component;
|
||||
this.filename = filename;
|
||||
}
|
||||
|
||||
public CsvOutputWriterBuilder headers(String... headers) {
|
||||
this.headers = headers;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CsvOutputPluginWriter build() {
|
||||
return new CsvOutputPluginWriter(component, filename, headers);
|
||||
}
|
||||
@ -406,26 +448,32 @@ public class NBCreators {
|
||||
public CsvReporterBuilder(NBComponent component) {
|
||||
this.component = component;
|
||||
}
|
||||
|
||||
public CsvReporterBuilder labels(NBLabels labels) {
|
||||
this.labels = labels;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CsvReporterBuilder path(Path reportTo) {
|
||||
this.reportTo = reportTo;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CsvReporterBuilder path(String reportTo) {
|
||||
this.reportTo = Path.of(reportTo);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CsvReporterBuilder interval(int interval) {
|
||||
this.interval = interval;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CsvReporterBuilder filter(MetricInstanceFilter filter) {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CsvReporter build() {
|
||||
return new CsvReporter(component, reportTo, interval, filter, labels);
|
||||
}
|
||||
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (c) 2023 nosqlbench
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.nosqlbench.nb.api.engine.metrics;
|
||||
|
||||
import io.nosqlbench.nb.api.engine.metrics.instruments.MetricCategory;
|
||||
import io.nosqlbench.nb.api.engine.metrics.instruments.NBMetricGauge;
|
||||
import io.nosqlbench.nb.api.labels.NBLabels;
|
||||
import io.nosqlbench.nb.api.stats.StatBucket;
|
||||
|
||||
import java.util.DoubleSummaryStatistics;
|
||||
import java.util.function.DoubleConsumer;
|
||||
|
||||
|
||||
/**
|
||||
* Create a discrete stat reservoir as a gauge.
|
||||
*/
|
||||
public class WindowSummaryGauge implements NBMetricGauge, DoubleConsumer {
|
||||
private final NBLabels labels;
|
||||
private final Stat stat;
|
||||
private final StatBucket stats;
|
||||
private final String description;
|
||||
private final MetricCategory[] categories;
|
||||
private final int window;
|
||||
|
||||
@Override
|
||||
public String typeName() {
|
||||
return "gauge";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MetricCategory[] getCategories() {
|
||||
return this.categories;
|
||||
}
|
||||
|
||||
public enum Stat {
|
||||
Min,
|
||||
Max,
|
||||
Average,
|
||||
Count,
|
||||
Sum
|
||||
}
|
||||
|
||||
|
||||
public WindowSummaryGauge(int window, NBLabels labels, Stat stat, String description,
|
||||
MetricCategory... categories) {
|
||||
this.labels = labels;
|
||||
this.stat = stat;
|
||||
this.description = description;
|
||||
this.categories = categories;
|
||||
this.window = window;
|
||||
this.stats = new StatBucket(window);
|
||||
}
|
||||
|
||||
public synchronized void accept(double value) {
|
||||
stats.apply(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Double getValue() {
|
||||
return switch(stat) {
|
||||
case Min -> stats.getMin();
|
||||
case Max -> stats.getMax();
|
||||
case Average -> stats.getAverage();
|
||||
case Count -> (double) stats.getCount();
|
||||
case Sum -> stats.getSum();
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBLabels getLabels() {
|
||||
return labels;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.labels.toString()+":"+this.stats.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (c) 2023 nosqlbench
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.nosqlbench.nb.api.engine.metrics.wrappers;
|
||||
|
||||
import io.nosqlbench.nb.api.components.core.NBComponent;
|
||||
import io.nosqlbench.nb.api.engine.metrics.WindowSummaryGauge;
|
||||
import io.nosqlbench.nb.api.engine.metrics.instruments.MetricCategory;
|
||||
import io.nosqlbench.nb.api.labels.NBLabeledElement;
|
||||
import io.nosqlbench.nb.api.labels.NBLabels;
|
||||
import io.nosqlbench.nb.api.stats.StatBucket;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class WindowedRelevancyMeasures implements NBLabeledElement {
|
||||
|
||||
private final NBComponent parent;
|
||||
private final NBLabels labels;
|
||||
private final List<RelevancyFunction> functions = new ArrayList<>();
|
||||
private final List<WindowSummaryGauge> gauges = new ArrayList<>();
|
||||
private final int window;
|
||||
private int offset = 0;
|
||||
|
||||
public WindowedRelevancyMeasures(NBComponent parent, int window) {
|
||||
this(parent, NBLabels.forKV(),window);
|
||||
}
|
||||
|
||||
public WindowedRelevancyMeasures(NBComponent parent, NBLabels labels, int window) {
|
||||
this.parent = parent;
|
||||
this.labels = labels;
|
||||
this.window = window;
|
||||
}
|
||||
|
||||
public WindowedRelevancyMeasures(NBComponent parent, Map<String, String> labels, int window) {
|
||||
this(parent, NBLabels.forMap(labels), window);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBLabels getLabels() {
|
||||
return parent.getLabels().and(labels);
|
||||
}
|
||||
|
||||
public WindowedRelevancyMeasures addFunction(RelevancyFunction... f) {
|
||||
for (RelevancyFunction function : f) {
|
||||
this.functions.add(function);
|
||||
function.prependLabels(this);
|
||||
WindowSummaryGauge gauge = parent.create().windowSummaryGauge(
|
||||
function.getUniqueName(),
|
||||
window,
|
||||
List.of("Average"),
|
||||
MetricCategory.Accuracy,
|
||||
WindowSummaryGauge.Stat.Average.toString()
|
||||
);
|
||||
this.gauges.add(gauge);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void accept(int[] relevant, int[] actual) {
|
||||
offset++;
|
||||
if (offset >= window) {
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < functions.size(); i++) {
|
||||
double metricValue = functions.get(i).apply(relevant, actual);
|
||||
gauges.get(i).accept(metricValue);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (WindowSummaryGauge gauge : gauges) {
|
||||
sb.append(gauge.toString()).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user