简体   繁体   English

访问 JMeter 脚本路径

[英]Access to JMeter script path

Is there a way of accessing the current script's absolute physical path via a variable/property?有没有办法通过变量/属性访问当前脚本的绝对物理路径? There doesn't appear to be anything listed via a Debug Sampler.通过调试采样器似乎没有列出任何内容。

It's incredibly annoying that actions like loading CSV files and JMX Includes uses the current working directory as its relative path.诸如加载 CSV 文件和 JMX Includes 之类的操作使用当前工作目录作为其相对路径,这非常令人讨厌。

I used the answer provided by haridsv.我使用了haridsv提供的答案。 It worked great except that I needed to put the directory to the JMX file in a variable.除了我需要将目录放入变量中的 JMX 文件之外,它工作得很好。 I made a "User Defined Variables" component and used BeanShell in the variable's "Value" field like this:我制作了一个“用户定义的变量”组件,并在变量的“值”字段中使用了 BeanShell,如下所示:

${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}

The first BeanShell section calls the Java class that gets the directory in question.第一个 BeanShell 部分调用获取相关目录的 Java 类。 The second appends a file separator to the path, which is of course optional.第二个将文件分隔符附加到路径,这当然是可选的。

  • Include Controller 包括控制器
    As per component's reference:根据组件的参考:

    This element does not support variables/functions in the filename field.此元素不支持文件名字段中的变量/函数。
    However, if the property includecontroller.prefix is defined, the contents are used to prefix the pathname.但是,如果定义了属性 includecontroller.prefix,则使用内容作为路径名的前缀。 If the file cannot be found at the location given by prefix+filename, then the controller attempts to open the fileName relative to the JMX launch directory (versions of JMeter after 2.3.4).如果在前缀+文件名给定的位置找不到文件,则控制器尝试打开相对于 JMX 启动目录的文件名(JMeter 2.3.4 之后的版本)。

    You can pass JMeter a java property named includecontroller.prefix which can be used to prepend a directory to the JMX file you're including.您可以向 JMeter 传递一个名为 includecontroller.prefix 的 java 属性,该属性可用于将目录添加到您包含的 JMX 文件中。

    1) In case of console launch use: 1) 在控制台启动的情况下使用:

    -Jincludecontroller.prefix=/full/path/to/jmx/scripts/dir/

    2) in case of GUI - add the same to .sh/.cmd/.bat file or write a wrapper file; 2) 在 GUI 的情况下 - 将相同的内容添加到 .sh/.cmd/.bat 文件或编写包装文件;
    3) in case of Jmeter Ant Task usage - set as separate property: 3) 在使用Jmeter Ant Task 的情况下 - 设置为单独的属性:

     <jmeter jmeterhome="${jmeter.home}" testplan="..." resultlog="..."> <property name="jmeter.save.saveservice.assertion_results" value="all"/> <property name="jmeter.save.saveservice.output_format" value="xml"/> <property name="includecontroller.prefix" value="..."/> </jmeter>
  • CSV Data Set Config CSV 数据集配置
    As per component's reference:根据组件的参考:

    Relative file names are resolved with respect to the path of the active test plan.相对文件名根据活动测试计划的路径进行解析。
    Absolute file names are also supported, but note that they are unlikely to work in remote mode, unless the remote server has the same directory structure.也支持绝对文件名,但请注意,它们不太可能在远程模式下工作,除非远程服务器具有相同的目录结构。 If the same physical file is referenced in two different ways - eg csvdata.txt and ./csvdata.txt - then these are > > treated as different files.如果以两种不同的方式引用同一个物理文件 - 例如 csvdata.txt 和 ./csvdata.txt - 那么这些将被 > > 视为不同的文件。 If the OS does not distinguish between upper and lower case, csvData.TXT would also be opened separately.如果操作系统不区分大小写,csvData.TXT 也会单独打开。


    You can declare a test plan variable that retrieves parameter value with the folder containing csv data files:您可以声明一个测试计划变量,该变量使用包含 csv 数据文件的文件夹检索参数值:
    eg例如

    csv.path | ${__P(csv.path, ${__property(user.dir)}${__BeanShell(File.separator,)})}

     CSV Data Set Config Filename = ${csv.path}${__P(users-list,)}

    Setting from console:从控制台设置:

     -Jcsv.path=/full/path/to/csv/data/dir/

    Setting for distributed testing setup:分布式测试设置的设置:

     -Gcsv.path=/full/path/to/csv/data/dir/

By saying "current script's absolute physical path", I am guessing OP is referring to the location where the testplan (jmx file) is loaded from.通过说“当前脚本的绝对物理路径”,我猜 OP 指的是加载测试计划(jmx 文件)的位置。 I needed exactly this to generate a CSV file from BeanShell script at the beginning of the run, which is subsequently used in a CSV Data Set Config to read back, so I wanted the script to work just like how the later works when no path is specified.我正是需要这个来在运行开始时从 BeanShell 脚本生成一个 CSV 文件,随后在 CSV 数据集配置中使用它来回读,所以我希望该脚本能够像后面没有路径时的工作方式一样工作指定的。 I went through the JMeter source and found this working solution:我浏览了 JMeter 源代码并找到了这个可行的解决方案:

import org.apache.jmeter.services.FileServer;
log.info(FileServer.getFileServer().getBaseDir());

I tested this and saw the correct path in the jmeter.log.我对此进行了测试,并在 jmeter.log 中看到了正确的路径。

My particular issue was that my relative Include Controller path included a backslash which broke on Linux and OSX.我的特殊问题是我的相对 Include Controller 路径包含在 Linux 和 OSX 上损坏的反斜杠。

The solution was to use a forward slash in relative paths, which works on all platforms.解决方案是在相对路径中使用正斜杠,这适用于所有平台。

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

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