Search in sources :

Example 6 with Metric

use of org.springframework.boot.actuate.metrics.Metric in project spring-boot by spring-projects.

the class DropwizardCounterServiceSpeedTests method counters.

@Theory
public void counters(String input) throws Exception {
    watch.start("counters" + count++);
    ExecutorService pool = Executors.newFixedThreadPool(threadCount);
    Runnable task = new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < number; i++) {
                String name = sample[i % sample.length];
                DropwizardCounterServiceSpeedTests.this.counterService.increment(name);
            }
        }
    };
    Collection<Future<?>> futures = new HashSet<>();
    for (int i = 0; i < threadCount; i++) {
        futures.add(pool.submit(task));
    }
    for (Future<?> future : futures) {
        future.get();
    }
    watch.stop();
    double rate = number / watch.getLastTaskTimeMillis() * 1000;
    System.err.println("Counters rate(" + count + ")=" + rate + ", " + watch);
    watch.start("read" + count);
    this.reader.findAll().forEach(new Consumer<Metric<?>>() {

        @Override
        public void accept(Metric<?> metric) {
            err.println(metric);
        }
    });
    final LongAdder total = new LongAdder();
    this.reader.findAll().forEach(new Consumer<Metric<?>>() {

        @Override
        public void accept(Metric<?> value) {
            total.add(value.getValue().intValue());
        }
    });
    watch.stop();
    System.err.println("Read(" + count + ")=" + watch.getLastTaskTimeMillis() + "ms");
    assertThat(total.longValue()).isEqualTo(number * threadCount);
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Metric(org.springframework.boot.actuate.metrics.Metric) HashSet(java.util.HashSet) Theory(org.junit.experimental.theories.Theory)

Example 7 with Metric

use of org.springframework.boot.actuate.metrics.Metric in project spring-boot by spring-projects.

the class RichGaugeReaderPublicMetricsTests method testMetrics.

@Test
public void testMetrics() throws Exception {
    InMemoryRichGaugeRepository repository = new InMemoryRichGaugeRepository();
    repository.set(new Metric<>("a", 0.d, new Date()));
    repository.set(new Metric<>("a", 0.5d, new Date()));
    RichGaugeReaderPublicMetrics metrics = new RichGaugeReaderPublicMetrics(repository);
    Map<String, Metric<?>> results = new HashMap<>();
    for (Metric<?> metric : metrics.metrics()) {
        results.put(metric.getName(), metric);
    }
    assertThat(results.containsKey("a.val")).isTrue();
    assertThat(results.get("a.val").getValue().doubleValue()).isEqualTo(0.5d);
    assertThat(results.containsKey("a.avg")).isTrue();
    assertThat(results.get("a.avg").getValue().doubleValue()).isEqualTo(0.25d);
    assertThat(results.containsKey("a.min")).isTrue();
    assertThat(results.get("a.min").getValue().doubleValue()).isEqualTo(0.0d);
    assertThat(results.containsKey("a.max")).isTrue();
    assertThat(results.get("a.max").getValue().doubleValue()).isEqualTo(0.5d);
    assertThat(results.containsKey("a.count")).isTrue();
    assertThat(results.get("a.count").getValue().longValue()).isEqualTo(2L);
    assertThat(results.containsKey("a.alpha")).isTrue();
    assertThat(results.get("a.alpha").getValue().doubleValue()).isEqualTo(-1.d);
}
Also used : HashMap(java.util.HashMap) InMemoryRichGaugeRepository(org.springframework.boot.actuate.metrics.rich.InMemoryRichGaugeRepository) Metric(org.springframework.boot.actuate.metrics.Metric) Date(java.util.Date) Test(org.junit.Test)

Example 8 with Metric

use of org.springframework.boot.actuate.metrics.Metric in project spring-boot by spring-projects.

the class PublicMetricsAutoConfigurationTests method richGaugePublicMetrics.

@Test
public void richGaugePublicMetrics() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(RichGaugeReaderConfig.class, MetricRepositoryAutoConfiguration.class, PublicMetricsAutoConfiguration.class);
    RichGaugeReader richGaugeReader = context.getBean(RichGaugeReader.class);
    assertThat(richGaugeReader).isNotNull();
    given(richGaugeReader.findAll()).willReturn(Collections.singletonList(new RichGauge("bar", 3.7d)));
    RichGaugeReaderPublicMetrics publicMetrics = context.getBean(RichGaugeReaderPublicMetrics.class);
    assertThat(publicMetrics).isNotNull();
    Collection<Metric<?>> metrics = publicMetrics.metrics();
    assertThat(metrics).isNotNull();
    assertThat(6).isEqualTo(metrics.size());
    assertHasMetric(metrics, new Metric<>("bar.val", 3.7d));
    assertHasMetric(metrics, new Metric<>("bar.avg", 3.7d));
    assertHasMetric(metrics, new Metric<>("bar.min", 3.7d));
    assertHasMetric(metrics, new Metric<>("bar.max", 3.7d));
    assertHasMetric(metrics, new Metric<>("bar.alpha", -1.d));
    assertHasMetric(metrics, new Metric<>("bar.count", 1L));
    context.close();
}
Also used : RichGaugeReaderPublicMetrics(org.springframework.boot.actuate.endpoint.RichGaugeReaderPublicMetrics) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) RichGauge(org.springframework.boot.actuate.metrics.rich.RichGauge) RichGaugeReader(org.springframework.boot.actuate.metrics.rich.RichGaugeReader) Metric(org.springframework.boot.actuate.metrics.Metric) Test(org.junit.Test)

