apache / jmeter

Apache JMeter open-source load testing tool for analyzing and measuring the performance of a variety of services
https://jmeter.apache.org/
Apache License 2.0
8.43k stars 2.11k forks source link

Listeners create folder with null path when path is evaluated as a property value inside a groovy script when run from CLI #6323

Closed artemkurov closed 1 month ago

artemkurov commented 3 months ago

Expected behavior

${__groovy(props.get("baseDir")+"/results/latency_data.csv")} evaluated to correct file path like /Users/username/projects/qa-perf/performance/jmeter/results/latency_data.csv

Actual behavior

${__groovy(props.get("baseDir")+"/results/latency_data.csv")} evaluates to null/results/latency_data.csv

image

Steps to reproduce the problem

Prerequisites:

Having a test plan (attached here bug.jmx.zip), containing:

init JSR223 Sampler having this code:

import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.services.FileServer

// Do not show this sampler in report
SampleResult.setIgnore()

// Set base directory. Mainly needed for local run
String baseDir = FileServer.getFileServer().getBaseDir()
JMeterUtils.setProperty("baseDir", baseDir)
log.info("Base directory set to " + props.get('baseDir'))

Simple Data Writer having this as a filename: ${__groovy(props.get("baseDir")+"/results/latency_data.csv")}

Tear down JSR223 Sampler having this code:

// Do not show this sampler in report
SampleResult.setIgnore()

String filePath = props.get('baseDir') + '/results/latency_data.csv'
log.info('filepath=' + filePath)
File file = new File(filePath);
if(!file.exists()) {
    log.error('FILE NOT FOUND')
}

// Pasre file
//...

Reproducing

  1. Run test plan from GUI.
  2. Observe /Users/username/projects/qa-perf/performance/jmeter/results/latency_data.csv file is created and no errors in the log
  3. Launch terminal in baseDir folder (for me is /Users/username/projects/qa-perf/performance/jmeter)
  4. Launch the same test plan via CLI using command jmeter -LINFO -n -j Buggy_Test_Plan.log -t bug.jmx -l Buggy_Test_Plan.jtl
  5. Observe unexpected behavior:
    • File /Users/username/projects/qa-perf/performance/jmeter/null/results/latency_data.csv is created
    • ERROR in logs because JSR223 sampler could not find the file.
    • INFO o.a.j.r.ResultCollector: Folder at /Users/username/projects/qa-perf/performance/jmeter/null/results was created

I assume that folder null is created because this happens before set of the variable baseDir, thus props.get("baseDir") returns null. But this flow does work through GUI mode, so should be a bug.

JMeter Version

5.6.3

Java Version

openjdk 17.0.11 2024-04-16

OS Version

MacOS Sonoma 14.5 (also reproducable on Alpine Linux in Docker container)

FSchumacher commented 3 months ago

I think you are out of luck here. The listeners are initialized before any sampler has been run and I believe the filename is set on initialization.

I wonder though, why you took the route through the groovy function to get the property instead of using the P-function. It would not work in this case, either, but is seems more straightforward.

Things, that should work include:

artemkurov commented 3 months ago

Ok I understand now. Thank you for explanation. Issue could be closed now.