JAVA - StopWatch 클래스 사용하기
일단 해당 클래스는 Spring Core 2.5.6 에 포함되어 있음
근데 난 저거 다 쓸꺼도 아니고.. 스탑워치만 필요해서 해당 클래스를 그냥 빼와서 사용함
사용법
아래와같이 사용하면면 됨
StopWatch stopWatch1 = new StopWatch();
stopWatch2.start();
//시간을 측정할 작업
stopWatch2.stop();
System.out.println(stopWatch2.getTotalTimeNanos());
※ 아래의 클래스를 추가하여서 그대로 사용하면 됨
/*
- Copyright 2002-2021 the original author or authors.
- Licensed under the Apache License, Version 2.0 (the “License”);
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- https://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an “AS IS” BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
*/
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
- Simple stop watch, allowing for timing of a number of tasks, exposing total
- running time and running time for each named task.
-
Conceals use of {@link System##nanoTime()}, improving the readability of
- application code and reducing the likelihood of calculation errors.
-
Note that this object is not designed to be thread-safe and does not use
- synchronization.
-
This class is normally used to verify performance during proof-of-concept
- work and in development, rather than as part of production applications.
-
As of Spring Framework 5.2, running time is tracked and reported in
- nanoseconds.
- @author Rod Johnson
- @author Juergen Hoeller
- @author Sam Brannen
-
@since May 2, 2001
*/
public class StopWatch {
/**
private boolean keepTaskList = true;
private final List taskList = new ArrayList<>(1);
/** Start time of the current task. */
private long startTimeNanos;
/** Name of the current task. */
private String currentTaskName;
private TaskInfo lastTaskInfo;
private int taskCount;
/** Total running time. */
private long totalTimeNanos;
/**
/**
/**
- Get the ID of this {@code StopWatch}, 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 this {@code StopWatch} is currently running.
- @see ##currentTaskName()
*/
public boolean isRunning() {
return (this.currentTaskName != null);
}
/**
- Get the name of the currently running task, if any.
- @since 4.2.2
- @see ##isRunning()
*/
public String currentTaskName() {
return this.currentTaskName;
}
/**
- Get the time taken by the last task in nanoseconds.
- @since 5.2
- @see ##getLastTaskTimeMillis()
*/
public long getLastTaskTimeNanos() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException(“No tasks run: can’t get last task interval”);
}
return this.lastTaskInfo.getTimeNanos();
}
/**
- Get the time taken by the last task in milliseconds.
- @see ##getLastTaskTimeNanos()
*/
public long getLastTaskTimeMillis() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException(“No tasks run: can’t get last task interval”);
}
return this.lastTaskInfo.getTimeMillis();
}
/**
- Get the name of the last task.
*/
public String getLastTaskName() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException(“No tasks run: can’t get last task name”);
}
return this.lastTaskInfo.getTaskName();
}
/**
- Get the last task as a {@link 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;
}
/**
- Get the total time in nanoseconds for all tasks.
- @since 5.2
- @see ##getTotalTimeMillis()
- @see ##getTotalTimeSeconds()
*/
public long getTotalTimeNanos() {
return this.totalTimeNanos;
}
/**
- Get the total time in milliseconds for all tasks.
- @see ##getTotalTimeNanos()
- @see ##getTotalTimeSeconds()
*/
public long getTotalTimeMillis() {
return nanosToMillis(this.totalTimeNanos);
}
/**
- Get the total time in seconds for all tasks.
- @see ##getTotalTimeNanos()
- @see ##getTotalTimeMillis()
*/
public double getTotalTimeSeconds() {
return nanosToSeconds(this.totalTimeNanos);
}
/**
- Get the number of tasks timed.
*/
public int getTaskCount() {
return this.taskCount;
}
/**
- Get an array of the data for tasks performed.
*/
public TaskInfo[] getTaskInfo() {
if (!this.keepTaskList) {
throw new UnsupportedOperationException(“Task info is not being kept!”);
}
return this.taskList.toArray(new TaskInfo[0]);
}
/**
- Get a short description of the total running time.
*/
public String shortSummary() {
return “StopWatch ‘” + getId() + “’: running time = “ + getTotalTimeNanos() + “ ns”;
}
/**
/**
private static long nanosToMillis(long duration) {
return TimeUnit.NANOSECONDS.toMillis(duration);
}
private static double nanosToSeconds(long duration) {
return duration / 1_000_000_000.0;
}
/**
-
Nested class to hold data about one task executed within the {@code StopWatch}.
*/
public static final class TaskInfo {
private final String taskName;
private final long timeNanos;
TaskInfo(String taskName, long timeNanos) {
this.taskName = taskName;
this.timeNanos = timeNanos;
}
/**
- Get the name of this task.
*/
public String getTaskName() {
return this.taskName;
}
/**
- Get the time in nanoseconds this task took.
- @since 5.2
- @see ##getTimeMillis()
- @see ##getTimeSeconds()
*/
public long getTimeNanos() {
return this.timeNanos;
}
/**
- Get the time in milliseconds this task took.
- @see ##getTimeNanos()
- @see ##getTimeSeconds()
*/
public long getTimeMillis() {
return nanosToMillis(this.timeNanos);
}
/**
- Get the time in seconds this task took.
- @see ##getTimeMillis()
- @see ##getTimeNanos()
*/
public double getTimeSeconds() {
return nanosToSeconds(this.timeNanos);
}
}
}