Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
chenpeng_fei
spring-framework
提交
a6a6aed1
S
spring-framework
项目概览
chenpeng_fei
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a6a6aed1
编写于
9月 25, 2015
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
StopWatch provides getId/currentTaskName accessors (plus further fine-tuning)
Issue: SPR-13509
上级
097bcfb9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
44 addition
and
13 deletion
+44
-13
spring-core/src/main/java/org/springframework/util/StopWatch.java
...ore/src/main/java/org/springframework/util/StopWatch.java
+25
-5
spring-core/src/test/java/org/springframework/util/StopWatchTests.java
...rc/test/java/org/springframework/util/StopWatchTests.java
+19
-8
未找到文件。
spring-core/src/main/java/org/springframework/util/StopWatch.java
浏览文件 @
a6a6aed1
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -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
* "false" when using a StopWatch for millions of intervals, or the task
...
...
@@ -116,15 +126,15 @@ public class StopWatch {
if
(
this
.
running
)
{
throw
new
IllegalStateException
(
"Can't start StopWatch: it's already running"
);
}
this
.
startTimeMillis
=
System
.
currentTimeMillis
();
this
.
running
=
true
;
this
.
currentTaskName
=
taskName
;
this
.
startTimeMillis
=
System
.
currentTimeMillis
();
}
/**
* Stop the current task. The results are undefined if timing
* methods are called without invoking at least one pair
* {@
link #start()} / {@link
#stop()} methods.
* {@
code #start()} / {@code
#stop()} methods.
* @see #start()
*/
public
void
stop
()
throws
IllegalStateException
{
...
...
@@ -144,11 +154,21 @@ public class StopWatch {
/**
* Return whether the stop watch is currently running.
* @see #currentTaskName()
*/
public
boolean
isRunning
()
{
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.
...
...
@@ -217,7 +237,7 @@ public class StopWatch {
* Return a short description of the total running time.
*/
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.
*/
public
double
getTimeSeconds
()
{
return
this
.
timeMillis
/
1000.0
;
return
(
this
.
timeMillis
/
1000.0
)
;
}
}
...
...
spring-core/src/test/java/org/springframework/util/StopWatchTests.java
浏览文件 @
a6a6aed1
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -21,6 +20,7 @@ import junit.framework.TestCase;
/**
* @author Rod Johnson
* @author Juergen Hoeller
*/
public
class
StopWatchTests
extends
TestCase
{
...
...
@@ -28,7 +28,8 @@ public class StopWatchTests extends TestCase {
* Are timings off in JUnit?
*/
public
void
testValidUsage
()
throws
Exception
{
StopWatch
sw
=
new
StopWatch
();
String
id
=
"myId"
;
StopWatch
sw
=
new
StopWatch
(
id
);
long
int1
=
166L
;
long
int2
=
45L
;
String
name1
=
"Task 1"
;
...
...
@@ -38,6 +39,7 @@ public class StopWatchTests extends TestCase {
sw
.
start
(
name1
);
Thread
.
sleep
(
int1
);
assertTrue
(
sw
.
isRunning
());
assertEquals
(
name1
,
sw
.
currentTaskName
());
sw
.
stop
();
// 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
);
String
pp
=
sw
.
prettyPrint
();
assertTrue
(
pp
.
indexOf
(
name1
)
!=
-
1
);
assertTrue
(
pp
.
indexOf
(
name2
)
!=
-
1
);
assertTrue
(
pp
.
contains
(
name1
)
);
assertTrue
(
pp
.
contains
(
name2
)
);
StopWatch
.
TaskInfo
[]
tasks
=
sw
.
getTaskInfo
();
assertTrue
(
tasks
.
length
==
2
);
assertTrue
(
tasks
[
0
].
getTaskName
().
equals
(
name1
));
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
{
...
...
@@ -92,8 +100,11 @@ public class StopWatchTests extends TestCase {
assertTrue
(
sw
.
getTaskCount
()
==
2
);
String
pp
=
sw
.
prettyPrint
();
assertTrue
(
pp
.
indexOf
(
"kept"
)
!=
-
1
);
sw
.
toString
();
assertTrue
(
pp
.
contains
(
"kept"
));
String
toString
=
sw
.
toString
();
assertFalse
(
toString
.
contains
(
name1
));
assertFalse
(
toString
.
contains
(
name2
));
try
{
sw
.
getTaskInfo
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录