added getLastTaskInfo() method to StopWatch (SPR-7134)

This commit is contained in:
Juergen Hoeller 2010-05-13 22:43:18 +00:00
parent 68055ba4c9
commit ceb668ac6c
1 changed files with 28 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2010 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.
@ -67,6 +67,7 @@ public class StopWatch {
/** Total running time */ /** Total running time */
private long totalTimeMillis; private long totalTimeMillis;
/** /**
* Construct a new stop watch. Does not start any task. * Construct a new stop watch. Does not start any task.
*/ */
@ -85,6 +86,7 @@ public class StopWatch {
this.id = id; this.id = 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
@ -94,6 +96,7 @@ public class StopWatch {
this.keepTaskList = keepTaskList; this.keepTaskList = keepTaskList;
} }
/** /**
* Start an unnamed task. The results are undefined if {@link #stop()} * Start an unnamed task. The results are undefined if {@link #stop()}
* or timing methods are called without invoking this method. * or timing methods are called without invoking this method.
@ -146,12 +149,13 @@ public class StopWatch {
return this.running; return this.running;
} }
/** /**
* Return the time taken by the last task. * Return the time taken by the last task.
*/ */
public long getLastTaskTimeMillis() throws IllegalStateException { public long getLastTaskTimeMillis() throws IllegalStateException {
if (this.lastTaskInfo == null) { if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tests run: can't get last interval"); throw new IllegalStateException("No tasks run: can't get last task interval");
} }
return this.lastTaskInfo.getTimeMillis(); return this.lastTaskInfo.getTimeMillis();
} }
@ -161,30 +165,41 @@ public class StopWatch {
*/ */
public String getLastTaskName() throws IllegalStateException { public String getLastTaskName() throws IllegalStateException {
if (this.lastTaskInfo == null) { if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tests run: can't get last interval"); throw new IllegalStateException("No tasks run: can't get last task name");
} }
return this.lastTaskInfo.getTaskName(); return this.lastTaskInfo.getTaskName();
} }
/**
* Return the last task as a TaskInfo object.
*/
public TaskInfo getLastTaskInfo() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tasks run: can't get last task info");
}
return this.lastTaskInfo;
}
/** /**
* Return the total time in milliseconds for all tasks. * Return the total time in milliseconds for all tasks.
*/ */
public long getTotalTimeMillis() { public long getTotalTimeMillis() {
return totalTimeMillis; return this.totalTimeMillis;
} }
/** /**
* Return the total time in seconds for all tasks. * Return the total time in seconds for all tasks.
*/ */
public double getTotalTimeSeconds() { public double getTotalTimeSeconds() {
return totalTimeMillis / 1000.0; return this.totalTimeMillis / 1000.0;
} }
/** /**
* Return the number of tasks timed. * Return the number of tasks timed.
*/ */
public int getTaskCount() { public int getTaskCount() {
return taskCount; return this.taskCount;
} }
/** /**
@ -197,6 +212,7 @@ public class StopWatch {
return this.taskList.toArray(new TaskInfo[this.taskList.size()]); return this.taskList.toArray(new TaskInfo[this.taskList.size()]);
} }
/** /**
* Return a short description of the total running time. * Return a short description of the total running time.
*/ */
@ -251,16 +267,17 @@ public class StopWatch {
return sb.toString(); return sb.toString();
} }
/** /**
* Inner class to hold data about one task executed within the stop watch. * Inner class to hold data about one task executed within the stop watch.
*/ */
public static class TaskInfo { public static final class TaskInfo {
private final String taskName; private final String taskName;
private final long timeMillis; private final long timeMillis;
private TaskInfo(String taskName, long timeMillis) { TaskInfo(String taskName, long timeMillis) {
this.taskName = taskName; this.taskName = taskName;
this.timeMillis = timeMillis; this.timeMillis = timeMillis;
} }
@ -269,21 +286,21 @@ public class StopWatch {
* Return the name of this task. * Return the name of this task.
*/ */
public String getTaskName() { public String getTaskName() {
return taskName; return this.taskName;
} }
/** /**
* Return the time in milliseconds this task took. * Return the time in milliseconds this task took.
*/ */
public long getTimeMillis() { public long getTimeMillis() {
return timeMillis; return this.timeMillis;
} }
/** /**
* Return the time in seconds this task took. * Return the time in seconds this task took.
*/ */
public double getTimeSeconds() { public double getTimeSeconds() {
return timeMillis / 1000.0; return this.timeMillis / 1000.0;
} }
} }