压测小工具

简介:
最近给某东西压测(由于是私有协议没有通用工具可用),就自己写了个可以给压力的工具,可以支持QPS显示和响应时间的分布显示,有兴趣的可以拿来玩玩

sql压测使用样例:

?
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
import groovy.sql.Sql
 
/**
 ``* @author <a href="mailto:wentong@taobao.com"></a>
 ``* @since 11-11-9 2:00
 ``*
 ``*/
 
def client_num = ``100
 
private List<Sql> connent(``int num) {
  ``def result = []
  ``1``.``upto``(num) {result.add(Sql.newInstance(``"jdbc:[mysql://127.0.0.1:3306/sbtest](3306/sbtest)"``, ``"test"``, ``"test"``, ``"com.mysql.jdbc.Driver"``))}
  ``return result
}
 
def clients = connent(client_num)
 
def getId() {
  ``def total_record_num = ``80000000
 
  ``def hot = ``20
 
  ``def step = ``100 / hot
 
  ``BigDecimal hot_range = total_record_num / ``step
  ``if (RandomTest.nextInt(``100``) < ``95``) {
    ``return RandomTest.getId(hot_range, ``1``).intValue().toString()
  ``} ``else {
    ``return RandomTest.getId(total_record_num, ``1``).intValue().toString()
  ``}
 
}
 
def s = ``new StressTest()
s.add({
  ``Sql client = clients.``get``(it)
  ``client.execute(``"select id ,k,c,pad from sbtest where id =" + getId())
}, client_num)
 
s.run()
 
clients.``each {it.close()}

StressTest 源码:

?
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
 
/**
 ``* @author <a href="mailto:wentong@taobao.com"></a>
 ``* @since 11-11-9 1:12
 ``*
 ``*/
class StressTest {
 
  ``int interval = ``1``;   ``//s
 
  ``int count = ``0``;
 
  ``int time = ``0``;
 
  ``boolean print_rt = true
 
  ``private final AtomicLong MONITOR_STAT_QPS_NUM = ``new AtomicLong();
 
  ``private final AtomicLong MONITOR_RESPNSE_TOTAL = ``new AtomicLong();
 
  ``private final AtomicLong MONITOR_RESPNSE_0MS = ``new AtomicLong();
 
  ``private final AtomicLong MONITOR_RESPNSE_0_1MS = ``new AtomicLong();
 
  ``private final AtomicLong MONITOR_RESPNSE_1_5MS = ``new AtomicLong();
 
  ``private final AtomicLong MONITOR_RESPNSE_5_10MS = ``new AtomicLong();
 
  ``private final AtomicLong MONITOR_RESPNSE_10_50MS = ``new AtomicLong();
 
  ``private final AtomicLong MONITOR_RESPNSE_50_100MS = ``new AtomicLong();
 
  ``private final AtomicLong MONITOR_RESPNSE_100_500MS = ``new AtomicLong();
 
  ``private final AtomicLong MONITOR_RESPNSE_500_1000MS = ``new AtomicLong();
 
  ``private final AtomicLong MONITOR_RESPNSE_1000_MS = ``new AtomicLong();
 
  ``private final ThreadLocal<Long> START_TIME = ``new ThreadLocal<Long>();
 
  ``private Map<Closure, Integer> closures = ``new HashMap<Closure, Integer>();
 
  ``private int thread_num = ``0``;
 
  ``private AtomicInteger stop_thread_num = ``new AtomicInteger();
 
  ``private ExecutorService pool;
 
  ``private boolean is_stop = false
 
  ``/**
   ``* Method start ...
   ``*/
  ``private void start() {
    ``START_TIME.set(System.currentTimeMillis());
  ``}
 
