Java并发压测剔除无关日志

  概述

  新开发接口压测, 采用RestTemplate访问远程接口, main加多线程并发访问完成测试, 为直观查看接口数据信息, 屏蔽无关日志的干扰, 特自建日志输出流, 设置日志过滤条件, 包装原System的对外输出结果. 具体操作代码如下:

  压测代码:

import com.rosetta.image.log.LoggerStream;
import org.springframework.http.HttpEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class TestInterface {

    public static void main(String[] args) throws Exception {


        ExecutorService executorService = Executors.newFixedThreadPool(100);

        List<Callable<String>> tasks = new ArrayList<>();
        Callable<String> task;

//        System.setOut(new PrintStream(new FileOutputStream("/home/lab/test/system_out.txt")));

        System.setOut(new PrintStream(new LoggerStream(Logger.getLogger("com.rosetta.image"),Level.INFO, new FileOutputStream("/home/lab/test/system_out.txt"))));
        ConcurrentLinkedDeque<Long> deque = new ConcurrentLinkedDeque<Long>();

        int i = 0;
        while (true) {

            task = () -> {
                RestTemplate restTemplate = new RestTemplate();
                LinkedMultiValueMap<String,String> paramMap = new LinkedMultiValueMap<>();
                paramMap.add("key","大王叫我来巡山,我把人间转一转");
                HttpEntity<LinkedMultiValueMap<String,String>> httpEntity = new HttpEntity<>(paramMap,null);
                long aa = System.currentTimeMillis();
                String body = restTemplate.postForEntity("http://172.18.28.100:9898/test", httpEntity, String.class).getBody();
                long bb = System.currentTimeMillis();
                long l = bb - aa;
                deque.add(l);
                System.out.println("millis ------->  " + (bb - aa));
                return body;
            };
            if (i < 10000) {
                tasks.add(task);
            } else break;
            i++;
        }

        List<Future<String>> futures = executorService.invokeAll(tasks);
        System.out.println("millis ------->  size : " + futures.size());
        long now = 0l;
        for (Long l :
                deque) {
            now += l;
        }
        System.out.println("millis ------->  平均耗时: " + (now / deque.size()));
        executorService.shutdown();

    }

}

  输出流:

import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LoggerStream extends OutputStream {

    private final Logger logger;

    private final Level logLevel;

    private final OutputStream outputStream;

    public LoggerStream(Logger logger, Level logLevel, OutputStream outputStream) {
        super();
        this.logger = logger;
        this.logLevel = logLevel;
        this.outputStream = outputStream;
    }

    @Override
    public void write(byte[] b) throws IOException {
        outputStream.write(b);
        String string = new String(b);
        if (string.contains("millis ------->")) {
            logger.log(logLevel, string);
        }
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        outputStream.write(b, off, len);
        String string = new String(b, off, len);
        if (string.contains("millis ------->")) {
            logger.log(logLevel, string);
        }
    }

    @Override
    public void write(int b) throws IOException {
        outputStream.write(b);
        String string = String.valueOf((char) b);
        if (string.contains("millis ------->")) {
            logger.log(logLevel, string);
        }
    }

}

猜你喜欢

转载自www.cnblogs.com/nyatom/p/10880555.html
今日推荐