使用FreeMarker模板动态处理JMeter运行脚本

JMeter是一个性能测试的开源框架,它提供了图形界面和非图形界面两种动行方式。最近在研究搭建一个性能测试的平台,希望通过.jmx文件,在非图形界面的linux上运行。.jmx文件通过页面配置。我们平台希望支持Java和Http请求两种方式的性能测试,通过收集性能测试时的QPS, 响应时间,线程数,主机监控等指标来观察应用程序的性能情况。
FreeMarker在百度百科是这样描述的(https://baike.baidu.com/item/freemarker/9489366?fr=aladdin)是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML、网页、电子邮件、配置文件、源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
在平台中,我们先根据需要创建.jmx模板文件,Java请求的模板如下:

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.9" jmeter="3.2 r1790748">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="测试计划" enabled="true">
      <stringProp name="TestPlan.comments"></stringProp>
      <boolProp name="TestPlan.functional_mode">false</boolProp>
      <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
        <collectionProp name="Arguments.arguments"/>
      </elementProp>
     
      <stringProp name="TestPlan.user_define_classpath">${UD_CLASSPATH}</stringProp>      <!-- 这里可以写入用户自定义的类路径,可以是目录,可以是jar包;可以添加多项,每一项通过逗号分隔-->
    </TestPlan>
    <hashTree>
      <com.blazemeter.jmeter.threads.concurrency.ConcurrencyThreadGroup guiclass="com.blazemeter.jmeter.threads.concurrency.ConcurrencyThreadGroupGui" testclass="com.blazemeter.jmeter.threads.concurrency.ConcurrencyThreadGroup" testname="bzm - Concurrency Thread Group" enabled="true">
        <elementProp name="ThreadGroup.main_controller" elementType="com.blazemeter.jmeter.control.VirtualUserController"/>
        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
        <stringProp name="TargetLevel">${UD_ThreadNum}</stringProp>   <!--这里定义要启动的并发数--> <!---->
        <stringProp name="RampUp">${UD_RampUp}</stringProp>   <!--这里定义启动这些并发数所需要的时间-->
        <stringProp name="Steps">${UD_Steps}</stringProp>   <!--这里定义启动这些并发数需要几个阶段-->
        <stringProp name="Hold">${UD_HoldTime}</stringProp>    <!--这里定义启动这些线后,保持压力多久-->
        <stringProp name="LogFilename"></stringProp>
        <stringProp name="Iterations"></stringProp>
        <stringProp name="Unit">M</stringProp>
      </com.blazemeter.jmeter.threads.concurrency.ConcurrencyThreadGroup>
      <hashTree>
        <CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV Data Set Config" enabled="${UD_isArg}"><!--是否需要数据文件,default false-->
          <stringProp name="delimiter">${UD_Delimiter}</stringProp>    <!--这里定义数据文件中一条记录的分割方式位置-->
          <stringProp name="fileEncoding">utf-8</stringProp>
          <stringProp name="filename">${UD_DataFileName}</stringProp>    <!--这里定义数据文件的位置-->
          <boolProp name="ignoreFirstLine">false</boolProp>
          <boolProp name="quotedData">false</boolProp>
          <boolProp name="recycle">true</boolProp>
          <stringProp name="shareMode">shareMode.all</stringProp>
          <boolProp name="stopThread">false</boolProp>
          <stringProp name="variableNames">${UD_VariableName}</stringProp>    <!--这里定义参数名称,使用逗号分隔-->
        </CSVDataSet>
        <hashTree/>
        <JavaSampler guiclass="JavaTestSamplerGui" testclass="JavaSampler" testname="Java请求" enabled="true">
          <elementProp name="arguments" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" enabled="true">
            <collectionProp name="Arguments.arguments">
            <#list elementProp as elementProp>
              <elementProp name="${elementProp.UD_ArgName}" elementType="Argument">    
                <stringProp name="Argument.name">${elementProp.UD_ArgName}</stringProp>    <!-- 这里填写参数名称,有多个时,循环写多段 -->
                <stringProp name="Argument.value">${elementProp.UD_ArgValue}</stringProp>   <!-- 这里填写参数值,如果是参数数据,需要从数据文件中取出,则写参数名,如 "${elementProp.query}" -->
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
             </#list>
            </collectionProp>
          </elementProp>
          <stringProp name="classname">${UD_TestClass}</stringProp>       <!--这里定义自定义JavaSampler实现类的全路径-->
        </JavaSampler>
        <hashTree/>
        <ConstantTimer guiclass="ConstantTimerGui" testclass="ConstantTimer" testname="固定定时器" enabled="true">
          <stringProp name="ConstantTimer.delay">${UD_Timer}</stringProp>   <!--这里定义自定义延时器的时长-->
        </ConstantTimer>
        <hashTree/>
        <BackendListener guiclass="BackendListenerGui" testclass="BackendListener" testname="Backend Listener" enabled="true">
          <elementProp name="arguments" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" enabled="true">
            <collectionProp name="Arguments.arguments">
              <elementProp name="graphiteMetricsSender" elementType="Argument">
                <stringProp name="Argument.name">graphiteMetricsSender</stringProp>
                <stringProp name="Argument.value">org.apache.jmeter.visualizers.backend.graphite.TextGraphiteMetricsSender</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="graphiteHost" elementType="Argument">
                <stringProp name="Argument.name">graphiteHost</stringProp>
                <stringProp name="Argument.value">${UD_GraphiteHost}</stringProp>    <!--这里填写graphiteHost-->
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="graphitePort" elementType="Argument">
                <stringProp name="Argument.name">graphitePort</stringProp>
                <stringProp name="Argument.value">${UD_GraphitePort}</stringProp>     <!--这里填写graphitePort-->
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="rootMetricsPrefix" elementType="Argument">
                <stringProp name="Argument.name">rootMetricsPrefix</stringProp>
                <stringProp name="Argument.value">${UD_RootMetricsPrefix}</stringProp>    <!--这里填写rootMetricsPrefix,默认值为 "jmeter."-->
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="summaryOnly" elementType="Argument">
                <stringProp name="Argument.name">summaryOnly</stringProp>
                <stringProp name="Argument.value">true</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="samplersList" elementType="Argument">
                <stringProp name="Argument.name">samplersList</stringProp>
                <stringProp name="Argument.value"></stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="percentiles" elementType="Argument">
                <stringProp name="Argument.name">percentiles</stringProp>
                <stringProp name="Argument.value">90;95;99</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
            </collectionProp>
          </elementProp>
          <stringProp name="classname">org.apache.jmeter.visualizers.backend.graphite.GraphiteBackendListenerClient</stringProp>
        </BackendListener>
        <hashTree/>
      </hashTree>
    </hashTree>
    <WorkBench guiclass="WorkBenchGui" testclass="WorkBench" testname="工作台" enabled="false">
      <boolProp name="WorkBench.save">true</boolProp>
    </WorkBench>
    <hashTree/>
  </hashTree>
</jmeterTestPlan>

模板中用${}包起来的字段即为我们要替换的字段
处理的关键代码如下:

Configuration configuration = new Configuration(Configuration.VERSION_2_3_25);
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
configuration.setDirectoryForTemplateLoading(new File(classLoader.getResource("").getPath()));
            configuration.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_25));
