Add TimerContext to wrap timer metrics
TimerContext wraps the context used in Timer{0|1|2|3} and allows to get the start time and elapsed time. Update the replication plugin to use it. Change-Id: I194b9a7bd6a00bc2841ac37ca8654f0b4a2dda00
This commit is contained in:
parent
e92349a607
commit
d45f092579
@ -31,22 +31,22 @@ import java.util.concurrent.TimeUnit;
|
||||
* </pre>
|
||||
*/
|
||||
public abstract class Timer0 implements RegistrationHandle {
|
||||
public class Context implements AutoCloseable {
|
||||
private final long startNanos;
|
||||
public static class Context extends TimerContext {
|
||||
private final Timer0 timer;
|
||||
|
||||
Context() {
|
||||
this.startNanos = System.nanoTime();
|
||||
Context(Timer0 timer) {
|
||||
this.timer = timer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
record(System.nanoTime() - startNanos, NANOSECONDS);
|
||||
public void record(long elapsed) {
|
||||
timer.record(elapsed, NANOSECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
/** Begin a timer for the current block, value will be recorded when closed. */
|
||||
public Context start() {
|
||||
return new Context();
|
||||
return new Context(this);
|
||||
}
|
||||
|
||||
/** Record a value in the distribution. */
|
||||
|
@ -33,21 +33,19 @@ import java.util.concurrent.TimeUnit;
|
||||
* @param <F1> type of the field.
|
||||
*/
|
||||
public abstract class Timer1<F1> implements RegistrationHandle {
|
||||
public static class Context implements AutoCloseable {
|
||||
public static class Context extends TimerContext {
|
||||
private final Timer1<Object> timer;
|
||||
private final Object field1;
|
||||
private final long startNanos;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
<F1> Context(Timer1<F1> timer, F1 field1) {
|
||||
this.timer = (Timer1<Object>) timer;
|
||||
this.field1 = field1;
|
||||
this.startNanos = System.nanoTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
timer.record(field1, System.nanoTime() - startNanos, NANOSECONDS);
|
||||
public void record(long elapsed) {
|
||||
timer.record(field1, elapsed, NANOSECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,23 +34,21 @@ import java.util.concurrent.TimeUnit;
|
||||
* @param <F2> type of the field.
|
||||
*/
|
||||
public abstract class Timer2<F1, F2> implements RegistrationHandle {
|
||||
public static class Context implements AutoCloseable {
|
||||
public static class Context extends TimerContext {
|
||||
private final Timer2<Object, Object> timer;
|
||||
private final Object field1;
|
||||
private final Object field2;
|
||||
private final long startNanos;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
<F1, F2> Context(Timer2<F1, F2> timer, F1 field1, F2 field2) {
|
||||
this.timer = (Timer2<Object, Object>) timer;
|
||||
this.field1 = field1;
|
||||
this.field2 = field2;
|
||||
this.startNanos = System.nanoTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
timer.record(field1, field2, System.nanoTime() - startNanos, NANOSECONDS);
|
||||
public void record(long elapsed) {
|
||||
timer.record(field1, field2, elapsed, NANOSECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,12 +35,11 @@ import java.util.concurrent.TimeUnit;
|
||||
* @param <F3> type of the field.
|
||||
*/
|
||||
public abstract class Timer3<F1, F2, F3> implements RegistrationHandle {
|
||||
public static class Context implements AutoCloseable {
|
||||
public static class Context extends TimerContext {
|
||||
private final Timer3<Object, Object, Object> timer;
|
||||
private final Object field1;
|
||||
private final Object field2;
|
||||
private final Object field3;
|
||||
private final long startNanos;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
<F1, F2, F3> Context(Timer3<F1, F2, F3> timer, F1 f1, F2 f2, F3 f3) {
|
||||
@ -48,13 +47,11 @@ public abstract class Timer3<F1, F2, F3> implements RegistrationHandle {
|
||||
this.field1 = f1;
|
||||
this.field2 = f2;
|
||||
this.field3 = f3;
|
||||
this.startNanos = System.nanoTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
timer.record(field1, field2, field3,
|
||||
System.nanoTime() - startNanos, NANOSECONDS);
|
||||
public void record(long elapsed) {
|
||||
timer.record(field1, field2, field3, elapsed, NANOSECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,59 @@
|
||||
// Copyright (C) 2015 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.metrics;
|
||||
|
||||
abstract class TimerContext implements AutoCloseable {
|
||||
private final long startNanos;
|
||||
private boolean stopped;
|
||||
|
||||
TimerContext() {
|
||||
this.startNanos = System.nanoTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Record the elapsed time to the timer.
|
||||
*
|
||||
* @param elapsed Elapsed time in nanoseconds.
|
||||
*/
|
||||
public abstract void record(long elapsed);
|
||||
|
||||
/** Get the start time in system time nanoseconds. */
|
||||
public long getStartTime() {
|
||||
return startNanos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the timer and record the elapsed time.
|
||||
*
|
||||
* @return the elapsed time in nanoseconds.
|
||||
* @throws IllegalStateException if the timer is already stopped.
|
||||
*/
|
||||
public long stop() {
|
||||
if (!stopped) {
|
||||
stopped = true;
|
||||
long elapsed = System.nanoTime() - startNanos;
|
||||
record(elapsed);
|
||||
return elapsed;
|
||||
}
|
||||
throw new IllegalStateException("Already stopped");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (!stopped) {
|
||||
stop();
|
||||
}
|
||||
}
|
||||
}
|
@ -1 +1 @@
|
||||
Subproject commit 168032cb1efadb83db5da4fd0060c94c9e05ccfa
|
||||
Subproject commit 4ab29b755a147a69d88bf000e276a7a2eaa6403b
|
Loading…
Reference in New Issue
Block a user