Skip to content

Commit

Permalink
Add total count of threads running on the JVM
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegrohmann committed Mar 11, 2016
1 parent e79adb4 commit 61a30ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

import static com.codahale.metrics.MetricRegistry.name;

public class ThreadMetrics extends JvmMetrics
{
public static final String THREAD_COUNT = name( JvmMetrics.NAME_PREFIX, "thread.count" );
public static final String THREAD_TOTAL = name( JvmMetrics.NAME_PREFIX, "thread.total" );

private final MetricRegistry registry;
private final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

public ThreadMetrics( MetricRegistry registry )
{
Expand All @@ -39,12 +44,14 @@ public ThreadMetrics( MetricRegistry registry )
public void start()
{
registry.register( THREAD_COUNT, (Gauge<Integer>) Thread::activeCount );
registry.register( THREAD_TOTAL, (Gauge<Integer>) threadMXBean::getThreadCount );
}

@Override
public void stop()
{
registry.remove( THREAD_COUNT );
registry.remove( THREAD_TOTAL );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
*/
package org.neo4j.metrics;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Map;
import java.util.UUID;
import java.util.function.BiPredicate;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
Expand All @@ -43,18 +43,17 @@
import org.neo4j.metrics.source.db.CypherMetrics;
import org.neo4j.metrics.source.db.EntityCountMetrics;
import org.neo4j.metrics.source.db.TransactionMetrics;
import org.neo4j.metrics.source.jvm.ThreadMetrics;
import org.neo4j.test.ha.ClusterRule;

import static java.util.concurrent.TimeUnit.SECONDS;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import static org.neo4j.graphdb.factory.GraphDatabaseSettings.check_point_interval_time;
import static org.neo4j.graphdb.factory.GraphDatabaseSettings.cypher_min_replan_interval;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
Expand Down Expand Up @@ -198,6 +197,24 @@ public void shouldUseEventBasedReportingCorrectly() throws Throwable
assertThat( result, greaterThanOrEqualTo( 0L ) );
}

@Test
public void shouldShowMetricsForThreads() throws Throwable
{
// WHEN
addNodes( 100 );

// wait for the file to be written before shutting down the cluster
File threadTotalFile = new File( outputPath, ThreadMetrics.THREAD_TOTAL + ".csv" );
File threadCountFile = new File( outputPath, ThreadMetrics.THREAD_COUNT + ".csv" );

long threadTotalResult = readLongValueAndAssert( threadTotalFile, ( newValue, currentValue ) -> newValue >= 0 );
long threadCountResult = readLongValueAndAssert( threadCountFile, ( newValue, currentValue ) -> newValue >= 0 );

// THEN
assertThat( threadTotalResult, greaterThanOrEqualTo( 0L ) );
assertThat( threadCountResult, greaterThanOrEqualTo( 0L ) );
}

private void addNodes( int numberOfNodes )
{
for ( int i = 0; i < numberOfNodes; i++ )
Expand Down

0 comments on commit 61a30ae

Please sign in to comment.