Avoid auto-boxing for gauge metrics. (#1526)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Adrian Sutton 6 years ago committed by GitHub
parent 335cae2249
commit 9ef6002a4a
  1. 4
      ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/worldstate/WorldStateDownloader.java
  2. 3
      ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/discovery/VertxPeerDiscoveryAgent.java
  3. 4
      ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/network/DefaultP2PNetwork.java
  4. 26
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/node/provider/SyncStatusNodePermissioningProviderTest.java
  5. 15
      metrics/core/src/main/java/tech/pegasys/pantheon/metrics/MetricsSystem.java
  6. 4
      metrics/core/src/main/java/tech/pegasys/pantheon/metrics/noop/NoOpMetricsSystem.java
  7. 9
      metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/CurrentValueCollector.java
  8. 4
      metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/PrometheusMetricsSystem.java

@ -24,7 +24,7 @@ import java.time.Clock;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.IntSupplier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -78,7 +78,7 @@ public class WorldStateDownloader {
downloadStateValue(WorldDownloadState::getOutstandingTaskCount));
}
private Supplier<Integer> downloadStateValue(final Function<WorldDownloadState, Integer> getter) {
private IntSupplier downloadStateValue(final Function<WorldDownloadState, Integer> getter) {
return () -> {
final WorldDownloadState state = this.downloadState.get();
return state != null ? getter.apply(state) : 0;

@ -32,6 +32,7 @@ import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.OptionalInt;
import java.util.concurrent.CompletableFuture;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
import java.util.stream.StreamSupport;
@ -69,7 +70,7 @@ public class VertxPeerDiscoveryAgent extends PeerDiscoveryAgent {
pendingTaskCounter(vertx.nettyEventLoopGroup()));
}
private Supplier<Integer> pendingTaskCounter(final EventLoopGroup eventLoopGroup) {
private IntSupplier pendingTaskCounter(final EventLoopGroup eventLoopGroup) {
return () ->
StreamSupport.stream(eventLoopGroup.spliterator(), false)
.filter(eventExecutor -> eventExecutor instanceof SingleThreadEventExecutor)

@ -69,7 +69,7 @@ import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.function.IntSupplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@ -249,7 +249,7 @@ public class DefaultP2PNetwork implements P2PNetwork {
return new Builder();
}
private Supplier<Integer> pendingTaskCounter(final EventLoopGroup eventLoopGroup) {
private IntSupplier pendingTaskCounter(final EventLoopGroup eventLoopGroup) {
return () ->
StreamSupport.stream(eventLoopGroup.spliterator(), false)
.filter(eventExecutor -> eventExecutor instanceof SingleThreadEventExecutor)

@ -29,7 +29,7 @@ import tech.pegasys.pantheon.util.enode.EnodeURL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Supplier;
import java.util.function.IntSupplier;
import org.junit.Before;
import org.junit.Test;
@ -44,7 +44,7 @@ public class SyncStatusNodePermissioningProviderTest {
@Mock private Counter checkCounter;
@Mock private Counter checkPermittedCounter;
@Mock private Counter checkUnpermittedCounter;
private Supplier<Integer> syncGauge;
private IntSupplier syncGauge;
private static final EnodeURL bootnode =
EnodeURL.fromString(
@ -70,8 +70,8 @@ public class SyncStatusNodePermissioningProviderTest {
bootnodes.add(bootnode);
@SuppressWarnings("unchecked")
final ArgumentCaptor<Supplier<Integer>> syncGaugeCallbackCaptor =
ArgumentCaptor.forClass(Supplier.class);
final ArgumentCaptor<IntSupplier> syncGaugeCallbackCaptor =
ArgumentCaptor.forClass(IntSupplier.class);
when(metricsSystem.createCounter(
MetricCategory.PERMISSIONING,
@ -106,7 +106,7 @@ public class SyncStatusNodePermissioningProviderTest {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2));
assertThat(provider.hasReachedSync()).isFalse();
assertThat(syncGauge.get()).isEqualTo(0);
assertThat(syncGauge.getAsInt()).isEqualTo(0);
}
@Test
@ -114,29 +114,29 @@ public class SyncStatusNodePermissioningProviderTest {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 1));
assertThat(provider.hasReachedSync()).isTrue();
assertThat(syncGauge.get()).isEqualTo(1);
assertThat(syncGauge.getAsInt()).isEqualTo(1);
}
@Test
public void whenInSyncChangesFromTrueToFalseHasReachedSyncShouldReturnTrue() {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2));
assertThat(provider.hasReachedSync()).isFalse();
assertThat(syncGauge.get()).isEqualTo(0);
assertThat(syncGauge.getAsInt()).isEqualTo(0);
syncStatusListener.onSyncStatus(new SyncStatus(0, 2, 1));
assertThat(provider.hasReachedSync()).isTrue();
assertThat(syncGauge.get()).isEqualTo(1);
assertThat(syncGauge.getAsInt()).isEqualTo(1);
syncStatusListener.onSyncStatus(new SyncStatus(0, 2, 3));
assertThat(provider.hasReachedSync()).isTrue();
assertThat(syncGauge.get()).isEqualTo(1);
assertThat(syncGauge.getAsInt()).isEqualTo(1);
}
@Test
public void whenHasNotSyncedNonBootnodeShouldNotBePermitted() {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2));
assertThat(provider.hasReachedSync()).isFalse();
assertThat(syncGauge.get()).isEqualTo(0);
assertThat(syncGauge.getAsInt()).isEqualTo(0);
boolean isPermitted = provider.isPermitted(enode1, enode2);
@ -150,7 +150,7 @@ public class SyncStatusNodePermissioningProviderTest {
public void whenHasNotSyncedBootnodeIncomingConnectionShouldNotBePermitted() {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2));
assertThat(provider.hasReachedSync()).isFalse();
assertThat(syncGauge.get()).isEqualTo(0);
assertThat(syncGauge.getAsInt()).isEqualTo(0);
boolean isPermitted = provider.isPermitted(bootnode, enode1);
@ -164,7 +164,7 @@ public class SyncStatusNodePermissioningProviderTest {
public void whenHasNotSyncedBootnodeOutgoingConnectionShouldBePermitted() {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2));
assertThat(provider.hasReachedSync()).isFalse();
assertThat(syncGauge.get()).isEqualTo(0);
assertThat(syncGauge.getAsInt()).isEqualTo(0);
boolean isPermitted = provider.isPermitted(enode1, bootnode);
@ -178,7 +178,7 @@ public class SyncStatusNodePermissioningProviderTest {
public void whenHasSyncedIsPermittedShouldReturnTrue() {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 1));
assertThat(provider.hasReachedSync()).isTrue();
assertThat(syncGauge.get()).isEqualTo(1);
assertThat(syncGauge.getAsInt()).isEqualTo(1);
boolean isPermitted = provider.isPermitted(enode1, enode2);

@ -12,7 +12,9 @@
*/
package tech.pegasys.pantheon.metrics;
import java.util.function.Supplier;
import java.util.function.DoubleSupplier;
import java.util.function.IntSupplier;
import java.util.function.LongSupplier;
import java.util.stream.Stream;
public interface MetricsSystem {
@ -33,23 +35,22 @@ public interface MetricsSystem {
LabelledMetric<OperationTimer> createLabelledTimer(
MetricCategory category, String name, String help, String... labelNames);
void createGauge(
MetricCategory category, String name, String help, Supplier<Double> valueSupplier);
void createGauge(MetricCategory category, String name, String help, DoubleSupplier valueSupplier);
default void createIntegerGauge(
final MetricCategory category,
final String name,
final String help,
final Supplier<Integer> valueSupplier) {
createGauge(category, name, help, () -> (double) valueSupplier.get());
final IntSupplier valueSupplier) {
createGauge(category, name, help, () -> (double) valueSupplier.getAsInt());
}
default void createLongGauge(
final MetricCategory category,
final String name,
final String help,
final Supplier<Long> valueSupplier) {
createGauge(category, name, help, () -> (double) valueSupplier.get());
final LongSupplier valueSupplier) {
createGauge(category, name, help, () -> (double) valueSupplier.getAsLong());
}
Stream<Observation> streamObservations(MetricCategory category);

@ -20,7 +20,7 @@ import tech.pegasys.pantheon.metrics.Observation;
import tech.pegasys.pantheon.metrics.OperationTimer;
import tech.pegasys.pantheon.metrics.OperationTimer.TimingContext;
import java.util.function.Supplier;
import java.util.function.DoubleSupplier;
import java.util.stream.Stream;
import com.google.common.base.Preconditions;
@ -85,7 +85,7 @@ public class NoOpMetricsSystem implements MetricsSystem {
final MetricCategory category,
final String name,
final String help,
final Supplier<Double> valueSupplier) {}
final DoubleSupplier valueSupplier) {}
@Override
public Stream<Observation> streamObservations(final MetricCategory category) {

@ -16,7 +16,7 @@ import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import java.util.List;
import java.util.function.Supplier;
import java.util.function.DoubleSupplier;
import io.prometheus.client.Collector;
import io.prometheus.client.Collector.MetricFamilySamples.Sample;
@ -25,10 +25,10 @@ class CurrentValueCollector extends Collector {
private final String metricName;
private final String help;
private final Supplier<Double> valueSupplier;
private final DoubleSupplier valueSupplier;
public CurrentValueCollector(
final String metricName, final String help, final Supplier<Double> valueSupplier) {
final String metricName, final String help, final DoubleSupplier valueSupplier) {
this.metricName = metricName;
this.help = help;
this.valueSupplier = valueSupplier;
@ -36,7 +36,8 @@ class CurrentValueCollector extends Collector {
@Override
public List<MetricFamilySamples> collect() {
final Sample sample = new Sample(metricName, emptyList(), emptyList(), valueSupplier.get());
final Sample sample =
new Sample(metricName, emptyList(), emptyList(), valueSupplier.getAsDouble());
return singletonList(
new MetricFamilySamples(metricName, Type.GAUGE, help, singletonList(sample)));
}

@ -29,7 +29,7 @@ import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.function.DoubleSupplier;
import java.util.stream.Stream;
import io.prometheus.client.Collector;
@ -138,7 +138,7 @@ public class PrometheusMetricsSystem implements MetricsSystem {
final MetricCategory category,
final String name,
final String help,
final Supplier<Double> valueSupplier) {
final DoubleSupplier valueSupplier) {
final String metricName = convertToPrometheusName(category, name);
if (enabledCategories.contains(category)) {
final Collector collector = new CurrentValueCollector(metricName, help, valueSupplier);

Loading…
Cancel
Save