压测小工具

    xiaoxiao2024-08-17  89

    最近给某东西压测(由于是私有协议没有通用工具可用),就自己写了个可以给压力的工具,可以支持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 " 相关资源:压力测试工具ab
    最新回复(0)