Devops关键工具及技术(七)—基于Pipeline的Jmeter性能测试[一](Jmeter容器化)

上三篇文章中,我们从windows下Web自动化测试工具的安装再到将Web自动化测试工具RobotFramework+Selenium+Chome容器化,最后利用容器化后的自动化工具与Jenkins Docker Cloud集成,并在Pipeline中实现Web的自动化测试。
三篇文章链接见下:
Devops关键工具及技术(六)—基于Pipeline的Web自动化测试(Windows下自动化测试工具安装)
Devops关键工具及技术(六)—基于Pipeline的Web自动化测试(Web自动化测试工具容器化)
Devops关键工具及技术(六)—基于Pipeline的Web自动化测试(Web自动化测试的流水线集成)

有了自动化测试、自然性能测试也需要成为Pipeline的一部分。性能测试的工具常见的主要有Jmeter和LoadRunner。我们将选用Jmeter作为我们的主角。因为它是免费的。由于在Windows下安装Jmeter比较简单,所以我们在Jmeter性能测试会由两种文章来介绍。分别是Jmeter性能测试容器化、Jmeter性能测试的流水线集成。

本篇我们将先介绍Jmeter性能测试的容器化。
以下所有的文件代码都可以在Github找到:
https://github.com/zbbkeepgoing/containerization

1、容器化

Jmeter容器化的目的和Web自动化测试工具的容器化一样,是为了能够将Jmeter加入到我们的Jenkins中成为一个 节点,这样Jmeter将会很容易地集成到我们pipeline流水线中。
下面是Jmeter的Dockerfile文件

FROM openjdk:8-jdk

ENV http_proxy http://192.168.88.130:8118      //增加代理,如果无需代理可以删除
ENV https_proxy https://192.168.88.130:8118    //增加代理,如果无需代理可以删除

ENV JMETER_VERSION=5.0       //定义环境变量
ENV JMETER_HOME=/usr/local/apache-jmeter-${JMETER_VERSION}
ENV PATH=${JMETER_HOME}/bin:${PATH}

RUN apt-get -y update && \     //安装必要软件
	apt-get -y install wget 

RUN wget http://www.eu.apache.org/dist/jmeter/binaries/apache-jmeter-${JMETER_VERSION}.tgz && \
	tar -xzf apache-jmeter-${JMETER_VERSION}.tgz -C /usr/local/    //下载Jmeter的包并解压

