Skip to content

Commit 2c6aa9d

Browse files
author
Zhen
committed
Added histogram support
1 parent c58f606 commit 2c6aa9d

File tree

5 files changed

+221
-7
lines changed

5 files changed

+221
-7
lines changed

driver/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@
7171
<groupId>com.fasterxml.jackson.core</groupId>
7272
<artifactId>jackson-databind</artifactId>
7373
</dependency>
74+
<dependency>
75+
<groupId>org.hdrhistogram</groupId>
76+
<artifactId>HdrHistogram</artifactId>
77+
<version>2.1.2</version>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.hdrhistogram</groupId>
81+
<artifactId>HdrHistogram</artifactId>
82+
<version>2.1.2</version>
83+
</dependency>
7484
</dependencies>
7585

7686
<build>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2002-2018 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.metrics;
20+
21+
import org.neo4j.driver.internal.metrics.spi.Histogram;
22+
23+
public class HistogramSanpshot implements Histogram
24+
{
25+
private Histogram copy;
26+
private Histogram origin;
27+
28+
public HistogramSanpshot( Histogram copy, Histogram origin )
29+
{
30+
this.copy = copy;
31+
this.origin = origin;
32+
}
33+
34+
@Override
35+
public long max()
36+
{
37+
return copy.max();
38+
}
39+
40+
@Override
41+
public double mean()
42+
{
43+
return copy.mean();
44+
}
45+
46+
@Override
47+
public double stdDeviation()
48+
{
49+
return copy.stdDeviation();
50+
}
51+
52+
@Override
53+
public long totalCount()
54+
{
55+
return copy.totalCount();
56+
}
57+
58+
@Override
59+
public long valueAtPercentile( double percentile )
60+
{
61+
return copy.valueAtPercentile( percentile );
62+
}
63+
64+
@Override
65+
public void reset()
66+
{
67+
origin.reset();
68+
}
69+
70+
@Override
71+
public String toString()
72+
{
73+
return copy.toString();
74+
}
75+
}

driver/src/main/java/org/neo4j/driver/internal/metrics/InternalConnectionPoolMetrics.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ public class InternalConnectionPoolMetrics implements ConnectionPoolMetrics, Con
4040
private AtomicInteger toCreate = new AtomicInteger();
4141
private AtomicLong failedToCreate = new AtomicLong();
4242

43-
// TODO
44-
private Histogram histogram;
43+
private InternalHistogram histogram;
4544

4645
public InternalConnectionPoolMetrics(BoltServerAddress address, ConnectionPool pool, long connAcquisitionTimeoutMs)
4746
{
4847
this.address = address;
4948
this.pool = pool;
50-
this.histogram = null;
49+
this.histogram = new InternalHistogram( connAcquisitionTimeoutMs );
5150
}
5251

5352
@Override
@@ -86,7 +85,7 @@ public void beforeAcquire( ListenerEvent listenerEvent )
8685
public void afterAcquire( ListenerEvent listenerEvent )
8786
{
8887
long elapsed = listenerEvent.elapsed();
89-
// TODO register in histogram
88+
histogram.recordValue( elapsed );
9089
}
9190

9291
@Override
@@ -147,13 +146,13 @@ public long closed()
147146
@Override
148147
public Histogram acquisitionTimeHistogram()
149148
{
150-
return null;
149+
return this.histogram.snapshot();
151150
}
152151

153152
@Override
154153
public String toString()
155154
{
156-
return format( "[created=%s, closed=%s, toCreate=%s, failedToCreate=%s inUse=%s, idle=%s, poolStatus=%s]",
157-
created(), closed(), toCreate(), failedToCreate(), inUse(), idle(), poolStatus() );
155+
return format( "[created=%s, closed=%s, toCreate=%s, failedToCreate=%s inUse=%s, idle=%s, poolStatus=%s, acquisitionTimeHistogram=%n%s]",
156+
created(), closed(), toCreate(), failedToCreate(), inUse(), idle(), poolStatus(), acquisitionTimeHistogram() );
158157
}
159158
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2002-2018 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.metrics;
20+
21+
import org.HdrHistogram.AbstractHistogram;
22+
import org.HdrHistogram.ConcurrentHistogram;
23+
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.PrintStream;
26+
import java.time.Duration;
27+
28+
import org.neo4j.driver.internal.metrics.spi.Histogram;
29+
30+
public class InternalHistogram implements Histogram
31+
{
32+
private static final long DEFAULT_HIGHEST_TRACKABLE_MS = Duration.ofMinutes( 10 ).toMillis();
33+
34+
private final AbstractHistogram delegate;
35+
36+
public InternalHistogram()
37+
{
38+
this( DEFAULT_HIGHEST_TRACKABLE_MS );
39+
}
40+
41+
public InternalHistogram( long highestTrackableValue )
42+
{
43+
this.delegate = createHdrHistogram( highestTrackableValue );
44+
}
45+
46+
public InternalHistogram( AbstractHistogram histogram )
47+
{
48+
this.delegate = histogram;
49+
}
50+
51+
public void recordValue( long value )
52+
{
53+
long newValue = truncateValue( value, delegate );
54+
this.delegate.recordValue( newValue );
55+
}
56+
57+
@Override
58+
public long max()
59+
{
60+
return delegate.getMaxValue();
61+
}
62+
63+
@Override
64+
public double mean()
65+
{
66+
return delegate.getMean();
67+
}
68+
69+
@Override
70+
public double stdDeviation()
71+
{
72+
return delegate.getStdDeviation();
73+
}
74+
75+
@Override
76+
public long totalCount()
77+
{
78+
return delegate.getTotalCount();
79+
}
80+
81+
@Override
82+
public long valueAtPercentile( double percentile )
83+
{
84+
return delegate.getValueAtPercentile( percentile );
85+
}
86+
87+
@Override
88+
public void reset()
89+
{
90+
delegate.reset();
91+
}
92+
93+
public static ConcurrentHistogram createHdrHistogram( long highestTrackableValue )
94+
{
95+
return new ConcurrentHistogram( highestTrackableValue, 3 );
96+
}
97+
98+
private static long truncateValue( long value, AbstractHistogram histogram )
99+
{
100+
if ( value > histogram.getHighestTrackableValue() )
101+
{
102+
return histogram.getHighestTrackableValue();
103+
}
104+
else
105+
{
106+
return value;
107+
}
108+
}
109+
110+
public Histogram snapshot()
111+
{
112+
return new HistogramSanpshot( new InternalHistogram( this.delegate.copy() ), this );
113+
}
114+
115+
@Override
116+
public String toString()
117+
{
118+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
119+
PrintStream writer = new PrintStream( stream );
120+
delegate.outputPercentileDistribution( writer, 1.0 );
121+
String content = new String( stream.toByteArray() );
122+
writer.close();
123+
return content;
124+
}
125+
}

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
<artifactId>netty-handler</artifactId>
5757
<version>4.1.16.Final</version>
5858
</dependency>
59+
<dependency>
60+
<groupId>org.hdrhistogram</groupId>
61+
<artifactId>hdrHistogram</artifactId>
62+
<version>2.1.10</version>
63+
</dependency>
5964

6065
<!-- Test dependencies -->
6166
<dependency>

0 commit comments

Comments
 (0)