【多线程】开发人员用多线程对Http接口进行压力测试

        问题描述:之前用smartBI开发的报表(通过A项目与smartBI的对接获取数据,然后A项目提供接口给前端作展示),相对来说比较简单,但一套流程开发下来后,发现A项目服务老挂掉,原因是访问量比较大(其实也就百万级的数据量,并发用户量也就1000左右,奈何公司规模小,硬件设备增加缓慢),这个后果造成的根本原因是smartBI撑不住这样的访问量。其实也好理解,开发角度来讲smartBI的优势是:可以革命性的加快开发速度,提高软件的稳定性并做到随需应变。做一些高并发的数据访问确实很吃力,所以就单独出一个新的项目专门承接smartBI,来替代smartBI。

  完成项目之后就要进行相应的压力测试,虽然有性能测试工程师,但是为减少bug的数量,还是自己简单的测一下,我没用相关工具如Sublime Text 3(其实也是不会用),就自己写了多线程进行测试,比较简单,一个借口一个类(含main方法),下面是所写的代码:

RequestHandler.java

public interface RequestHandler {
	
	public void handle(T result);
}

ConcurrentTestUtil.java

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import 

public class TestUtil {
	static HttpsUtils httpsUtils=new HttpsUtils();
	 /**
     * 多线程并发执行某项任务
     *
     * @param concurrentThreads    并发线程数,可以用来模拟并发访问用户数
     * @param times                总共执行多少次
     * @param task                 任务
     * @param requestHandler        结果处理器
     * @param executeTimeoutMillis 执行任务总超时
     * @throws InterruptedException
     * @throws ExecutionException
     */
    public static  void concurrentTest(long concurrentThreads, int times, final Callable task,
                                          RequestHandler requestHandler, long executeTimeoutMillis)
            throws InterruptedException, ExecutionException {

        ExecutorService executor = Executors.newFixedThreadPool((int) concurrentThreads);
        List> results = new ArrayList>(times);

        long startTimeMillis = System.currentTimeMillis();
        for (int i = 0; i < times; i++) {
            results.add(executor.submit(task));
        }
        executor.shutdown();

        boolean executeCompleteWithinTimeout = executor.awaitTermination(executeTimeoutMillis,TimeUnit.MILLISECONDS);
        if (!executeCompleteWithinTimeout) {
            System.out.println("Execute tasks out of timeout [" + executeTimeoutMillis + "ms]");

            /*
             * 取消所有任务
             */
            for (Future r : results) {
                r.cancel(true);
            }
        } else {
            long totalCostTimeMillis = System.currentTimeMillis() - startTimeMillis;

            // 线程池此时肯定已关闭,处理任务结果
            for (Future r : results) {
                if (requestHandler != null) {
                    requestHandler.handle(r.get());
                }
            }

            System.out.println("concurrent threads: " + concurrentThreads + ", times: "   + times);
            System.out.println("total cost time(ms): " + totalCostTimeMillis  + "ms, avg time(ms): " + ((double) totalCostTimeMillis / times));
            System.out.println("tps: " + (double) (times * 1000) / totalCostTimeMillis);
        }
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ConcurrentTestUtil.concurrentTest(1000, 3000,
                new Callable() {
                    @Override
                    public String call() throws Exception {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                        String mobile=httpsUtils.doGetForHttps("http接口", "参数Map", "UTF-8");//测试接口
                        returne  "hello!";
                    }
                },
                new RequestHandler() {
                    @Override
                    public void handle(String result) {
                        System.out.println("result: " + result);
                    }
                }, 6000);
    }
}
        只是简单的测一下,我的项目接口挂掉了,祝你们好运!!!



你可能感兴趣的:(多线程)