How to use Stopwatch class of org.junit.rules package

Best junit code snippet using org.junit.rules.Stopwatch

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package org.junit.rules;
2
3import java.util.concurrent.TimeUnit;
4import org.junit.AssumptionViolatedException;
5import org.junit.runner.Description;
6import org.junit.runners.model.Statement;
7
8public abstract class Stopwatch implements TestRule {
9    private final Clock clock;
10    private volatile long endNanos;
11    private volatile long startNanos;
12
13    public Stopwatch() {
14        this(new Clock());
15    }
16
17    Stopwatch(Clock clock2) {
18        this.clock = clock2;
19    }
20
21    public long runtime(TimeUnit unit) {
22        return unit.convert(getNanos(), TimeUnit.NANOSECONDS);
23    }
24
25    /* access modifiers changed from: protected */
26    public void succeeded(long nanos, Description description) {
27    }
28
29    /* access modifiers changed from: protected */
30    public void failed(long nanos, Throwable e, Description description) {
31    }
32
33    /* access modifiers changed from: protected */
34    public void skipped(long nanos, AssumptionViolatedException e, Description description) {
35    }
36
37    /* access modifiers changed from: protected */
38    public void finished(long nanos, Description description) {
39    }
40
41    /* access modifiers changed from: private */
42    /* access modifiers changed from: public */
43    private long getNanos() {
44        if (this.startNanos != 0) {
45            long currentEndNanos = this.endNanos;
46            if (currentEndNanos == 0) {
47                currentEndNanos = this.clock.nanoTime();
48            }
49            return currentEndNanos - this.startNanos;
50        }
51        throw new IllegalStateException("Test has not started");
52    }
53
54    /* access modifiers changed from: private */
55    /* access modifiers changed from: public */
56    private void starting() {
57        this.startNanos = this.clock.nanoTime();
58        this.endNanos = 0;
59    }
60
61    /* access modifiers changed from: private */
62    /* access modifiers changed from: public */
63    private void stopping() {
64        this.endNanos = this.clock.nanoTime();
65    }
66
67    @Override // org.junit.rules.TestRule
68    public final Statement apply(Statement base, Description description) {
69        return new InternalWatcher().apply(base, description);
70    }
71
72    private class InternalWatcher extends TestWatcher {
73        private InternalWatcher() {
74        }
75
76        /* access modifiers changed from: protected */
77        @Override // org.junit.rules.TestWatcher
78        public void starting(Description description) {
79            Stopwatch.this.starting();
80        }
81
82        /* access modifiers changed from: protected */
83        @Override // org.junit.rules.TestWatcher
84        public void finished(Description description) {
85            Stopwatch stopwatch = Stopwatch.this;
86            stopwatch.finished(stopwatch.getNanos(), description);
87        }
88
89        /* access modifiers changed from: protected */
90        @Override // org.junit.rules.TestWatcher
91        public void succeeded(Description description) {
92            Stopwatch.this.stopping();
93            Stopwatch stopwatch = Stopwatch.this;
94            stopwatch.succeeded(stopwatch.getNanos(), description);
95        }
96
97        /* access modifiers changed from: protected */
98        @Override // org.junit.rules.TestWatcher
99        public void failed(Throwable e, Description description) {
100            Stopwatch.this.stopping();
101            Stopwatch stopwatch = Stopwatch.this;
102            stopwatch.failed(stopwatch.getNanos(), e, description);
103        }
104
105        /* access modifiers changed from: protected */
106        @Override // org.junit.rules.TestWatcher
107        public void skipped(AssumptionViolatedException e, Description description) {
108            Stopwatch.this.stopping();
109            Stopwatch stopwatch = Stopwatch.this;
110            stopwatch.skipped(stopwatch.getNanos(), e, description);
111        }
112    }
113
114    /* access modifiers changed from: package-private */
115    public static class Clock {
116        Clock() {
117        }
118
119        public long nanoTime() {
120            return System.nanoTime();
121        }
122    }
123}
124
Full Screen
copy
1long startTime = System.nanoTime();
2//code here...
3System.out.println("Code took "+(System.nanoTime()-startTime)+"nano seconds");
4
Full Screen
copy
1final long endTime, startTime = System.nanoTime();
2//code here...
3endTime = System.nanoTime();
4System.out.println("Code took "+(endTime-startTime)+"nano seconds");
5
Full Screen
copy
1for(i=1..n)
2  alg1();
3for(i=1..n)
4  alg2();
5for(i=1..n)
6  alg2();
7for(i=1..n)
8  alg1();
9
Full Screen
copy
1long start1 = System.nanoTime();
2// maybe add here a call to a return to remove call up time, too.
3// Avoid optimization 
4long start2 = System.nanoTime();
5myCall(); 
6long stop = System.nanoTime();
7long diff = stop - 2*start2 + start1;
8
9System.out.println(diff + " ns");
10
Full Screen
copy
1public class MyService { 
2    public void doSomething() {
3        for (int i = 1; i < 10000; i++) {
4            System.out.println("i=" + i);
5        }
6    }
7}
8
Full Screen
copy
1public class ServiceMethodInterceptor implements MethodInterceptor {
2    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
3        long startTime = System.currentTimeMillis();
4        Object result = methodInvocation.proceed();
5        long duration = System.currentTimeMillis() - startTime;
6        Method method = methodInvocation.getMethod();
7        String methodName = method.getDeclaringClass().getName() + "." + method.getName();
8        System.out.println("Method '" + methodName + "' took " + duration + " milliseconds to run");
9        return null;
10    }
11}
12
Full Screen
copy
1long start = System.currentTimeMillis();
2class.method();
3long time = System.currentTimeMillis() - start;
4
Full Screen
copy
1class TimeTest1 {
2   public static void main(String[] args) {
3
4      long startTime = System.currentTimeMillis();
5
6      long total = 0;
7      for (int i = 0; i < 10000000; i++) {
8         total += i;
9      }
10
11      long stopTime = System.currentTimeMillis();
12      long elapsedTime = stopTime - startTime;
13      System.out.println(elapsedTime);
14   }
15}
16
Full Screen
copy
1class TimeTest2 {
2   public static void main(String[] args) {
3
4      Stopwatch timer = new Stopwatch().start();
5
6      long total = 0;
7      for (int i = 0; i < 10000000; i++) {
8         total += i;
9      }
10
11      timer.stop();
12      System.out.println(timer.getElapsedTime());
13   }
14}
15
Full Screen
copy
1long startTime = System.nanoTime();
2myCall(); 
3long stopTime = System.nanoTime();
4System.out.println(stopTime - startTime);
5
Full Screen
copy
1Instant start = Instant.now();
2Thread.sleep(63553);
3Instant end = Instant.now();
4System.out.println(Duration.between(start, end)); // prints PT1M3.553S
5
Full Screen
copy
1Stopwatch stopwatch = Stopwatch.createStarted();
2myCall();
3stopwatch.stop(); // optional
4System.out.println("Time elapsed: "+ stopwatch.elapsed(TimeUnit.MILLISECONDS));
5
Full Screen
copy
1long start1 = System.nanoTime();
2// maybe add here a call to a return to remove call up time, too.
3// Avoid optimization 
4long start2 = System.nanoTime();
5myCall(); 
6long stop = System.nanoTime();
7long diff = stop - 2*start2 + start1;
8
9System.out.println(diff + " ns");
10
Full Screen
copy
1public class MyService { 
2    public void doSomething() {
3        for (int i = 1; i < 10000; i++) {
4            System.out.println("i=" + i);
5        }
6    }
7}
8
Full Screen
copy
1public class ServiceMethodInterceptor implements MethodInterceptor {
2    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
3        long startTime = System.currentTimeMillis();
4        Object result = methodInvocation.proceed();
5        long duration = System.currentTimeMillis() - startTime;
6        Method method = methodInvocation.getMethod();
7        String methodName = method.getDeclaringClass().getName() + "." + method.getName();
8        System.out.println("Method '" + methodName + "' took " + duration + " milliseconds to run");
9        return null;
10    }
11}
12
Full Screen
copy
1long start = System.currentTimeMillis();
2class.method();
3long time = System.currentTimeMillis() - start;
4
Full Screen
copy
1class TimeTest1 {
2   public static void main(String[] args) {
3
4      long startTime = System.currentTimeMillis();
5
6      long total = 0;
7      for (int i = 0; i < 10000000; i++) {
8         total += i;
9      }
10
11      long stopTime = System.currentTimeMillis();
12      long elapsedTime = stopTime - startTime;
13      System.out.println(elapsedTime);
14   }
15}
16
Full Screen
copy
1class TimeTest2 {
2   public static void main(String[] args) {
3
4      Stopwatch timer = new Stopwatch().start();
5
6      long total = 0;
7      for (int i = 0; i < 10000000; i++) {
8         total += i;
9      }
10
11      timer.stop();
12      System.out.println(timer.getElapsedTime());
13   }
14}
15
Full Screen
copy
1long startTime = System.nanoTime();
2myCall(); 
3long stopTime = System.nanoTime();
4System.out.println(stopTime - startTime);
5
Full Screen
copy
1Instant start = Instant.now();
2Thread.sleep(63553);
3Instant end = Instant.now();
4System.out.println(Duration.between(start, end)); // prints PT1M3.553S
5
Full Screen
copy
1Stopwatch stopwatch = Stopwatch.createStarted();
2myCall();
3stopwatch.stop(); // optional
4System.out.println("Time elapsed: "+ stopwatch.elapsed(TimeUnit.MILLISECONDS));
5
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in Stopwatch

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)