Copilot commented on code in PR #2654:
URL: https://github.com/apache/groovy/pull/2654#discussion_r3510876244
##########
src/test/groovy/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.groovy:
##########
@@ -43,4 +43,37 @@ class DefaultGroovyStaticMethodsTest {
void testAllThreads() {
assert Thread.allThreads().stream().anyMatch(t -> 'Finalizer' ==
t.name)
}
+
+ @Test
+ void testTimedNanos() {
+ long elapsed = System.timedNanos { (1..1000).sum() }
+ assert elapsed >= 0
+ }
Review Comment:
`testTimedNanos` only asserts the returned value is non-negative; it doesn't
verify the closure actually ran. A broken implementation that returns 0 without
invoking the closure would still pass this test.
##########
src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java:
##########
@@ -209,6 +210,87 @@ public static void sleep(Object self, long milliseconds,
Closure onInterrupt) {
sleepImpl(milliseconds, onInterrupt);
}
+ /**
+ * Executes the given closure and returns the elapsed time in nanoseconds.
+ * <p>
+ * Timing uses {@link System#nanoTime()}, which is monotonic and
unaffected by
+ * wall-clock adjustments. The closure is executed synchronously on the
calling
+ * thread and any exception it throws propagates unchanged (the elapsed
time is
+ * not reported in that case). Its result, if any, is discarded; use
+ * {@link #timed(System, Closure)} when the result is also needed.
+ * <pre class="groovyTestCase">
+ * long elapsed = System.timedNanos { (1..1_000).sum() }
+ * assert elapsed >= 0
+ * </pre>
+ *
+ * @param self placeholder variable used by Groovy categories; ignored for
default static methods
+ * @param code the closure to execute and time
+ * @return the elapsed time in nanoseconds
+ * @see #timedMillis(System, Closure)
+ * @see #timed(System, Closure)
+ * @since 6.0.0
+ */
+ public static long timedNanos(System self, Closure<?> code) {
+ long start = System.nanoTime();
+ code.call();
+ return System.nanoTime() - start;
+ }
+
+ /**
+ * Executes the given closure and returns the elapsed time in whole
milliseconds.
+ * <p>
+ * This is a convenience for {@code timedNanos(code) / 1_000_000}. Timing
uses
+ * {@link System#nanoTime()}, which is monotonic and unaffected by
wall-clock
+ * adjustments. The closure is executed synchronously on the calling
thread and
+ * any exception it throws propagates unchanged. Its result, if any, is
+ * discarded; use {@link #timed(System, Closure)} when the result is also
needed.
+ * <pre class="groovyTestCase">
+ * long elapsed = System.timedMillis { (1..1_000).sum() }
+ * assert elapsed >= 0
+ * </pre>
+ *
+ * @param self placeholder variable used by Groovy categories; ignored for
default static methods
+ * @param code the closure to execute and time
+ * @return the elapsed time in milliseconds, truncated toward zero
+ * @see #timedNanos(System, Closure)
+ * @see #timed(System, Closure)
+ * @since 6.0.0
+ */
+ public static long timedMillis(System self, Closure<?> code) {
+ long start = System.nanoTime();
+ code.call();
+ return (System.nanoTime() - start) / 1_000_000;
+ }
Review Comment:
`timedMillis` duplicates the same timing logic as `timedNanos`. Implementing
it in terms of `timedNanos(self, code)` reduces duplication and ensures the
methods can't drift apart if future changes (e.g. additional validation) are
made.
##########
src/test/groovy/org/codehaus/groovy/runtime/DefaultGroovyStaticMethodsTest.groovy:
##########
@@ -43,4 +43,37 @@ class DefaultGroovyStaticMethodsTest {
void testAllThreads() {
assert Thread.allThreads().stream().anyMatch(t -> 'Finalizer' ==
t.name)
}
+
+ @Test
+ void testTimedNanos() {
+ long elapsed = System.timedNanos { (1..1000).sum() }
+ assert elapsed >= 0
+ }
+
+ @Test
+ void testTimedMillis() {
+ long elapsed = System.timedMillis { (1..1000).sum() }
+ assert elapsed >= 0
+ }
Review Comment:
`testTimedMillis` only asserts the returned value is non-negative; it
doesn't verify the closure actually ran. A broken implementation that returns 0
without invoking the closure would still pass this test.
--
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]