zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    
    }
  • 相关阅读:
    跟vczh看实例学编译原理——二:实现Tinymoe的词法分析
    跟vczh看实例学编译原理——一:Tinymoe的设计哲学
    跟vczh看实例学编译原理——零:序言
    2013年终总结
    如何设计一门语言(十二)——设计可扩展的类型
    开始用Word 2013来写博客
    如何设计一门语言(十一)——删减语言的功能
    如何设计一门语言(十)——正则表达式与领域特定语言(DSL)
    链表
    结构的学习
  • 原文地址:https://www.cnblogs.com/nyatom/p/10880555.html
Copyright © 2011-2022 走看看