StopWatch provides getId/currentTaskName accessors (plus further fine-tuning)

Issue: SPR-13509
This commit is contained in:
Juergen Hoeller 2015-09-25 15:23:39 +02:00
parent 097bcfb997
commit a6a6aed17f
2 changed files with 44 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -87,6 +87,16 @@ public class StopWatch {
} }
/**
* Return the id of this stop watch, as specified on construction.
* @return the id (empty String by default)
* @since 4.2.2
* @see #StopWatch(String)
*/
public String getId() {
return this.id;
}
/** /**
* Determine whether the TaskInfo array is built over time. Set this to * Determine whether the TaskInfo array is built over time. Set this to
* "false" when using a StopWatch for millions of intervals, or the task * "false" when using a StopWatch for millions of intervals, or the task
@ -116,15 +126,15 @@ public class StopWatch {
if (this.running) { if (this.running) {
throw new IllegalStateException("Can't start StopWatch: it's already running"); throw new IllegalStateException("Can't start StopWatch: it's already running");
} }
this.startTimeMillis = System.currentTimeMillis();
this.running = true; this.running = true;
this.currentTaskName = taskName; this.currentTaskName = taskName;
this.startTimeMillis = System.currentTimeMillis();
} }
/** /**
* Stop the current task. The results are undefined if timing * Stop the current task. The results are undefined if timing
* methods are called without invoking at least one pair * methods are called without invoking at least one pair
* {@link #start()} / {@link #stop()} methods. * {@code #start()} / {@code #stop()} methods.
* @see #start() * @see #start()
*/ */
public void stop() throws IllegalStateException { public void stop() throws IllegalStateException {
@ -144,11 +154,21 @@ public class StopWatch {
/** /**
* Return whether the stop watch is currently running. * Return whether the stop watch is currently running.
* @see #currentTaskName()
*/ */
public boolean isRunning() { public boolean isRunning() {
return this.running; return this.running;
} }
/**
* Return the name of the currently running task, if any.
* @since 4.2.2
* @see #isRunning()
*/
public String currentTaskName() {
return this.currentTaskName;
}
/** /**
* Return the time taken by the last task. * Return the time taken by the last task.
@ -217,7 +237,7 @@ public class StopWatch {
* Return a short description of the total running time. * Return a short description of the total running time.
*/ */
public String shortSummary() { public String shortSummary() {
return "StopWatch '" + this.id + "': running time (millis) = " + getTotalTimeMillis(); return "StopWatch '" + getId() + "': running time (millis) = " + getTotalTimeMillis();
} }
/** /**
@ -302,7 +322,7 @@ public class StopWatch {
* Return the time in seconds this task took. * Return the time in seconds this task took.
*/ */
public double getTimeSeconds() { public double getTimeSeconds() {
return this.timeMillis / 1000.0; return (this.timeMillis / 1000.0);
} }
} }

View File

@ -1,6 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,6 +20,7 @@ import junit.framework.TestCase;
/** /**
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller
*/ */
public class StopWatchTests extends TestCase { public class StopWatchTests extends TestCase {
@ -28,7 +28,8 @@ public class StopWatchTests extends TestCase {
* Are timings off in JUnit? * Are timings off in JUnit?
*/ */
public void testValidUsage() throws Exception { public void testValidUsage() throws Exception {
StopWatch sw = new StopWatch(); String id = "myId";
StopWatch sw = new StopWatch(id);
long int1 = 166L; long int1 = 166L;
long int2 = 45L; long int2 = 45L;
String name1 = "Task 1"; String name1 = "Task 1";
@ -38,6 +39,7 @@ public class StopWatchTests extends TestCase {
sw.start(name1); sw.start(name1);
Thread.sleep(int1); Thread.sleep(int1);
assertTrue(sw.isRunning()); assertTrue(sw.isRunning());
assertEquals(name1, sw.currentTaskName());
sw.stop(); sw.stop();
// TODO are timings off in JUnit? Why do these assertions sometimes fail // TODO are timings off in JUnit? Why do these assertions sometimes fail
@ -54,14 +56,20 @@ public class StopWatchTests extends TestCase {
assertTrue(sw.getTaskCount() == 2); assertTrue(sw.getTaskCount() == 2);
String pp = sw.prettyPrint(); String pp = sw.prettyPrint();
assertTrue(pp.indexOf(name1) != -1); assertTrue(pp.contains(name1));
assertTrue(pp.indexOf(name2) != -1); assertTrue(pp.contains(name2));
StopWatch.TaskInfo[] tasks = sw.getTaskInfo(); StopWatch.TaskInfo[] tasks = sw.getTaskInfo();
assertTrue(tasks.length == 2); assertTrue(tasks.length == 2);
assertTrue(tasks[0].getTaskName().equals(name1)); assertTrue(tasks[0].getTaskName().equals(name1));
assertTrue(tasks[1].getTaskName().equals(name2)); assertTrue(tasks[1].getTaskName().equals(name2));
sw.toString();
String toString = sw.toString();
assertTrue(toString.contains(id));
assertTrue(toString.contains(name1));
assertTrue(toString.contains(name2));
assertEquals(id, sw.getId());
} }
public void testValidUsageNotKeepingTaskList() throws Exception { public void testValidUsageNotKeepingTaskList() throws Exception {
@ -92,8 +100,11 @@ public class StopWatchTests extends TestCase {
assertTrue(sw.getTaskCount() == 2); assertTrue(sw.getTaskCount() == 2);
String pp = sw.prettyPrint(); String pp = sw.prettyPrint();
assertTrue(pp.indexOf("kept") != -1); assertTrue(pp.contains("kept"));
sw.toString();
String toString = sw.toString();
assertFalse(toString.contains(name1));
assertFalse(toString.contains(name2));
try { try {
sw.getTaskInfo(); sw.getTaskInfo();