Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Avoid auto-boxing for gauge metrics #1526

Merged
merged 1 commit into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
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;
Expand Down Expand Up @@ -78,7 +78,7 @@ public 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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;

Expand Down Expand Up @@ -69,7 +70,7 @@ public VertxPeerDiscoveryAgent(
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
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;
Expand Down Expand Up @@ -249,7 +249,7 @@ public static Builder builder() {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

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;
Expand All @@ -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(
Expand All @@ -70,8 +70,8 @@ public void before() {
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,
Expand Down Expand Up @@ -106,37 +106,37 @@ public void whenIsNotInSyncHasReachedSyncShouldReturnFalse() {
syncStatusListener.onSyncStatus(new SyncStatus(0, 1, 2));

assertThat(provider.hasReachedSync()).isFalse();
assertThat(syncGauge.get()).isEqualTo(0);
assertThat(syncGauge.getAsInt()).isEqualTo(0);
}

@Test
public void whenInSyncHasReachedSyncShouldReturnTrue() {
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);

Expand All @@ -150,7 +150,7 @@ public void whenHasNotSyncedNonBootnodeShouldNotBePermitted() {
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);

Expand All @@ -164,7 +164,7 @@ public void whenHasNotSyncedBootnodeIncomingConnectionShouldNotBePermitted() {
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);

Expand All @@ -178,7 +178,7 @@ public void whenHasNotSyncedBootnodeOutgoingConnectionShouldBePermitted() {
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -33,23 +35,22 @@ default OperationTimer createTimer(
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
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;
Expand Down Expand Up @@ -85,7 +85,7 @@ public void createGauge(
final MetricCategory category,
final String name,
final String help,
final Supplier<Double> valueSupplier) {}
final DoubleSupplier valueSupplier) {}

@Override
public Stream<Observation> streamObservations(final MetricCategory category) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
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;
Expand All @@ -25,18 +25,19 @@ 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;
}

@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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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;
Expand Down Expand Up @@ -138,7 +138,7 @@ public void createGauge(
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);
Expand Down