jmeter beanshell调用jmeter函数

时间:2019-03-20 20:17:03

标签: jmeter beanshell

我配置了一个HTTP请求采样器。在请求正文中,我调出我编写的beanshell函数:

${__BeanShell(createHeader("GET"\,"Customer"\,"${__UUID}"\,"${__time(yyyy-MM-dd'T'hh:mm:ss)}"))}

该函数仅使用传入的参数构建一些请求字符串。我想删除jmeter函数调用(__UUID和__time),然后直接从beanshell函数中调用它们。我找不到办法。 谢谢

1 个答案:

答案 0 :(得分:1)

  1. 不要将JMeter Functions or Variables内联到脚本中,在这种情况下,您将不得不使用基于代码的等效项,从而:

    • // Open reader using (var reader = Cmd.ExecuteReader()) { // Read while cursor has records to fetch while (reader.Read()) { // current record read string datoI = Convert.ToString(reader[0]); // which we append to file tw1.WriteLine(datoI); } } -> sqliteCon.Open(); ... //DONE: Keep SQL readable string query = @"SELECT tabStoricoDetail.id FROM tabStoreExec JOIN tabStoricoDetail ON tabStoreExec.idSE = tabStoricoDetail.id WHERE tabStoricoDetail.NomeItem LIKE @prmNome"; //DONE: wrap IDisposable into using using (SqlCommand command = new SqlCommand(query, sqliteCon)) { //DONE: paramterize the query, do not hardcode it //TODO: .Add(value, rdbmsType); is a better approach then .AddWithValue(value); command.Parameters.AddWithValue("@prmNome", this.txtSrcVR.Text); using (var reader = command.ExecuteReader()) { //DONE: do not concat strings but use Path.Combine string file = Path.Combine(@"D:\Reports", filename); using (TextWriter writer = new StreamWriter(file, true)) { while (reader.Read()) { //DONE: Convert.To is a safier (culture independent) approach then .ToString() string record = Convert.ToString(reader[0]); writer.WriteLine(record); } } } }
    • __UUID()-> UUID.randomUUID().toString()
  2. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language用于脚本编写,在您的情况下为__groovy() function。如果要重复使用__time()方法-可以将其放入单独的.groovy文件中,并定义指向该文件的groovy.utilities property

    有关在JMeter测试中使用Groovy脚本的更多信息,请参见Apache Groovy - Why and How You Should Use It文章。