Skip to content

Commit

Permalink
time a process
Browse files Browse the repository at this point in the history
  • Loading branch information
heySourabh committed Feb 1, 2024
1 parent 88a0355 commit d4361ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/main/util/StopWatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ public Duration stop() {
return Duration.ofMillis(System.currentTimeMillis() - this.startMillis);
}
}

public static Duration timeIt(Runnable process) {
StopWatch stopWatch = StopWatch.start();
process.run();
return stopWatch.stop();
}
}
40 changes: 25 additions & 15 deletions test/main/util/StopWatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,74 @@ public class StopWatchTest {
@Test
public void test_stopwatch() {
StopWatch stopWatch = StopWatch.start();
LockSupport.parkNanos(Duration.ofMillis(100).toNanos());
simulateRunningProcessForMillis(100);
assertEquals(stopWatch.stop().toMillis() - 100, 0, 5);
}

@Test
public void test_pause() {
StopWatch stopWatch = StopWatch.start();
LockSupport.parkNanos(Duration.ofMillis(100).toNanos());
simulateRunningProcessForMillis(100);
stopWatch.pauseWatch();
LockSupport.parkNanos(Duration.ofMillis(50).toNanos()); // does not matter as the watch is paused
simulateRunningProcessForMillis(50); // does not matter as the watch is paused
assertEquals(stopWatch.stop().toMillis() - 100, 0, 5);
}

@Test
public void test_pause_continue() {
StopWatch stopWatch = StopWatch.start();
LockSupport.parkNanos(Duration.ofMillis(100).toNanos()); // run
simulateRunningProcessForMillis(100); // run
stopWatch.pauseWatch();
LockSupport.parkNanos(Duration.ofMillis(50).toNanos()); // pause for this much time
simulateRunningProcessForMillis(50); // pause for this much time
stopWatch.continueWatch();
LockSupport.parkNanos(Duration.ofMillis(25).toNanos()); // again run for this much time
simulateRunningProcessForMillis(25); // again run for this much time
assertEquals(stopWatch.stop().toMillis() - (100 + 25), 0, 5);
}

@Test
public void test_pause_continue_pause() {
StopWatch stopWatch = StopWatch.start();
LockSupport.parkNanos(Duration.ofMillis(100).toNanos()); // run
simulateRunningProcessForMillis(100); // run

stopWatch.pauseWatch();
LockSupport.parkNanos(Duration.ofMillis(50).toNanos()); // pause for this much time
simulateRunningProcessForMillis(50); // pause for this much time

stopWatch.continueWatch();
LockSupport.parkNanos(Duration.ofMillis(25).toNanos()); // again run for this much time
simulateRunningProcessForMillis(25); // again run for this much time

stopWatch.pauseWatch();
LockSupport.parkNanos(Duration.ofMillis(75).toNanos()); // pause
simulateRunningProcessForMillis(75); // pause

assertEquals(stopWatch.stop().toMillis() - (100 + 25), 0, 5);
}

@Test
public void test_pause_continue_pause_continue() {
StopWatch stopWatch = StopWatch.start();
LockSupport.parkNanos(Duration.ofMillis(100).toNanos()); // run
simulateRunningProcessForMillis(100); // run

stopWatch.pauseWatch();
LockSupport.parkNanos(Duration.ofMillis(50).toNanos()); // pause for this much time
simulateRunningProcessForMillis(50); // pause for this much time

stopWatch.continueWatch();
LockSupport.parkNanos(Duration.ofMillis(25).toNanos()); // again run for this much time
simulateRunningProcessForMillis(25); // again run for this much time

stopWatch.pauseWatch();
LockSupport.parkNanos(Duration.ofMillis(75).toNanos()); // pause
simulateRunningProcessForMillis(75); // pause

stopWatch.continueWatch();
LockSupport.parkNanos(Duration.ofMillis(111).toNanos()); // again run for this much time
simulateRunningProcessForMillis(111); // again run for this much time

assertEquals(stopWatch.stop().toMillis() - (100 + 25 + 111), 0, 5);
}

@Test
public void test_process_duration() {
Runnable process = () -> simulateRunningProcessForMillis(124);
assertEquals(StopWatch.timeIt(process).toMillis() - 124, 0, 5);
}

private static void simulateRunningProcessForMillis(long millis) {
LockSupport.parkNanos(Duration.ofMillis(millis).toNanos());
}
}

0 comments on commit d4361ed

Please sign in to comment.