Example 9 with Metric

use of org.springframework.boot.actuate.metrics.Metric in project spring-boot by spring-projects.

the class PublicMetricsAutoConfigurationTests method multipleDataSourcesWithPrimary.

@Test
public void multipleDataSourcesWithPrimary() {
    load(MultipleDataSourcesWithPrimaryConfig.class);
    PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class);
    Collection<Metric<?>> metrics = bean.metrics();
    assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage", "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
}
Also used : Metric(org.springframework.boot.actuate.metrics.Metric) RichGaugeReaderPublicMetrics(org.springframework.boot.actuate.endpoint.RichGaugeReaderPublicMetrics) MetricReaderPublicMetrics(org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics) CachePublicMetrics(org.springframework.boot.actuate.endpoint.CachePublicMetrics) DataSourcePublicMetrics(org.springframework.boot.actuate.endpoint.DataSourcePublicMetrics) TomcatPublicMetrics(org.springframework.boot.actuate.endpoint.TomcatPublicMetrics) PublicMetrics(org.springframework.boot.actuate.endpoint.PublicMetrics) SystemPublicMetrics(org.springframework.boot.actuate.endpoint.SystemPublicMetrics) Test(org.junit.Test)

Example 10 with Metric

use of org.springframework.boot.actuate.metrics.Metric in project spring-boot by spring-projects.

the class PublicMetricsAutoConfigurationTests method multipleDataSources.

@Test
public void multipleDataSources() {
    load(MultipleDataSourcesConfig.class);
    PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class);
    Collection<Metric<?>> metrics = bean.metrics();
    assertMetrics(metrics, "datasource.tomcat.active", "datasource.tomcat.usage", "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
    // Hikari won't work unless a first connection has been retrieved
    JdbcTemplate jdbcTemplate = new JdbcTemplate(this.context.getBean("hikariDS", DataSource.class));
    jdbcTemplate.execute(new ConnectionCallback<Void>() {

        @Override
        public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
            return null;
        }
    });
    Collection<Metric<?>> anotherMetrics = bean.metrics();
    assertMetrics(anotherMetrics, "datasource.tomcat.active", "datasource.tomcat.usage", "datasource.hikariDS.active", "datasource.hikariDS.usage", "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) Metric(org.springframework.boot.actuate.metrics.Metric) RichGaugeReaderPublicMetrics(org.springframework.boot.actuate.endpoint.RichGaugeReaderPublicMetrics) MetricReaderPublicMetrics(org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics) CachePublicMetrics(org.springframework.boot.actuate.endpoint.CachePublicMetrics) DataSourcePublicMetrics(org.springframework.boot.actuate.endpoint.DataSourcePublicMetrics) TomcatPublicMetrics(org.springframework.boot.actuate.endpoint.TomcatPublicMetrics) PublicMetrics(org.springframework.boot.actuate.endpoint.PublicMetrics) SystemPublicMetrics(org.springframework.boot.actuate.endpoint.SystemPublicMetrics) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException) DataSource(javax.sql.DataSource) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Test(org.junit.Test)

Aggregations

Metric (org.springframework.boot.actuate.metrics.Metric)24 Test (org.junit.Test)11 CachePublicMetrics (org.springframework.boot.actuate.endpoint.CachePublicMetrics)7 RichGaugeReaderPublicMetrics (org.springframework.boot.actuate.endpoint.RichGaugeReaderPublicMetrics)6 LongAdder (java.util.concurrent.atomic.LongAdder)5 Theory (org.junit.experimental.theories.Theory)5 DataSourcePublicMetrics (org.springframework.boot.actuate.endpoint.DataSourcePublicMetrics)5 MetricReaderPublicMetrics (org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics)5 PublicMetrics (org.springframework.boot.actuate.endpoint.PublicMetrics)5 SystemPublicMetrics (org.springframework.boot.actuate.endpoint.SystemPublicMetrics)5 TomcatPublicMetrics (org.springframework.boot.actuate.endpoint.TomcatPublicMetrics)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 HashSet (java.util.HashSet)3 ExecutorService (java.util.concurrent.ExecutorService)3 Future (java.util.concurrent.Future)3 HashMap (java.util.HashMap)2 DataSource (javax.sql.DataSource)2 RichGauge (org.springframework.boot.actuate.metrics.rich.RichGauge)2 Counter (com.codahale.metrics.Counter)1