week07 作业
发布于: 2020 年 07 月 22 日
1.性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?
响应时间增加 资源挤压
吞吐量先增加后减少 系统资源从富余到饱和再到过载

2.用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
import com.google.common.collect.MinMaxPriorityQueue;import java.util.concurrent.CountDownLatch;import java.util.concurrent.atomic.AtomicInteger;/** * @author yin.jianfeng * @date 2020/7/22 */public class Test {    String url;    int cn;    int frequency;    public Test(String url, int cn, int frequency) {        this.url = url;        this.cn = cn;        this.frequency = frequency;    }    public static Test parseArgs(String[] args) {        String url = args[0];        int cn = Integer.parseInt(args[1]);        int time = Integer.parseInt(args[2]);        return new Test(url, cn, time);    }    static class Statistics {        private MinMaxPriorityQueue<Long> queue;        private long totalTime;        private int frequency;        public Statistics(Test test) {            this.frequency = test.frequency;            queue = MinMaxPriorityQueue.maximumSize(test.frequency * 95 / 100).create();        }        public void print() {            System.out.println("avg:" + totalTime / frequency);            System.out.println("95%:" + queue.peekLast());        }        public void record(long time) {            totalTime += time;            queue.add(time);        }    }    public Statistics doJob(CountDownLatch latch) {        Statistics statistics = new Statistics();        AtomicInteger frequency = new AtomicInteger(this.frequency);        for (int i = 0; i < cn; i++) {            new Thread(                    () -> {                        if (frequency.decrementAndGet() <= 0) {                            latch.countDown();                            return;                        }                        long start = System.currentTimeMillis();                        HttpUtils.get(url);                        long time = System.currentTimeMillis() - start;                        statistics.record(time);                    }            ).start();        }        return statistics;    }    public void run() {        CountDownLatch latch = new CountDownLatch(cn);        Statistics statistics = doJob(latch);        try {            latch.await();        } catch (Exception e) {            e.printStackTrace();        }        statistics.print();    }    public static void main(String[] args) {        Test.parseArgs(args).run();    }}
划线
评论
复制
发布于: 2020 年 07 月 22 日阅读数: 45
Safufu
关注
还未添加个人签名 2018.11.16 加入
还未添加个人简介











 
    
评论