Skip to content

Commit

Permalink
Metrics improvements : rename a few things and use static instance fo…
Browse files Browse the repository at this point in the history
…r DummyMetrics as they are stateless
  • Loading branch information
vietj committed May 26, 2016
1 parent 6b8437f commit 2bf96da
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/vertx/core/impl/VertxImpl.java
Expand Up @@ -378,7 +378,7 @@ private VertxMetrics initialiseMetrics(VertxOptions options) {
return metrics; return metrics;
} }
} }
return new DummyVertxMetrics(); return DummyVertxMetrics.INSTANCE;
} }


private ClusterManager getClusterManager(VertxOptions options) { private ClusterManager getClusterManager(VertxOptions options) {
Expand Down
40 changes: 27 additions & 13 deletions src/main/java/io/vertx/core/metrics/impl/DummyVertxMetrics.java
Expand Up @@ -44,6 +44,8 @@
*/ */
public class DummyVertxMetrics implements VertxMetrics { public class DummyVertxMetrics implements VertxMetrics {


public static final DummyVertxMetrics INSTANCE = new DummyVertxMetrics();

@Override @Override
public void verticleDeployed(Verticle verticle) { public void verticleDeployed(Verticle verticle) {
} }
Expand All @@ -62,37 +64,37 @@ public void timerEnded(long id, boolean cancelled) {


@Override @Override
public EventBusMetrics createMetrics(EventBus eventBus) { public EventBusMetrics createMetrics(EventBus eventBus) {
return new DummyEventBusMetrics(); return DummyEventBusMetrics.INSTANCE;
} }


@Override @Override
public HttpServerMetrics createMetrics(HttpServer server, SocketAddress localAddress, HttpServerOptions options) { public HttpServerMetrics createMetrics(HttpServer server, SocketAddress localAddress, HttpServerOptions options) {
return new DummyHttpServerMetrics(); return DummyHttpServerMetrics.INSTANCE;
} }


@Override @Override
public HttpClientMetrics createMetrics(HttpClient client, HttpClientOptions options) { public HttpClientMetrics createMetrics(HttpClient client, HttpClientOptions options) {
return new DummyHttpClientMetrics(); return DummyHttpClientMetrics.INSTANCE;
} }


@Override @Override
public TCPMetrics createMetrics(NetServer server, SocketAddress localAddress, NetServerOptions options) { public TCPMetrics createMetrics(NetServer server, SocketAddress localAddress, NetServerOptions options) {
return new DummyTCPMetrics(); return DummyTCPMetrics.INSTANCE;
} }


@Override @Override
public TCPMetrics createMetrics(NetClient client, NetClientOptions options) { public TCPMetrics createMetrics(NetClient client, NetClientOptions options) {
return new DummyTCPMetrics(); return DummyTCPMetrics.INSTANCE;
} }


@Override @Override
public DatagramSocketMetrics createMetrics(DatagramSocket socket, DatagramSocketOptions options) { public DatagramSocketMetrics createMetrics(DatagramSocket socket, DatagramSocketOptions options) {
return new DummyDatagramMetrics(); return DummyDatagramMetrics.INSTANCE;
} }


@Override @Override
public <P> PoolMetrics<?> createMetrics(P pool, String poolType, String poolName, int maxPoolSize) { public <P> PoolMetrics<?> createMetrics(P pool, String poolType, String poolName, int maxPoolSize) {
return new DummyWorkerPoolMetrics(); return DummyWorkerPoolMetrics.INSTANCE;
} }


@Override @Override
Expand All @@ -109,7 +111,9 @@ public boolean isMetricsEnabled() {
return false; return false;
} }


protected class DummyEventBusMetrics implements EventBusMetrics<Void> { public static class DummyEventBusMetrics implements EventBusMetrics<Void> {

public static final DummyEventBusMetrics INSTANCE = new DummyEventBusMetrics();


@Override @Override
public void messageWritten(String address, int numberOfBytes) { public void messageWritten(String address, int numberOfBytes) {
Expand Down Expand Up @@ -158,7 +162,9 @@ public void close() {
} }
} }


protected class DummyHttpServerMetrics implements HttpServerMetrics<Void, Void, Void> { public static class DummyHttpServerMetrics implements HttpServerMetrics<Void, Void, Void> {

public static final DummyHttpServerMetrics INSTANCE = new DummyHttpServerMetrics();


@Override @Override
public Void requestBegin(Void socketMetric, HttpServerRequest request) { public Void requestBegin(Void socketMetric, HttpServerRequest request) {
Expand Down Expand Up @@ -223,7 +229,9 @@ public void disconnected(Void serverWebSocketMetric) {
} }
} }


protected class DummyHttpClientMetrics implements HttpClientMetrics<Void, Void, Void> { public static class DummyHttpClientMetrics implements HttpClientMetrics<Void, Void, Void> {

public static final DummyHttpClientMetrics INSTANCE = new DummyHttpClientMetrics();


@Override @Override
public Void requestBegin(Void socketMetric, SocketAddress localAddress, SocketAddress remoteAddress, HttpClientRequest request) { public Void requestBegin(Void socketMetric, SocketAddress localAddress, SocketAddress remoteAddress, HttpClientRequest request) {
Expand Down Expand Up @@ -283,7 +291,9 @@ public void disconnected(Void webSocketMetric) {
} }
} }


protected class DummyTCPMetrics implements TCPMetrics<Void> { public static class DummyTCPMetrics implements TCPMetrics<Void> {

public static final DummyTCPMetrics INSTANCE = new DummyTCPMetrics();


@Override @Override
public Void connected(SocketAddress remoteAddress, String remoteName) { public Void connected(SocketAddress remoteAddress, String remoteName) {
Expand Down Expand Up @@ -316,7 +326,9 @@ public boolean isEnabled() {
} }
} }


protected class DummyDatagramMetrics implements DatagramSocketMetrics { public static class DummyDatagramMetrics implements DatagramSocketMetrics {

public static final DummyDatagramMetrics INSTANCE = new DummyDatagramMetrics();


@Override @Override
public void listening(String localName, SocketAddress localAddress) { public void listening(String localName, SocketAddress localAddress) {
Expand Down Expand Up @@ -344,7 +356,9 @@ public boolean isEnabled() {
} }
} }


private class DummyWorkerPoolMetrics implements PoolMetrics<Void> { public static class DummyWorkerPoolMetrics implements PoolMetrics<Void> {

public static final DummyWorkerPoolMetrics INSTANCE = new DummyWorkerPoolMetrics();


@Override @Override
public Void submitted() { public Void submitted() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/vertx/test/core/CustomMetricsFactory.java
Expand Up @@ -30,7 +30,7 @@ public class CustomMetricsFactory implements VertxMetricsFactory {


@Override @Override
public VertxMetrics metrics(Vertx vertx, VertxOptions options) { public VertxMetrics metrics(Vertx vertx, VertxOptions options) {
return new DummyVertxMetrics(); return DummyVertxMetrics.INSTANCE;
} }


@Override @Override
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/io/vertx/test/core/MetricsContextTest.java
Expand Up @@ -49,7 +49,7 @@ public void testFactory() throws Exception {
VertxMetricsFactory factory = (vertx, options) -> { VertxMetricsFactory factory = (vertx, options) -> {
metricsThread.set(Thread.currentThread()); metricsThread.set(Thread.currentThread());
metricsContext.set(Vertx.currentContext()); metricsContext.set(Vertx.currentContext());
return new DummyVertxMetrics(); return DummyVertxMetrics.INSTANCE;
}; };
vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory))); vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
assertSame(Thread.currentThread(), metricsThread.get()); assertSame(Thread.currentThread(), metricsThread.get());
Expand All @@ -64,7 +64,7 @@ public void testFactoryInCluster() throws Exception {
VertxMetricsFactory factory = (vertx, options) -> { VertxMetricsFactory factory = (vertx, options) -> {
metricsThread.set(Thread.currentThread()); metricsThread.set(Thread.currentThread());
metricsContext.set(Vertx.currentContext()); metricsContext.set(Vertx.currentContext());
return new DummyVertxMetrics(); return DummyVertxMetrics.INSTANCE;
}; };
clusteredVertx(new VertxOptions().setClustered(true).setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)), onSuccess(vertx -> { clusteredVertx(new VertxOptions().setClustered(true).setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)), onSuccess(vertx -> {
assertSame(testThread, metricsThread.get()); assertSame(testThread, metricsThread.get());
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/io/vertx/test/core/MetricsOptionsTest.java
Expand Up @@ -27,7 +27,6 @@
import org.junit.Test; import org.junit.Test;


import java.io.File; import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
Expand Down Expand Up @@ -88,7 +87,7 @@ public void testMetricsEnabledWithoutConfig() {


@Test @Test
public void testSetMetricsInstance() { public void testSetMetricsInstance() {
DummyVertxMetrics metrics = new DummyVertxMetrics(); DummyVertxMetrics metrics = DummyVertxMetrics.INSTANCE;
vertx.close(); vertx.close();
vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(new SimpleVertxMetricsFactory<>(metrics)))); vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(new SimpleVertxMetricsFactory<>(metrics))));
assertSame(metrics, ((VertxInternal) vertx).metricsSPI()); assertSame(metrics, ((VertxInternal) vertx).metricsSPI());
Expand All @@ -106,7 +105,7 @@ public void testMetricsFromServiceLoader() {


@Test @Test
public void testSetMetricsInstanceTakesPrecedenceOverServiceLoader() { public void testSetMetricsInstanceTakesPrecedenceOverServiceLoader() {
DummyVertxMetrics metrics = new DummyVertxMetrics(); DummyVertxMetrics metrics = DummyVertxMetrics.INSTANCE;
vertx.close(); vertx.close();
VertxOptions options = new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(new SimpleVertxMetricsFactory<>(metrics))); VertxOptions options = new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(new SimpleVertxMetricsFactory<>(metrics)));
vertx = createVertxLoadingMetricsFromMetaInf(options, "io.vertx.test.fakemetrics.FakeMetricsFactory"); vertx = createVertxLoadingMetricsFromMetaInf(options, "io.vertx.test.fakemetrics.FakeMetricsFactory");
Expand Down
24 changes: 11 additions & 13 deletions src/test/java/io/vertx/test/core/MetricsTest.java
Expand Up @@ -29,8 +29,6 @@
import io.vertx.core.net.NetSocket; import io.vertx.core.net.NetSocket;
import io.vertx.core.spi.metrics.PoolMetrics; import io.vertx.core.spi.metrics.PoolMetrics;
import io.vertx.test.fakemetrics.*; import io.vertx.test.fakemetrics.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;


import java.net.InetAddress; import java.net.InetAddress;
Expand Down Expand Up @@ -674,9 +672,9 @@ private void testDatagram(String host, Consumer<PacketMetric> checker) throws Ex


@Test @Test
public void testThreadPoolMetricsWithExecuteBlocking() { public void testThreadPoolMetricsWithExecuteBlocking() {
Map<String, PoolMetrics> all = FakeThreadPoolMetrics.getThreadPoolMetrics(); Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();


FakeThreadPoolMetrics metrics = (FakeThreadPoolMetrics) all.get("vert.x-worker-thread"); FakePoolMetrics metrics = (FakePoolMetrics) all.get("vert.x-worker-thread");


assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize())); assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize()));
assertThat(metrics.numberOfIdleThreads(), is(getOptions().getWorkerPoolSize())); assertThat(metrics.numberOfIdleThreads(), is(getOptions().getWorkerPoolSize()));
Expand Down Expand Up @@ -724,8 +722,8 @@ public void testThreadPoolMetricsWithExecuteBlocking() {
public void testThreadPoolMetricsWithInternalExecuteBlocking() { public void testThreadPoolMetricsWithInternalExecuteBlocking() {
// Internal blocking thread pool is used by blocking file system actions. // Internal blocking thread pool is used by blocking file system actions.


Map<String, PoolMetrics> all = FakeThreadPoolMetrics.getThreadPoolMetrics(); Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
FakeThreadPoolMetrics metrics = (FakeThreadPoolMetrics) all.get("vert.x-internal-blocking"); FakePoolMetrics metrics = (FakePoolMetrics) all.get("vert.x-internal-blocking");


assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize())); assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize()));
assertThat(metrics.numberOfIdleThreads(), is(getOptions().getInternalBlockingPoolSize())); assertThat(metrics.numberOfIdleThreads(), is(getOptions().getInternalBlockingPoolSize()));
Expand Down Expand Up @@ -785,8 +783,8 @@ public void testThreadPoolMetricsWithWorkerVerticleAndMultiThread() {


private void testWithWorkerVerticle(DeploymentOptions options) { private void testWithWorkerVerticle(DeploymentOptions options) {
AtomicInteger counter = new AtomicInteger(); AtomicInteger counter = new AtomicInteger();
Map<String, PoolMetrics> all = FakeThreadPoolMetrics.getThreadPoolMetrics(); Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
FakeThreadPoolMetrics metrics = (FakeThreadPoolMetrics) all.get("vert.x-worker-thread"); FakePoolMetrics metrics = (FakePoolMetrics) all.get("vert.x-worker-thread");


assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize())); assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize()));
assertThat(metrics.numberOfIdleThreads(), is(getOptions().getWorkerPoolSize())); assertThat(metrics.numberOfIdleThreads(), is(getOptions().getWorkerPoolSize()));
Expand Down Expand Up @@ -855,9 +853,9 @@ public void testThreadPoolMetricsWithNamedExecuteBlocking() {


WorkerExecutor workerExec = vertx.createSharedWorkerExecutor("my-pool", 10); WorkerExecutor workerExec = vertx.createSharedWorkerExecutor("my-pool", 10);


Map<String, PoolMetrics> all = FakeThreadPoolMetrics.getThreadPoolMetrics(); Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();


FakeThreadPoolMetrics metrics = (FakeThreadPoolMetrics) all.get("my-pool"); FakePoolMetrics metrics = (FakePoolMetrics) all.get("my-pool");


assertThat(metrics.getPoolSize(), is(10)); assertThat(metrics.getPoolSize(), is(10));
assertThat(metrics.numberOfIdleThreads(), is(10)); assertThat(metrics.numberOfIdleThreads(), is(10));
Expand Down Expand Up @@ -906,9 +904,9 @@ public void testWorkerPoolClose() {
WorkerExecutor ex1 = vertx.createSharedWorkerExecutor("ex1"); WorkerExecutor ex1 = vertx.createSharedWorkerExecutor("ex1");
WorkerExecutor ex1_ = vertx.createSharedWorkerExecutor("ex1"); WorkerExecutor ex1_ = vertx.createSharedWorkerExecutor("ex1");
WorkerExecutor ex2 = vertx.createSharedWorkerExecutor("ex2"); WorkerExecutor ex2 = vertx.createSharedWorkerExecutor("ex2");
Map<String, PoolMetrics> all = FakeThreadPoolMetrics.getThreadPoolMetrics(); Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
FakeThreadPoolMetrics metrics1 = (FakeThreadPoolMetrics) all.get("ex1"); FakePoolMetrics metrics1 = (FakePoolMetrics) all.get("ex1");
FakeThreadPoolMetrics metrics2 = (FakeThreadPoolMetrics) all.get("ex2"); FakePoolMetrics metrics2 = (FakePoolMetrics) all.get("ex2");
assertNotNull(metrics1); assertNotNull(metrics1);
assertNotNull(metrics2); assertNotNull(metrics2);
assertNotSame(metrics1, metrics2); assertNotSame(metrics1, metrics2);
Expand Down
Expand Up @@ -28,7 +28,7 @@
* *
* @author <a href="http://escoffier.me">Clement Escoffier</a> * @author <a href="http://escoffier.me">Clement Escoffier</a>
*/ */
public class FakeThreadPoolMetrics implements PoolMetrics<Void> { public class FakePoolMetrics implements PoolMetrics<Void> {
private final static Map<String, PoolMetrics> METRICS = new ConcurrentHashMap<>(); private final static Map<String, PoolMetrics> METRICS = new ConcurrentHashMap<>();


private final int poolSize; private final int poolSize;
Expand All @@ -41,7 +41,7 @@ public class FakeThreadPoolMetrics implements PoolMetrics<Void> {
private final String name; private final String name;
private final AtomicBoolean closed = new AtomicBoolean(); private final AtomicBoolean closed = new AtomicBoolean();


public FakeThreadPoolMetrics(String name, int poolSize) { public FakePoolMetrics(String name, int poolSize) {
this.poolSize = poolSize; this.poolSize = poolSize;
this.name = name; this.name = name;
this.idle.set(this.poolSize); this.idle.set(this.poolSize);
Expand Down Expand Up @@ -116,7 +116,7 @@ public int numberOfRunningTasks() {
return running.get(); return running.get();
} }


public static Map<String, PoolMetrics> getThreadPoolMetrics() { public static Map<String, PoolMetrics> getPoolMetrics() {
return METRICS; return METRICS;
} }


Expand Down
Expand Up @@ -136,7 +136,7 @@ public DatagramSocketMetrics createMetrics(DatagramSocket socket, DatagramSocket


@Override @Override
public <P> PoolMetrics<?> createMetrics(P pool, String poolType, String poolName, int maxPoolSize) { public <P> PoolMetrics<?> createMetrics(P pool, String poolType, String poolName, int maxPoolSize) {
return new FakeThreadPoolMetrics(poolName, maxPoolSize); return new FakePoolMetrics(poolName, maxPoolSize);
} }


public boolean isEnabled() { public boolean isEnabled() {
Expand Down

0 comments on commit 2bf96da

Please sign in to comment.