align test name to file

This commit is contained in:
Jonathan Shook 2023-08-16 15:44:29 -05:00
parent df15137999
commit 745d203766
7 changed files with 143 additions and 46 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 nosqlbench
* Copyright (c) 2022-2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,14 +17,16 @@
package io.nosqlbench.engine.core.lifecycle.scenario.script.bindings;
import com.codahale.metrics.*;
import com.codahale.metrics.Timer;
import io.nosqlbench.engine.core.metrics.MetricMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyObject;
import java.util.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
/**
* A view of metrics objects as an object tree.
@ -82,7 +84,7 @@ public class PolyglotMetricRegistryBindings implements ProxyObject, MetricRegist
@Override
public void onGaugeRemoved(String name) {
metrics.findOwner(name).remove(name);
metrics.findOrCreateDottedParentPath(name).remove(name);
logger.debug("gauge removed: " + name);
}
@ -94,7 +96,7 @@ public class PolyglotMetricRegistryBindings implements ProxyObject, MetricRegist
@Override
public void onCounterRemoved(String name) {
metrics.findOwner(name).remove(name);
metrics.findOrCreateDottedParentPath(name).remove(name);
logger.debug("counter removed: " + name);
}
@ -106,7 +108,7 @@ public class PolyglotMetricRegistryBindings implements ProxyObject, MetricRegist
@Override
public void onHistogramRemoved(String name) {
metrics.findOwner(name).remove(name);
metrics.findOrCreateDottedParentPath(name).remove(name);
logger.debug("histogram removed: " + name);
}
@ -118,7 +120,7 @@ public class PolyglotMetricRegistryBindings implements ProxyObject, MetricRegist
@Override
public void onMeterRemoved(String name) {
metrics.findOwner(name).remove(name);
metrics.findOrCreateDottedParentPath(name).remove(name);
logger.debug("meter removed: " + name);
}
@ -131,7 +133,7 @@ public class PolyglotMetricRegistryBindings implements ProxyObject, MetricRegist
@Override
public void onTimerRemoved(String name) {
metrics.findOwner(name).remove(name);
metrics.findOrCreateDottedParentPath(name).remove(name);
logger.debug("timer removed: " + name);
}
@ -154,4 +156,21 @@ public class PolyglotMetricRegistryBindings implements ProxyObject, MetricRegist
return totalMap;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PolyglotMetricRegistryBindings that = (PolyglotMetricRegistryBindings) o;
if (!registry.equals(that.registry)) return false;
return Objects.equals(metrics, that.metrics);
}
@Override
public int hashCode() {
int result = registry.hashCode();
result = 31 * result + (metrics != null ? metrics.hashCode() : 0);
return result;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 nosqlbench
* Copyright (c) 2022-2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,10 +17,12 @@
package io.nosqlbench.engine.core.metrics;
import com.codahale.metrics.Metric;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyObject;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import java.security.InvalidParameterException;
import java.util.*;
public class MetricMap implements ProxyObject {
@ -30,56 +32,62 @@ public class MetricMap implements ProxyObject {
private final String parent_name;
private final HashMap<String, Object> map = new HashMap<>();
public final static char DELIM = '.';
public MetricMap(String name, String parent) {
this.name = name;
this.parent_name = parent;
}
public MetricMap findOwner(String metricName) {
public MetricMap() {
this("ROOT", "ROOT"); // because of auto-intern, the root node is the only one with parent==parent
}
public MetricMap findOrCreateDottedParentPath(String metricName) {
String[] names = metricName.split("\\.");
String[] pathTraversal = Arrays.copyOfRange(names, 0, names.length - 1);
MetricMap owner = findPath(pathTraversal);
MetricMap owner = findOrCreateNodePath(pathTraversal);
return owner;
}
public MetricMap findOrCreateDottedNodePath(String nodeName) {
String[] names = nodeName.split("\\.");
MetricMap owner = findOrCreateNodePath(names);
return owner;
}
@Override
public String toString() {
return "MetricMap{" +
"name='" + name + '\'' +
", map=" + map +
(parent_name!=null ? ", parent=" + parent_name : "") +
(parent_name != null ? ", parent=" + parent_name : "") +
'}';
}
public MetricMap findPath(String... names) {
MetricMap current = this;
for (int i = 0; i < names.length; i++) {
String edgeName = names[i];
if (current.map.containsKey(edgeName)) {
Object element = current.map.get(edgeName);
if (element instanceof MetricMap) {
current = (MetricMap) element;
logger.trace(() -> "traversing edge:" + edgeName);
} else {
String error = "edge exists at level:" + i;
logger.error(error);
throw new RuntimeException(error);
}
} else {
MetricMap newMap = new MetricMap(edgeName,this.name);
current.map.put(edgeName, newMap);
current = newMap;
logger.trace(() -> "adding edge:" + edgeName);
}
/**
* Given an array of non-delimited component names, walk from the root node to each name, creating any needed nodes
* along the way.
*
* @param names the names of the nodes to traverse or create
* @return The MetricMap node in the node tree with the given path-wise address.
* @throws InvalidParameterException if any of the component names includes a delimiter
*/
public MetricMap findOrCreateNodePath(String... names) {
if (names.length == 0) {
return this;
}
return current;
String nodeName = names[0];
if (nodeName.contains(String.valueOf(DELIM))) {
throw new InvalidParameterException("Path components must not include interior delimiters. (" + DELIM + ").");
}
MetricMap childNode = (MetricMap) map.computeIfAbsent(nodeName, name -> new MetricMap(names[0], this.name));
return childNode.findOrCreateNodePath(Arrays.copyOfRange(names, 1, names.length));
}
public void add(String name, Metric metric) {
MetricMap owner = findOwner(name);
String leafName = name.substring(name.lastIndexOf(".")+1);
owner.map.put(leafName,metric);
MetricMap owner = findOrCreateDottedParentPath(name);
String leafName = name.substring(name.lastIndexOf(".") + 1);
owner.map.put(leafName, metric);
}
public void remove(String name) {
@ -100,6 +108,9 @@ public class MetricMap implements ProxyObject {
@Override
public Object getMember(String key) {
if (key.contains(".")) {
throw new InvalidParameterException("Members of the metrics registry tree must have names which do not include the '.' delimiter.");
}
Object got = get(key);
return got;
}
@ -120,4 +131,13 @@ public class MetricMap implements ProxyObject {
public void putMember(String key, Value value) {
throw new RuntimeException("Not allowed here");
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (parent_name != null ? parent_name.hashCode() : 0);
result = 31 * result + map.hashCode();
return result;
}
}

View File

@ -0,0 +1,49 @@
/*
* 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.engine.core.metrics;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class MetricMapTest {
@Test
public void testNodeByNodeConstruction() {
MetricMap root = new MetricMap();
MetricMap alpha = root.findOrCreateNodePath("alpha");
MetricMap beta = alpha.findOrCreateNodePath("beta");
MetricMap gamma = beta.findOrCreateNodePath("gamma");
assertThat(root.containsKey("alpha")).isTrue();
assertThat(root.findOrCreateNodePath("alpha","beta","gamma")==gamma).isTrue();
assertThat(root.findOrCreateDottedNodePath("alpha.beta.gamma")==gamma).isTrue();
}
@Test
public void testConcatenatedConstruction() {
MetricMap root = new MetricMap();
MetricMap gamma = root.findOrCreateDottedNodePath("alpha.beta.gamma");
assertThat(root.findOrCreateDottedParentPath("alpha.beta.gamma.abstract_leaf_node")).isEqualTo(gamma);
MetricMap alpha = root.findOrCreateDottedParentPath("alpha.beta");
MetricMap beta = alpha.findOrCreateDottedParentPath("beta.gamma");
MetricMap betaToo = beta.findOrCreateDottedParentPath("gamma");
assertThat(beta).isEqualTo(betaToo);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 nosqlbench
* Copyright (c) 2022-2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import com.codahale.metrics.MetricRegistry;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
public class CSVMetrics {
@ -80,6 +81,7 @@ public class CSVMetrics {
}
public CSVMetrics add(Metric metric) {
Objects.requireNonNull(metric);
filter.add(metric);
return this;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 nosqlbench
* Copyright (c) 2022-2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -40,7 +40,7 @@ public class CSVMetricsPlugin {
*/
public CSVMetrics log(String filename) {
CSVMetrics csvMetrics = new CSVMetrics(filename, logger, metricRegistry);
writeStdout("started new csvlogger: " + filename + "\n");
writeStdout("started new csvmetrics: " + filename + "\n");
return csvMetrics;
}

View File

@ -87,4 +87,9 @@ public class NBMetricTimer extends Timer implements DeltaSnapshotter, HdrDeltaHi
public NBLabels getLabels() {
return labels;
}
@Override
public String toString() {
return "NBTIMER:"+this.getLabels().toString();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 nosqlbench
* Copyright (c) 2022-2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,11 +20,13 @@ activitydef = {
"alias" : "csvmetrics",
"driver" : "diag",
"cycles" : "50000",
"threads" : "20",
"op": '{"log":"level=debug,interval=1000"}',
"targetrate" : "10000.0"
"threads" : "1",
"op": "log: level=debug",
"rate" : "100.0"
};
scenario.start(activitydef);
scenario.waitMillis(500);
csvlogger.add(metrics.csvmetrics.cycles.servicetime);
csvlogger.start(500,"MILLISECONDS");