Java并发测试工具类

Java并发测试工具类

  • java代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package cn.pconline.common.utils;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

/**
* 并发测试工具类
* @author jie.zhao
* @date 创建时间:2017年10月17日
* @version v2.0
*/
public class TlUtil {

/**
* 测试耗时
*
* @param nThreads 线程数
* @param task 执行任务
* @param singleNum 单个线程执行个数
* @return
* @throws InterruptedException
*/
public static long timeTasks(int nThreads, int singleNum,
final Runnable task) {
final CountDownLatch startGate = new CountDownLatch(1);
final CountDownLatch endGate = new CountDownLatch(nThreads);
ThreadFactory tf = Executors.defaultThreadFactory();
final int singleExeNum = singleNum == 0 ? 1 : singleNum;
final AtomicLong sum = new AtomicLong();
final AtomicLong min = new AtomicLong(10000);
final AtomicLong max = new AtomicLong(0);
for (int i = 0; i < nThreads; i++) {
tf.newThread(new Thread() {
@Override
public void run() {
try {
startGate.await();
for (int j = 0; j < singleExeNum; j++) {
long start = System.nanoTime();
try {
task.run();
} finally {
long end = System.nanoTime();
long at = ((end - start) / 1000 / 1000);
sum.addAndGet(at);
if (min.get() > at) {
min.getAndSet(at);
}
if (max.get() < at) {
max.getAndSet(at);
}
}
}
endGate.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
long start = System.nanoTime();
startGate.countDown();
try {
endGate.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.nanoTime();
long at = ((end - start) / 1000 / 1000);

int allCount = singleExeNum * nThreads;

System.out.println("执行任务数:" + allCount);
System.out.println("------------------------");
System.out.println("所有线程共耗时:" + transStr(sum.get()));
System.out.println("并发执行完耗时:" + transStr(at));
System.out
.println("单任务平均耗时:" + transStr((double) sum.get() / allCount));
System.out.println("单线程最小耗时:" + transStr(min.get()));
System.out.println("单线程最大耗时:" + transStr(max.get()));
return end - start;
}

public static String transStr(long ms) {
return transStr((double) ms);

}

public static String transStr(double ms) {
if (ms < 1000) {
return ms + " ms";
}
double s = ms / 1000;
if (s < 1000) {
return s + " s";
}
double m = s / 60;
if (m < 60) {
return m + " m";
}
double h = m / 60;
if (h < 24) {
return h + " h";
}

double d = h / 24;
if (d < 30) {
return d + " d";
}
return d + " d";
}


}
-------------已经触及底线 感谢您的阅读-------------

本文标题:Java并发测试工具类

文章作者:趙小傑~~

发布时间:2019年12月17日 - 14:04:47

最后更新:2020年03月01日 - 19:50:39

原始链接:https://cnsyear.com/posts/8e7e6b31.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%