From 9ef6002a4ad40939b4ff2975f03741e6792b3ef2 Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Wed, 5 Jun 2019 13:38:00 +1000 Subject: [PATCH] Avoid auto-boxing for gauge metrics. (#1526) Signed-off-by: Adrian Sutton --- .../sync/worldstate/WorldStateDownloader.java | 4 +-- .../discovery/VertxPeerDiscoveryAgent.java | 3 ++- .../p2p/network/DefaultP2PNetwork.java | 4 +-- ...ncStatusNodePermissioningProviderTest.java | 26 +++++++++---------- .../pantheon/metrics/MetricsSystem.java | 15 ++++++----- .../metrics/noop/NoOpMetricsSystem.java | 4 +-- .../prometheus/CurrentValueCollector.java | 9 ++++--- .../prometheus/PrometheusMetricsSystem.java | 4 +-- 8 files changed, 36 insertions(+), 33 deletions(-) diff --git a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/worldstate/WorldStateDownloader.java b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/worldstate/WorldStateDownloader.java index ad49c994a0..21186e723f 100644 --- a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/worldstate/WorldStateDownloader.java +++ b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/worldstate/WorldStateDownloader.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 downloadStateValue(final Function getter) { + private IntSupplier downloadStateValue(final Function getter) { return () -> { final WorldDownloadState state = this.downloadState.get(); return state != null ? getter.apply(state) : 0; diff --git a/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/discovery/VertxPeerDiscoveryAgent.java b/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/discovery/VertxPeerDiscoveryAgent.java index 1d45e5f0f4..200e27c9b0 100644 --- a/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/discovery/VertxPeerDiscoveryAgent.java +++ b/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/discovery/VertxPeerDiscoveryAgent.java @@ -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 pendingTaskCounter(final EventLoopGroup eventLoopGroup) { + private IntSupplier pendingTaskCounter(final EventLoopGroup eventLoopGroup) { return () -> StreamSupport.stream(eventLoopGroup.spliterator(), false) .filter(eventExecutor -> eventExecutor instanceof SingleThreadEventExecutor) diff --git a/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/network/DefaultP2PNetwork.java b/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/network/DefaultP2PNetwork.java index 6e8af0ab73..be9736771d 100644 --- a/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/network/DefaultP2PNetwork.java +++ b/ethereum/p2p/src/main/java/tech/pegasys/pantheon/ethereum/p2p/network/DefaultP2PNetwork.java @@ -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 pendingTaskCounter(final EventLoopGroup eventLoopGroup) { + private IntSupplier pendingTaskCounter(final EventLoopGroup eventLoopGroup) { return () -> StreamSupport.stream(eventLoopGroup.spliterator(), false) .filter(eventExecutor -> eventExecutor instanceof SingleThreadEventExecutor) diff --git a/ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/node/provider/SyncStatusNodePermissioningProviderTest.java b/ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/node/provider/SyncStatusNodePermissioningProviderTest.java index 94f87e2f45..07aa00b16c 100644 --- a/ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/node/provider/SyncStatusNodePermissioningProviderTest.java +++ b/ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/node/provider/SyncStatusNodePermissioningProviderTest.java @@ -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 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> syncGaugeCallbackCaptor = - ArgumentCaptor.forClass(Supplier.class); + final ArgumentCaptor 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); diff --git a/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/MetricsSystem.java b/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/MetricsSystem.java index cdd5b70d7f..b444af6a32 100644 --- a/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/MetricsSystem.java +++ b/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/MetricsSystem.java @@ -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 createLabelledTimer( MetricCategory category, String name, String help, String... labelNames); - void createGauge( - MetricCategory category, String name, String help, Supplier 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 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 valueSupplier) { - createGauge(category, name, help, () -> (double) valueSupplier.get()); + final LongSupplier valueSupplier) { + createGauge(category, name, help, () -> (double) valueSupplier.getAsLong()); } Stream streamObservations(MetricCategory category); diff --git a/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/noop/NoOpMetricsSystem.java b/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/noop/NoOpMetricsSystem.java index 524ecf1fa1..e478e093f6 100644 --- a/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/noop/NoOpMetricsSystem.java +++ b/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/noop/NoOpMetricsSystem.java @@ -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 valueSupplier) {} + final DoubleSupplier valueSupplier) {} @Override public Stream streamObservations(final MetricCategory category) { diff --git a/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/CurrentValueCollector.java b/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/CurrentValueCollector.java index e1666be7ca..5cffb0f7cd 100644 --- a/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/CurrentValueCollector.java +++ b/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/CurrentValueCollector.java @@ -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 valueSupplier; + private final DoubleSupplier valueSupplier; public CurrentValueCollector( - final String metricName, final String help, final Supplier 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 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))); } diff --git a/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/PrometheusMetricsSystem.java b/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/PrometheusMetricsSystem.java index c3b9ca84d5..d4f58dc5f0 100644 --- a/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/PrometheusMetricsSystem.java +++ b/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/PrometheusMetricsSystem.java @@ -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 valueSupplier) { + final DoubleSupplier valueSupplier) { final String metricName = convertToPrometheusName(category, name); if (enabledCategories.contains(category)) { final Collector collector = new CurrentValueCollector(metricName, help, valueSupplier);