implement threads metric on activity

This commit is contained in:
Jonathan Shook
2023-09-25 23:47:37 -05:00
parent e9cf289787
commit bd57c26de1

View File

@@ -15,6 +15,9 @@
*/ */
package io.nosqlbench.engine.core.lifecycle.activity; package io.nosqlbench.engine.core.lifecycle.activity;
import com.codahale.metrics.Gauge;
import io.nosqlbench.api.engine.metrics.ActivityMetrics;
import io.nosqlbench.api.engine.metrics.instruments.NBFunctionGauge;
import io.nosqlbench.api.labels.NBLabeledElement; import io.nosqlbench.api.labels.NBLabeledElement;
import io.nosqlbench.api.labels.NBLabels; import io.nosqlbench.api.labels.NBLabels;
import io.nosqlbench.engine.api.activityapi.core.*; import io.nosqlbench.engine.api.activityapi.core.*;
@@ -72,6 +75,7 @@ public class ActivityExecutor implements NBLabeledElement, ActivityController, P
private long stoppedAt = 0L; private long stoppedAt = 0L;
private ActivityExecutorShutdownHook shutdownHook = null; private ActivityExecutorShutdownHook shutdownHook = null;
private NBFunctionGauge threadsGauge;
public ActivityExecutor(Activity activity, String sessionId) { public ActivityExecutor(Activity activity, String sessionId) {
this.activity = activity; this.activity = activity;
@@ -450,6 +454,15 @@ public class ActivityExecutor implements NBLabeledElement, ActivityController, P
startMotorExecutorService(); startMotorExecutorService();
startRunningActivityThreads(); startRunningActivityThreads();
awaitMotorsAtLeastRunning(); awaitMotorsAtLeastRunning();
registerMetrics();
}
private void registerMetrics() {
this.threadsGauge= threadsGauge = ActivityMetrics.register(new NBFunctionGauge(activity, () -> (double) this.motors.size(), "threads"));
}
private void unregisterMetrics() {
ActivityMetrics.unregister(this.threadsGauge);
this.threadsGauge=null;
} }
private boolean shutdownExecutorService(int secondsToWait) { private boolean shutdownExecutorService(int secondsToWait) {
@@ -472,6 +485,7 @@ public class ActivityExecutor implements NBLabeledElement, ActivityController, P
} finally { } finally {
logger.trace(() -> "finally shutting down activity " + this.getActivity().getAlias()); logger.trace(() -> "finally shutting down activity " + this.getActivity().getAlias());
this.stoppedAt = System.currentTimeMillis(); this.stoppedAt = System.currentTimeMillis();
unregisterMetrics();
activity.setRunState(RunState.Stopped); activity.setRunState(RunState.Stopped);
} }
@@ -567,6 +581,16 @@ public class ActivityExecutor implements NBLabeledElement, ActivityController, P
.build()); .build());
} }
private class ThreadsGauge implements Gauge<Double> {
public ThreadsGauge(ActivityExecutor activityExecutor) {
ActivityExecutor ae = activityExecutor;
}
@Override
public Double getValue() {
return (double) ActivityExecutor.this.motors.size();
}
}
} }