jmeter向kafka写入数据,以供测试消费

#将以下代码保存为.jmx文件即可以jmeter打开

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.1.1 r1855137">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="kafka造数据" enabled="true">
      <stringProp name="TestPlan.comments"></stringProp>
      <boolProp name="TestPlan.functional_mode">false</boolProp>
      <boolProp name="TestPlan.tearDown_on_shutdown">true</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"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="轨迹模拟车机" enabled="true">
        <stringProp name="ThreadGroup.on_sample_error">stopthread</stringProp>
        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" enabled="true">
          <boolProp name="LoopController.continue_forever">false</boolProp>
          <stringProp name="LoopController.loops">10</stringProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">1</stringProp>
        <stringProp name="ThreadGroup.ramp_time">1</stringProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
      </ThreadGroup>
      <hashTree>
        <Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="用户定义的变量" enabled="true">
          <collectionProp name="Arguments.arguments">
            <elementProp name="KAFKA_BROKERS" elementType="Argument">
              <stringProp name="Argument.name">KAFKA_BROKERS</stringProp>
              <stringProp name="Argument.value">ip:9092 </stringProp>
              <stringProp name="Argument.metadata">=</stringProp>
              <stringProp name="Argument.desc">kafka服务器地址</stringProp>
            </elementProp>
            <elementProp name="KAFKA_TOPIC" elementType="Argument">
              <stringProp name="Argument.name">KAFKA_TOPIC</stringProp>
              <stringProp name="Argument.value">netvehicle_gps</stringProp>
              <stringProp name="Argument.metadata">=</stringProp>
              <stringProp name="Argument.desc">发送的主题名称</stringProp>
            </elementProp>
            <elementProp name="KAFKA_TOPIC_PARTITIONS" elementType="Argument">
              <stringProp name="Argument.name">KAFKA_TOPIC_PARTITIONS</stringProp>
              <stringProp name="Argument.value">2</stringProp>
              <stringProp name="Argument.metadata">=</stringProp>
              <stringProp name="Argument.desc">发送到哪个分区,没有或不知道就填1</stringProp>
            </elementProp>
            <elementProp name="KAFKA_KEY" elementType="Argument">
              <stringProp name="Argument.name">KAFKA_KEY</stringProp>
              <stringProp name="Argument.value"></stringProp>
              <stringProp name="Argument.metadata">=</stringProp>
              <stringProp name="Argument.desc">发送key,没有则为空,不能是空格</stringProp>
            </elementProp>
            <elementProp name="KAFKA_MESSAGE" elementType="Argument">
              <stringProp name="Argument.name">KAFKA_MESSAGE</stringProp>
              <stringProp name="Argument.value">{&quot;accuracy&quot;:0,&quot;deviceType&quot;:200,&quot;enduranceMileage&quot;:0,&quot;lat&quot;:31.01,&quot;lon&quot;:121.99,&quot;mileage&quot;:1000,&quot;remainElectric&quot;:0,&quot;speed&quot;:0.0,&quot;timestamp&quot;:1599132376918,&quot;vin&quot;:&quot;test1898&quot;}</stringProp>
              <stringProp name="Argument.metadata">=</stringProp>
              <stringProp name="Argument.desc">要发送的数据</stringProp>
            </elementProp>
          </collectionProp>
        </Arguments>
        <hashTree/>
        <JavaSampler guiclass="JavaTestSamplerGui" testclass="JavaSampler" testname="车载轨迹上报kafka模拟" enabled="true">
          <elementProp name="arguments" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" enabled="true">
            <collectionProp name="Arguments.arguments">
              <elementProp name="kafka服务地址" elementType="Argument">
                <stringProp name="Argument.name">kafka服务地址</stringProp>
                <stringProp name="Argument.value">${KAFKA_BROKERS}</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="kafka主题" elementType="Argument">
                <stringProp name="Argument.name">kafka主题</stringProp>
                <stringProp name="Argument.value">${KAFKA_TOPIC}</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="kafka分区" elementType="Argument">
                <stringProp name="Argument.name">kafka分区</stringProp>
                <stringProp name="Argument.value">${KAFKA_TOPIC_PARTITIONS}</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="kafka_key" elementType="Argument">
                <stringProp name="Argument.name">kafka_key</stringProp>
                <stringProp name="Argument.value"> </stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="kafka_message消息" elementType="Argument">
                <stringProp name="Argument.name">kafka_message消息</stringProp>
                <stringProp name="Argument.value">${KAFKA_MESSAGE}</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="kafka_message_serializer" elementType="Argument">
                <stringProp name="Argument.name">kafka_message_serializer</stringProp>
                <stringProp name="Argument.value">org.apache.kafka.common.serialization.ByteArraySerializer</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
              <elementProp name="kafka_key_serializer" elementType="Argument">
                <stringProp name="Argument.name">kafka_key_serializer</stringProp>
                <stringProp name="Argument.value">org.apache.kafka.common.serialization.ByteArraySerializer</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
            </collectionProp>
          </elementProp>
          <stringProp name="classname">co.signal.kafkameter.KafkaProducerSampler</stringProp>
        </JavaSampler>
        <hashTree>
          <BeanShellPreProcessor guiclass="TestBeanGUI" testclass="BeanShellPreProcessor" testname="kafka请求报文组装" enabled="true">
            <stringProp name="filename"></stringProp>
            <stringProp name="parameters"></stringProp>
            <boolProp name="resetInterpreter">false</boolProp>
            <stringProp name="script">import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

        JSONObject json = new JSONObject();
        //精度
        json.put(&quot;accuracy&quot;,&quot;0&quot;);
        //
        json.put(&quot;deviceType&quot;,&quot;200&quot;);
        //
        json.put(&quot;enduranceMileage&quot;,&quot;0&quot;);
        //
        json.put(&quot;remainElectric&quot;,&quot;0&quot;);
        //纬度
        json.put(&quot;lat&quot;,&quot;31.01&quot;);
        //经度
        json.put(&quot;lon&quot;,&quot;121.99&quot;);
        //里程
        json.put(&quot;mileage&quot;,&quot;1000&quot;);
        //速度
        json.put(&quot;speed&quot;,&quot;${__Random(20,30,)}.${__Random(0,9,)}&quot;);
        //时间戳
        json.put(&quot;timestamp&quot;,&quot;${__time(,)}&quot;);
        //车辆vin码