Template template = template = configuration.getTemplate("JavaSampler_Templete.jmx","UTF-8");//加载模板文件
Map<String, Object> rootMap = new HashMap<>();//用map处理模板中的字段
rootMap.put("UD_CLASSPATH", "/");
rootMap.put("UD_ThreadNum", 1);
rootMap.put("UD_RampUp", 1);
rootMap.put("UD_Steps", 1);
rootMap.put("UD_HoldTime", 1);
rootMap.put("UD_Timer", "1000");
rootMap.put("UD_isArg", "false");
rootMap.put("UD_Domain", "www.baidu.com");
rootMap.put("UD_Port", "80");
rootMap.put("UD_Protocol", "");
rootMap.put("UD_Path", "");
rootMap.put("UD_Delimiter", "");
rootMap.put("UD_DataFileName", "");
rootMap.put("UD_VariableName", "");
rootMap.put("UD_GraphiteHost", "localhost");
rootMap.put("UD_GraphitePort", "2003");
rootMap.put("UD_RootMetricsPrefix", "jmeter.");
List<Map<String, Object>> paraList = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("UD_ArguName", "");
map.put("UD_ArguValue", "");
map.put("testName", "");
paraList.add(map);
rootMap.put("elementProp", paraList);// 用list处理模板中list的部分

根据上述设置即可生成可执行的模板文件。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,699评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 堆栈 栈 存储局部变量 存储方法调用 堆 存储Java对象(成员变量 局部变量 类变量 ->指向的对象都存储在堆内...
    cd2016阅读 304评论 0 1
  • 《可爱的诅咒》这本书相信很多人看过吧?我是在小她知道这本书的当时有人发帖问大家有没有一本在情绪低落的时候可以反复看...
    光向上走阅读 564评论 0 0