  ``/**
   ``* Method end ...
   ``*/
  ``private void end() {
    ``long start = START_TIME.``get``();
    ``long response = System.currentTimeMillis() - start;
    ``MONITOR_STAT_QPS_NUM.incrementAndGet();
    ``MONITOR_RESPNSE_TOTAL.incrementAndGet();
    ``if (response <= ``0``) {
      ``MONITOR_RESPNSE_0MS.incrementAndGet();
    ``} ``else if (response > ``0 && response <= ``1``) {
      ``MONITOR_RESPNSE_0_1MS.incrementAndGet();
    ``} ``else if (response > ``1 && response <= ``5``) {
      ``MONITOR_RESPNSE_1_5MS.incrementAndGet();
    ``} ``else if (response > ``5 && response <= ``10``) {
      ``MONITOR_RESPNSE_5_10MS.incrementAndGet();
    ``} ``else if (response > ``10 && response <= ``50``) {
      ``MONITOR_RESPNSE_10_50MS.incrementAndGet();
    ``} ``else if (response > ``50 && response <= ``100``) {
      ``MONITOR_RESPNSE_50_100MS.incrementAndGet();
    ``} ``else if (response > ``100 && response <= ``500``) {
      ``MONITOR_RESPNSE_100_500MS.incrementAndGet();
    ``} ``else if (response > ``500 && response <= ``1000``) {
      ``MONITOR_RESPNSE_500_1000MS.incrementAndGet();
    ``} ``else if (response > ``1000``) {
      ``MONITOR_RESPNSE_1000_MS.incrementAndGet();
    ``}
  ``}
 
  ``public void add(Closure closure, ``int threadNum) {
    ``closures.put(closure, threadNum)
    ``this.thread_num += threadNum
  ``}
 
  ``public void run() {
    ``pool = Executors.newFixedThreadPool(thread_num + ``1``)
    ``def defer = { c -> pool.submit(c ``as Runnable) }
    ``closures.``each { c ->
      ``if (c.value > ``0``) {
        ``1``.``upto``(c.value) {  idx ->
          ``defer {runit(c.key, idx - ``1``)}
        ``}
      ``}
    ``}
    ``defer {monitor()}
    ``pool.shutdown()
    ``while (!pool.awaitTermination(``1000``, TimeUnit.SECONDS)) {
    ``}
    ``println "#######################################"
  ``}
 
  ``private void runit(Closure closure, ``int i) {
    ``int c = ``count / thread_num;
    ``try {
      ``while(!is_stop && (``count == ``0 || c > ``0``)) {
        ``try {
          ``start()
          ``closure.call(i)
        ``} ``finally {
          ``end()
          ``if (c > ``0``) {
            ``c--
          ``}
        ``}
      ``}
    ``} ``finally {
      ``stop_thread_num.incrementAndGet()
    ``}
  ``}
 
  ``/**
   ``* Method run ...
   ``*/
  ``private void monitor() {
    ``println``(``"start monitor"``)
    ``long start_time = System.currentTimeMillis()
    ``long end_time = start_time + (time * ``1000``);
    ``while (stop_thread_num.``get``() < thread_num) {
      ``if (time > ``0 && System.currentTimeMillis() >= end_time) {
        ``println "time is over"
        ``is_stop = true
      ``}
      ``Thread.sleep(interval * ``1000``);
 
      ``long num = MONITOR_STAT_QPS_NUM.getAndSet(``0``);
      ``println``(``"QPS:" + (num / interval) + ``" TOTAL:" + MONITOR_RESPNSE_TOTAL.``get``());
 
      ``if (print_rt) {
        ``print_rt();
      ``}
      ``println``(``"----------------------------------"``);
    ``}
 
    ``def total_time = (System.currentTimeMillis() - start_time) / ``1000
 
    ``println "avg QPS:"+ MONITOR_RESPNSE_TOTAL.``get``() / total_time + ``" ,total:" + MONITOR_RESPNSE_TOTAL.``get``()
    ``print_rt()
    ``println``(``"end monitor"``)
  ``}
 
