架构师训练营W7-作业

米斯特程序猿 2020年11月08日 311次浏览

1、性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?

  • 随着并发数量的增加系统响应时间会越来越低,而吞吐量也会相应的下降,因为处理器频繁切换线程造成吞吐量下降(因为CPU要将时间分配给更多的处理线程)

2、用你熟悉的编程语言写一个 Web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 作业模拟并发请求
 */
public class Test {

    public static final CountDownLatch cdl=new CountDownLatch(1);// 发送信号,开始并发请求
    public static Semaphore s;// 限制并发数
    public static CountDownLatch cdl2;
    public static final ConcurrentLinkedQueue<Long> time=new ConcurrentLinkedQueue();
    public static AtomicLong totalTime=new AtomicLong(0);
    public static void main(String[] args) throws IOException, InterruptedException {
        int maxThread=10;
        int requestNum=100;
        String url="http://www.baidu.com";
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("输入请求地址:");
        url=br.readLine();
        if(url==null||url.length()<1){
            url="http://www.baidu.com";
        }
        System.out.println(url);
        System.out.println("输入总请求数:");
        String t=br.readLine();
        if(t==null||t.length()<1){
            t="100";
        }
        System.out.println(t);
        requestNum=Integer.parseInt(t);
        System.out.println("输入并发数:");
        t=br.readLine();
        if(t==null||t.length()<1){
            t="10";
        }
        System.out.println(t);
        maxThread=Integer.parseInt(t);
        ExecutorService es= Executors.newFixedThreadPool(maxThread);
        s=new Semaphore(maxThread);
        cdl2=new CountDownLatch(requestNum);// 请求次数
        for (int i = 0; i < requestNum; i++) {
            es.execute(new Get(url));
        }
        cdl.countDown();// 开始并发请求
        // 等待执行结束
        cdl2.await();
        es.shutdown();
        double avgTime=totalTime.get()/requestNum;
        List<Long> temp=new ArrayList<Long>(time.size());
        for(Long tt:time){
            temp.add(tt);
        }
        Collections.sort(temp);
        long _95Time=temp.get((int) (requestNum*0.95));;
        System.out.println("平均响应时间\t95%响应时间");
        System.out.println(String.format("[%.2f]ms\t[%d]ms",avgTime,_95Time));
    }

    public static class Get implements Runnable{
        private String url;
        public Get(String url){
            this.url=url;
        }
        public void run() {
            try {
                cdl.await();// 阻塞,等待请求指令
                s.acquire();
                // 请求百度
                URL urlTest=new URL(url);
                long start=System.currentTimeMillis();
                HttpURLConnection http=(HttpURLConnection) urlTest.openConnection();
                BufferedReader br=new BufferedReader(new InputStreamReader(http.getInputStream()));
                String line;
                StringBuilder sb=new StringBuilder();
                while ((line=br.readLine())!=null){
                    sb.append(line);
                }
                long end=System.currentTimeMillis()-start;
                time.add(end);
                totalTime.addAndGet(end);
                //System.out.println(sb);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                s.release();
                cdl2.countDown();
            }
        }
    }
}

测试结果

输入请求地址:

http://www.baidu.com
输入总请求数:

100
输入并发数:

10
平均响应时间	95%响应时间
[19.00]ms	[103]ms