RUN rm -rf apache-jmeter-${JMETER_VERSION}.tgz \
			${JMETER_HOME}/bin/examples \
			${JMETER_HOME}/bin/templates \
			${JMETER_HOME}/bin/*.cmd \
			${JMETER_HOME}/bin/*.bat \
			${JMETER_HOME}/docs \
			${JMETER_HOME}/printable_docs && \
	apt-get -y remove wget && \
	apt-get -y --purge autoremove && \
	apt-get -y clean && \
	rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

ENV http_proxy ""
ENV https_proxy ""   //取消代理

根据Dockerfile镜像Docker的构建。

 docker build -t jmeter:latest .

构建完成后运行

docker run --name jmeter -it -d jmeter:latest

测试

  • 测试Jmeter容器化是否都已成功安装
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                                            NAMES
90404438810e        jmeter:latest       "bash"              About a minute ago   Up About a minute                                                    jmeter
11f7e107e7db        sonarqube           "./bin/run.sh"      14 hours ago         Up 14 hours         0.0.0.0:9000->9000/tcp, 0.0.0.0:9092->9092/tcp   sonarqube
[root@localhost ~]# docker exec -it 90404438810e sh
# jmeter.sh --help
Oct 20, 2018 6:00:22 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
    _    ____   _    ____ _   _ _____       _ __  __ _____ _____ _____ ____     
   / \  |  _ \ / \  / ___| | | | ____|     | |  \/  | ____|_   _| ____|  _ \   
  / _ \ | |_) / _ \| |   | |_| |  _|    _  | | |\/| |  _|   | | |  _| | |_) | 
 / ___ \|  __/ ___ \ |___|  _  | |___  | |_| | |  | | |___  | | | |___|  _ <  
/_/   \_\_| /_/   \_\____|_| |_|_____|  \___/|_|  |_|_____| |_| |_____|_| \_\ 5.0 r1840935  

Copyright (c) 1999-2018 The Apache Software Foundation


To list all command line options, open a command prompt and type:

jmeter.bat(Windows)/jmeter.sh(Linux) -?

--------------------------------------------------

To run Apache JMeter in GUI mode, open a command prompt and type:

jmeter.bat(Windows)/jmeter.sh(Linux) [-p property-file]

--------------------------------------------------

To run Apache JMeter in NON_GUI mode:
Open a command prompt (or Unix shell) and type:

jmeter.bat(Windows)/jmeter.sh(Linux) -n -t test-file [-p property-file] [-l results-file] [-j log-file]

--------------------------------------------------

To run Apache JMeter in NON_GUI mode and generate a report at end :
Open a command prompt (or Unix shell) and type:

jmeter.bat(Windows)/jmeter.sh(Linux) -n -t test-file [-p property-file] [-l results-file] [-j log-file] -e -o [Path to output folder]

--------------------------------------------------
To generate a Report from existing CSV file:
Open a command prompt (or Unix shell) and type:

jmeter.bat(Windows)/jmeter.sh(Linux) -g [csv results file] -o [path to output folder (empty or not existing)]

--------------------------------------------------

To tell Apache JMeter to use a proxy server:
Open a command prompt and type:

jmeter.bat(Windows)/jmeter.sh(Linux) -H [your.proxy.server] -P [your proxy server port]

---------------------------------------------------

To run Apache JMeter in server mode:
Open a command prompt and type:

jmeter-server.bat(Windows)/jmeter-server(Linux)

---------------------------------------------------

# 

  • 测试Baidu的Demo性能测试脚本

脚本可以在这里找到:https://github.com/zbbkeepgoing/containerization/blob/master/Jmeter/baidu.jmx



  
    
      
      false
      true
      false
      
        
      
      
    
    
      
        continue
        
          false
          10
        
        1
        1
        false
        
        
      
      
        ............
        true
              0
              true
              true
              true
              true
              true
              true
            
          
          
        
        
      
    
  

进入容器进行测试

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                            NAMES
90404438810e        jmeter:latest       "bash"              16 minutes ago      Up 16 minutes                                                        jmeter
11f7e107e7db        sonarqube           "./bin/run.sh"      15 hours ago        Up 15 hours         0.0.0.0:9000->9000/tcp, 0.0.0.0:9092->9092/tcp   sonarqube
[root@localhost ~]# docker cp baidu.jmx 90404438810e:/opt
[root@localhost ~]# docker exec -it 90404438810e sh
# mkdir -p /opt/output
# ls
bin  boot  dev	docker-java-home  etc  home  jmeter.log  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
# cd /opt
# ls
baidu.jmx  output
# jmeter.sh -n -t /opt/baidu.jmx  -l /opt/baidu.jtl -j /opt/baidu.log -e -o /opt/output
Creating summariser 
Created the tree successfully using /opt/baidu.jmx
Starting the test @ Sat Oct 20 06:18:46 UTC 2018 (1540016326114)
Waiting for possible Shutdown/StopTestNow/Heapdump message on port 4445
summary +      2 in 00:00:02 =    1.3/s Avg:   409 Min:   378 Max:   440 Err:     0 (0.00%) Active: 1 Started: 1 Finished: 0
summary +      8 in 00:00:02 =    4.2/s Avg:   235 Min:   109 Max:   496 Err:     0 (0.00%) Active: 0 Started: 1 Finished: 1
summary =     10 in 00:00:03 =    2.9/s Avg:   269 Min:   109 Max:   496 Err:     0 (0.00%)
Tidying up ...    @ Sat Oct 20 06:19:02 UTC 2018 (1540016342106)
... end of run
# ls
baidu.jmx  baidu.jtl  baidu.log  output
# cd output
# ls
README.TXT  content  index.html  sbadmin2-1.0.7
#

以上即是Jmeter容器化的内容,后面将会利用容器化后的镜像与Jenkins、Pipeline结合进行性能测试的集成

你可能感兴趣的:(Devops)