前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jmeter采集测试日志

jmeter采集测试日志

作者头像
测试加
发布2022-03-24 15:54:07
4840
发布2022-03-24 15:54:07
举报
文章被收录于专栏:用户4624600的专栏

背景

在使用jmeter压测的工作中,不仅需要关注当前qps,也需要查看请求日志. 下面介绍两种方式收集jmeter的请求日志

BeanShell

使用BeanShell方法是解析请求数据并存到本地文件中.

代码片段

代码语言:javascript
复制
import java.io.*;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
import org.apache.jmeter.config.Arguments;
import java.text.SimpleDateFormat;
import java.util.Calendar;


Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
String RequestTime = ft.format(dNow);
byte[] rpData = (byte[])ResponseData;
String reponseDataAsString = Response.getResponseDataAsString();
String filePath="/Users/xinxi/Documents/jmeter/apache-jmeter-5.0/bin/jmeter_file.log";


log.info("当前时间为=============================:" + ft.format(dNow));
log.info("SampleLabel=============================:" + SampleLabel);
log.info("SamplerData=============================:" + SamplerData);
log.info("RequestHeaders=============================:" + RequestHeaders);
log.info("ResponseHeaders=============================:" + ResponseHeaders);
log.info("ResponseMessage=============================:" + ResponseMessage);
log.info("isSuccessful=============================:" + Successful);
log.info("ResponseCode=============================:" + ResponseCode);
log.info("原生的ResponseData============================:" + ResponseData);
log.info("reponseDataAsString============================:" + reponseDataAsString);
//log.info("reponseDataAsString============================:" + reponseDataAsString);


JSONObject jsonObject = new JSONObject();
jsonObject.put("request_time",RequestTime);
jsonObject.put("url",SamplerData);
jsonObject.put("request_code",ResponseCode);
jsonObject.put("response_data",reponseDataAsString);
String jsonString = JSON.toJSONString(jsonObject); 
log.info("reponseDataAsString============================:" + jsonString);

try{

   String filePath="/Users/xinxi/Documents/jmeter/apache-jmeter-5.0/bin/jmeter_file.log";
   try{
       File file = new File(filePath);
       if(!file.exists()){
           file.createNewFile();
       }
       FileWriter writer = new FileWriter(filePath, true);
       writer.write(jsonString + "\r\n");
       writer.close();
   }catch(IOException e){
       e.printStackTrace();
   }
}catch(IOException e){
	e.printStackTrace();
	fos.close();
}

elk

借助elk系统,存储数据和展示数据.

安装镜像

代码语言:javascript
复制
docker run \
--name elk \
-d \
-e LOGSTASH_START=0 \
-p 5601:5601 \
-p 9200:9200 \
-p 5044:5044 -d \
sebp/elk

linux配置

linux启动服务有内存限制

解决方法

代码语言:javascript
复制
[root@localhost ~]# sysctl -w vm.max_map_count=262144

[root@localhost ~]# sysctl -a|grep vm.max_map_count
vm.max_map_count = 262144

logstash

logstash配置

代码语言:javascript
复制
input {
    file {
        path => "/Users/xinxi/Documents/jmeter/apache-jmeter-5.0/bin/jmeter_file.log"
        start_position => beginning
        codec => "json"
    }
}
filter {
  date {
        match => ["log_time", "ISO8601"]
        timezone => "Asia/Shanghai"
    }
}
output {
    elasticsearch {
        hosts => ["192.168.143.242:9200"]
        index => "requests-json-%{+YYYY.MM.dd}"
    }
    stdout{  
    codec => rubydebug  
    }
}

启动命令

启动logstash服务,定时把jmeter_file.log文件发送到elk系统中

代码语言:javascript
复制
bin/logstash -f config/request.conf

filebeat

相比logstash, filebeat更能节省机器性能.

自定义jmeter.yml文件

代码语言:javascript
复制
#=========================== Filebeat inputs =============================

filebeat.inputs:

- type: log

  # Change to true to enable this input configuration.
  enabled: true

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /Users/xinxi/Documents/jmeter/apache-jmeter-5.0/bin/jmeter.log
  multiline.pattern: ^[0-9]{4}-[0-9]{2}-[0-9]{2}
  multiline.negate: true
  multiline.match: after  

#============================= Filebeat modules ===============================

filebeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml

  # Set to true to enable config reloading
  reload.enabled: false

  # Period on which files under path should be checked for changes
  #reload.period: 10s

#==================== Elasticsearch template setting ==========================

setup.template.settings:
  index.number_of_shards: 1
  #index.codec: best_compression
  #_source.enabled: false

#============================== Kibana =====================================

# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
setup.kibana:

  # Kibana Host
  # Scheme and port can be left out and will be set to the default (http and 5601)
  # In case you specify and additional path, the scheme is required: http://localhost:5601/path
  # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
  #host: "localhost:5601"

  # Kibana Space ID
  # ID of the Kibana Space into which the dashboards should be loaded. By default,
  # the Default Space will be used.
  #space.id:

#================================ Outputs =====================================

# Configure what output to use when sending the data collected by the beat.

#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["192.168.143.242:9200"]

  # Optional protocol and basic auth credentials.
  #protocol: "https"
  #username: "elastic"
  #password: "changeme"

#----------------------------- Logstash output --------------------------------
#output.logstash:
  # The Logstash hosts
  #hosts: ["localhost:5044"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"

  # Client Certificate Key
  #ssl.key: "/etc/pki/client/cert.key"

#================================ Processors =====================================

# Configure processors to enhance or manipulate events generated by the beat.

processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~

启动filebeat

代码语言:javascript
复制
./filebeat -e -c filebeat_jmeter.yml

展示

使用es后置配置监听器

另外一种直接在jmeter中使用es的后端监听器

es插件

代码语言:javascript
复制
下载: https://github.com/xinxi1990/perf-es.git

mvn pageage打包后放到ext下

jmeter插件

展示

参考

Jmeter-Beanshell Assertion-内置变量的使用<br> https://www.jianshu.com/p/66587703551c

性能监控之JMeter分布式压测轻量日志解决方案<br> https://mp.weixin.qq.com/s?__biz=MzIwNDY3MDg1OA==&mid=2247484507&idx=1&sn=545ddf373a11d2380192f44edacbca09&chksm=973dd6bea04a5fa8667db03b3e6fe21cb214e12eda9e9d6570207e9559e862a3c49c7a05ba78&scene=21#wechat_redirect

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-01-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 测试加 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • BeanShell
  • elk
    • 安装镜像
      • linux配置
      • logstash
        • logstash配置
          • 启动命令
          • filebeat
            • 自定义jmeter.yml文件
              • 启动filebeat
                • 展示
                • 使用es后置配置监听器
                  • es插件
                    • jmeter插件
                      • 展示
                      • 参考
                      相关产品与服务
                      Elasticsearch Service
                      腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档