//        json.put(&quot;vin&quot;,&quot;test1898&quot;);
    json.put(&quot;vin&quot;,&quot;vinum&quot;);
        
String reqJson=json.toJSONString();
vars.put(&quot;KAFKA_MESSAGE&quot;,reqJson);</stringProp>
          </BeanShellPreProcessor>
          <hashTree/>
          <ConstantTimer guiclass="ConstantTimerGui" testclass="ConstantTimer" testname="间隔1秒上传" enabled="true">
            <stringProp name="ConstantTimer.delay">1000</stringProp>
          </ConstantTimer>
          <hashTree/>
        </hashTree>
        <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="察看结果树" enabled="true">
          <boolProp name="ResultCollector.error_logging">false</boolProp>
          <objProp>
            <name>saveConfig</name>
            <value class="SampleSaveConfiguration">
              <time>true</time>
              <latency>true</latency>
              <timestamp>true</timestamp>
              <success>true</success>
              <label>true</label>
              <code>true</code>
              <message>true</message>
              <threadName>true</threadName>
              <dataType>true</dataType>
              <encoding>false</encoding>
              <assertions>true</assertions>
              <subresults>true</subresults>
              <responseData>false</responseData>
              <samplerData>false</samplerData>
              <xml>false</xml>
              <fieldNames>true</fieldNames>
              <responseHeaders>false</responseHeaders>
              <requestHeaders>false</requestHeaders>
              <responseDataOnError>false</responseDataOnError>
              <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
              <assertionsResultsToSave>0</assertionsResultsToSave>
              <bytes>true</bytes>
              <sentBytes>true</sentBytes>
              <url>true</url>
              <threadCounts>true</threadCounts>
              <idleTime>true</idleTime>
              <connectTime>true</connectTime>
            </value>
          </objProp>
          <stringProp name="filename"></stringProp>
        </ResultCollector>
        <hashTree/>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>
 

  • 11
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值