简体   繁体   English

JMeter:使用Java类为自定义标头生成值

[英]JMeter: Generate value for custom Header with a java class

i try to test the performance for our api which has a request-limiter (100requets in 100sec otherwise the ip is blocked). 我尝试测试我们的具有请求限制器的api的性能(100秒内发生100次重击,否则IP被阻止)。 You can avoid this with a custom header. 您可以使用自定义标题避免这种情况。 The value for this header is generate by a java class. 此标头的值由Java类生成。

With jruby i can use "require" and "import" too use the class. 使用jruby,我也可以使用“ require”和“ import”使用类。

How can i do this for JMeter? 我如何为JMeter做到这一点?

Additional infos: the header value is dynamic -> for each request it has to be generated. 其他信息:标头值是动态的->对于必须生成的每个请求。 To call HttpPwd.genPwd() two *.jar files are needed 要调用HttpPwd.genPwd(),需要两个* .jar文件

This is how to proceed : 这是如何进行:

  • Put the 2 Jars in jmeter/lib folder 将2个罐子放在jmeter / lib文件夹中

  • Put groovy-all.jar in jmeter/lib 将groovy-all.jar放入jmeter / lib

  • Use a JSR223 Preprocessor + Groovy 使用JSR223预处理器+ Groovy

  • Put in the Preprocessor the following code: 将以下代码放入预处理器中:

import xxxxxx; 进口xxxxxx; // where xxxx is the full name of the HttpPwd class //其中xxxx是HttpPwd类的全名

vars.put("header", HttpPwd.genPwd()); vars.put(“ header”,HttpPwd.genPwd());

  • On your request add a HeaderManager as a child and in its content put: 根据您的请求添加HeaderManager作为子项,并在其内容中添加:

HeaderName (I don't know what it is) : ${header} HeaderName(我不知道它是什么):$ {header}

In your JMeter project navigate to Thread Group > HTTP Request . 在您的JMeter项目中,导航到Thread Group > HTTP Request You can set your request parameters there. 您可以在那里设置请求参数。

Here is the way of implementing this via JMeter __Beanshell function 这是通过JMeter __Beanshell函数实现此方法的方法

Assuming that you're having following logic for generating custom header: 假设您具有以下用于生成自定义标头的逻辑:

package custom.header;

public class HttpPwd {

public static String getPwd() {
    return String.valueOf(System.currentTimeMillis());
}
}

Which is accessible from some jar called "customheader.jar" 可从一个名为“ customheader.jar”的罐子中进行访问

And you want to add a header named "MyHeader" to your request 并且您想在请求中添加一个名为“ MyHeader”的标头

  1. Put customheader.jat to lib/ext folder of your jmeter installation. 将customheader.jat放入jmeter安装的lib / ext文件夹中。 (or to location under search_paths or user.classpath if you have overriden these properties in your user.properties file) (如果您在user.properties文件中覆盖了这些属性,则返回到search_paths或user.classpath下的位置)
  2. Create a Thread Group 创建一个线程组
  3. Add ie HTTP Request to Thread Group 将HTTP请求添加到线程组
  4. In HTTP Request put Server Name or IP to be ie "google.com", Path - /, and add a parameter "q" with a value of "test". 在HTTP请求中,将服务器名称或IP输入为“ google.com”,路径-/,并添加一个值为“ test”的参数“ q”。 Actually it doesn't matter. 其实没关系。
  5. Add HTTP Header Manager AS A CHILD of HTTP Request 将HTTP标头管理器添加为HTTP请求的形式
  6. Add Name as MyHeader and Value - "${__BeanShell(import custom.header.*;HttpPwd.getPwd();)}" 将名称添加为MyHeader和值-“ $ {__ BeanShell(import custom.header。*; HttpPwd.getPwd();)}”
  7. Add View Results Tree listener after HTTP Request 在HTTP请求之后添加视图结果树侦听器
  8. Save and run 保存并运行

You should see something like: 您应该看到类似以下内容:

GET http://www.google.com/?q=test

[no cookies]

Request Headers:
Connection: keep-alive
MyHeader: 1382360571781
Host: www.google.com
User-Agent: Apache-HttpClient/4.2.3 (java 1.5)

Mention MyHeader presense and value 提及MyHeader的存在和价值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM