belliottsmith commented on code in PR #4324: URL: https://github.com/apache/cassandra/pull/4324#discussion_r2315706466
########## src/java/org/apache/cassandra/metrics/Timer.java: ########## @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.metrics; + +import java.time.Duration; +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import com.codahale.metrics.Clock; +import com.codahale.metrics.Metered; +import com.codahale.metrics.Sampling; + +/** + * An interface which mimics {@link com.codahale.metrics.Timer} API and allows alternative implementations + */ +public interface Timer extends Metered, Sampling +{ + /** + * A timing context. + */ + class Context implements AutoCloseable + { + private final Timer timer; + private final Clock clock; + private final long startTime; + + Context(Timer timer, Clock clock) + { + this.timer = timer; + this.clock = clock; + this.startTime = clock.getTick(); + } + + /** + * Updates the timer with the difference between current and start time. Call to this method will + * not reset the start time. Multiple calls result in multiple updates. + * + * @return the elapsed time in nanoseconds + */ + public long stop() + { + final long elapsed = clock.getTick() - startTime; + timer.update(elapsed, TimeUnit.NANOSECONDS); Review Comment: I don't think all of the possible clocks use NANOSECONDS. In fact, the abstract class Clock has its base method returning currentTimeMillis() (don't ask me why). We should perhaps require the units of the clock to be provided to the context, or we should have our own Clock that extends codahale's that tells us the granularity. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

