jmeter icon indicating copy to clipboard operation
jmeter copied to clipboard

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

Open artemkurov opened this issue 1 year ago • 2 comments

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:

  • setUp thread group, which contains --- init JSR223 Sampler
  • main thread group with --- Dummy sampler --- Simple Data Writer (or any listener)
  • tear down thread group with --- JSR223 Sampler

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)

artemkurov avatar Aug 01 '24 16:08 artemkurov

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:

  • using Fileserver inside a groovy function to set the filename (instead of taking the route via a runtime set property)
  • using a property set via command line

FSchumacher avatar Aug 10 '24 09:08 FSchumacher

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

artemkurov avatar Aug 10 '24 15:08 artemkurov