• 周六. 7 月 27th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

流量回复学习记录二

admin

11 月 28, 2021

   继续上次的学习记录,因为整个流量回放的过程已经开发完成,所以可以来复盘一下。

   现在各类压测平台都使用 jmeter 执行压测,jmeter 是如何判断什么时候执行什么步骤?

   在一中提到,jmeter的脚本是 hashtree,为了保证压测(回放) 能按照顺序执行请求,因此可以采用 ListedHashTree 来根据添加到先后顺序决定执行顺序,同样的,每个 node 都是一个步骤。所以对于jmeter来说,常见的执行发送http请求的场景,通过 httpsampler 完成,在 listedhashtree 里,会使用 httpsamplerproxy 来完成这件事情。

   生成的顺序是,HttpSamplerProxy init -> TestPlan init -> BackendListener init -> BackendListener added to TestPlan -> TestPlan passed to Engine

   除了最后一步,一定是一个 testplan 提交给 jmeter 引擎外,其他步骤都是可以按不同顺序操作。

   如何初始化一个 sampler? sampler 里包含什么信息?

        HTTPSamplerProxy sampler = new HTTPSamplerProxy();

        sampler.setEnabled(true);
        sampler.setName("请求名称");
        sampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
        sampler.setProperty(TestElement.GUI_CLASS, "HttpTestSampleGui");
        sampler.setProperty(TestPlan.COMMENTS,"");
        sampler.setContentEncoding("UTF-8");
        sampler.setFollowRedirects(true);
        sampler.setAutoRedirects(false);
        sampler.setUseKeepAlive(true);
        sampler.setDoMultipartPost(false);
        sampler.setConnectTimeout("");
        sampler.setResponseTimeout("");
        sampler.setEmbeddedUrlRE("");

        sampler.setMethod(info.getJSONObject("request").getString("method"));
        String requestUrl = info.getJSONObject("request").getString("url");
        sampler.setPostBodyRaw(true);

        try{
            if(!StringUtils.isEmpty(info.getJSONObject("request").getString("body"))){
                JSONObject body = JSONObject.parseObject(info.getJSONObject("request").getString("body"));
                sampler.setArguments(addHttpArguments(body));

            }
        } catch (Exception e){
            log.info("Request body is not key-value pair!");
        }
        URL url = null;
        try {
            url = new URL(requestUrl);
            sampler.setDomain(URLDecoder.decode(url.getHost(),"UTF-8"));
            sampler.setPath(URLDecoder.decode(url.getPath() + "?" + url.getQuery(), "UTF-8"));
            sampler.setProtocol(URLDecoder.decode(url.getProtocol(),"UTF-8"));
            if (url.getPort() == -1 && url.getProtocol().equals("http")){
                sampler.setPort(80);
            }else if (url.getPort() == -1 && url.getProtocol().equals("https")){
                sampler.setPort(443);
            }else{
                sampler.setPort(url.getPort());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

     比较坑的一点是,httpsamplerproxy没有支持设置 rawdata的方法,尽管有

sampler.setPostBodyRaw(true);

  除了能设置 true/false , 其他在发送http 请求时的body部分,如果是Keyvalue pair ,需要自己转成 argument,如果是文件,需要转成二进制文件,看了好久的 jmeter官方文档,才确认了这一点,就是设置不了……

   

发表回复