  ``private def print_rt() {
    ``long total = MONITOR_RESPNSE_TOTAL.``get``();
    ``println``(``" RT <= 0:      " + (MONITOR_RESPNSE_0MS.``get``() * ``100 / total) + ``"% " +
            ``MONITOR_RESPNSE_0MS.``get``() + ``"/" + total);
    ``println``(``" RT (0,1]:     " + (MONITOR_RESPNSE_0_1MS.``get``() * ``100 / total) + ``"% "+
            ``MONITOR_RESPNSE_0_1MS.``get``() + ``"/" + total);
    ``println``(``" RT (1,5]:     " + (MONITOR_RESPNSE_1_5MS.``get``() * ``100 / total) + ``"% "+
            ``MONITOR_RESPNSE_1_5MS.``get``() + ``"/" + total);
    ``println``(``" RT (5,10]:    " + (MONITOR_RESPNSE_5_10MS.``get``() * ``100 / total) + ``"% " +
            ``MONITOR_RESPNSE_5_10MS.``get``() + ``"/" + total);
    ``println``(``" RT (10,50]:   " + (MONITOR_RESPNSE_10_50MS.``get``() * ``100 / total) + ``"% " +
            ``MONITOR_RESPNSE_10_50MS.``get``() + ``"/" + total);
    ``println``(``" RT (50,100]:  " + (MONITOR_RESPNSE_50_100MS.``get``() * ``100 / total) + ``"% " +
            ``MONITOR_RESPNSE_50_100MS.``get``() + ``"/" + total);
    ``println``(``" RT (100,500]: " + (MONITOR_RESPNSE_100_500MS.``get``() * ``100 / total) + ``"% " +
            ``MONITOR_RESPNSE_100_500MS.``get``() + ``"/" + total);
    ``println``(``" RT (500,1000]:" + (MONITOR_RESPNSE_500_1000MS.``get``() * ``100 / total) + ``"% " +
            ``MONITOR_RESPNSE_500_1000MS.``get``() + ``"/" + total);
    ``println``(``" RT > 1000:    " + (MONITOR_RESPNSE_1000_MS.``get``() * ``100 / total) + ``"% "+
            ``MONITOR_RESPNSE_1000_MS.``get``() + ``"/" + total)
  ``}
}
本文来源于"阿里中间件团队播客",原文发表时间" 2011-11-18 "
相关实践学习
通过性能测试PTS对云服务器ECS进行规格选择与性能压测
本文为您介绍如何利用性能测试PTS对云服务器ECS进行规格选择与性能压测。
相关文章
|
2月前
|
JavaScript jenkins 测试技术
这10款性能测试工具,收藏起来,测试人的工具箱!
这10款性能测试工具,收藏起来,测试人的工具箱!
115 1
|
6月前
|
Cloud Native 测试技术
性能测试小工具 wrk 可以怎么用
性能测试小工具 wrk 可以怎么用
|
8月前
|
消息中间件 监控 测试技术
消息队列和应用工具产品体系-性能测试场景和工具
消息队列和应用工具产品体系-性能测试场景和工具
90 0
消息队列和应用工具产品体系-性能测试场景和工具
|
6月前
|
消息中间件 弹性计算 Java
使用阿里云性能测试工具 JMeter 场景压测 RocketMQ 最佳实践
使用阿里云性能测试工具 JMeter 场景压测 RocketMQ 最佳实践
|
2月前
|
算法 Java 测试技术
性能工具之代码级性能测试工具ContiPerf
【2月更文挑战第23天】性能工具之代码级性能测试工具ContiPerf
274 1
性能工具之代码级性能测试工具ContiPerf
|
4月前
|
人工智能 测试技术 iOS开发
iOS性能指标和性能测试工具
iOS性能指标和性能测试工具
|
4月前
|
测试技术
HTTP性能测试工具Siege 简介
HTTP性能测试工具Siege 简介
|
4月前
|
监控 测试技术 Apache
性能测试:方法、工具与最佳实践
性能测试:方法、工具与最佳实践
141 0
|
5月前
|
测试技术 Linux
百度搜索:蓝易云【【Linux】硬件性能测试工具安装。】
以上是一些常见的硬件性能测试工具,你可以根据需要选择适合的工具进行安装和使用。注意,在安装和运行这些工具时,请遵循官方文档和指南,并在测试过程中小心操作,以避免对系统造成不良影响。
90 1
|
10月前
|
SQL 存储 分布式计算
数据仓库性能测试方法论与工具集
数据仓库是数据库的下一代产品形态 —— 如何对数字化转型过程中涌现的数据集合进行有效的存储、分析和利用,继而帮忙企业进行运营决策优化甚至创造出新的获客模式和商业模式形成竞争力,是企业主们亟需解决的问题。在数据价值爆发的时代背景中,数据仓库在千行百业中都有着相应的应用场景。
367 0