0

I wrote a custom sampler in JMeter that takes a sql query and returns the response. I currently have a the CSV Data Set Config setup that takes a CSV file of sample users that I want to query in my custom sampler.

Here is my sampler and tree of files on JMeter.

I configured my jmeter test like this post stated here.

In my Sampler code, I'm extracting the inputs of the text area like this:

DbSampler.java:

public class CosmosDBSampler extends AbstractSampler {

    private static final Logger logger = LoggerFactory.getLogger(CosmosDBSampler.class);

    private static final String COSMOS_QUERY = "Cosmos.cosmosQuerys";

   
    public void setCosmosQuery(String query) {
        setProperty(COSMOS_QUERY, query);
    }

    public String getCosmosQuery() {
        return getPropertyAsString(COSMOS_QUERY, "");
    }

    @Override
    public SampleResult sample(Entry entry) {
        SampleResult result = new SampleResult();
        boolean isOK = false;
        result.setSampleLabel("Cosmos Sampler");
        result.sampleStart();
        try {

            // Cosmos Service constructor for entering config and query
            CosmosService cosmosService = new CosmosService( getCosmosQuery());

            // Kick off Query
            JsonNode response = cosmosService.ReadCosmos();

            // set response
            result.setSamplerData(response.toString());
            result.setResponseData(response.toString(), null);
            result.setDataType(SampleResult.TEXT);
            result.setResponseCodeOK();
            result.setResponseMessage("OK");
            result.getRequestHeaders();
            result.getResponseHeaders();
            isOK = true;

        } catch (Exception ex) {
            logger.debug("", ex);
            result.setResponseCode("500");
            result.setResponseMessage(ex.toString());
        }
        result.sampleEnd();
        result.setSuccessful(isOK);
        return result;
    }
    

When the ${variable} gets executed, the error response is due to the db not being able to read $. It's not converting the variable to a string when executing the query.

Do I need to specify in the code that it should take a variable somewhere or is my configuration on JMeter need modifications? Let me know if I can give anymore information that would help. Thank you.

ards
  • 3
  • 3

1 Answers1

0

First of all double check that your ${name/firstName} variable has anticipated value using Debug Sampler and View Results Tree listener combination as if it doesn't - there is no sense in further analysis of your code.

If it does - make sure you're following steps from How to write a plugin for JMeter chapter, i.e. your Sampler GUI class should extend AbstractSamplerGui, your Sampler should extend AbstractSampler, you call JMeterGUIComponent.modifyTestElement() function, etc.

Your 5 lines of code are not sufficient for identifying the potential implementation or configuration problems.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks for the response and sorry about the lack of code, I updated it. I ended up figuring out because of the pre-processor debugger so thank you for suggesting that. I wasn't putting quotes around the variable: `"${variableName"}`. Also, after restarting it, it started working without changing the configuration. Not very helpful but I'll take it for now. Thanks again for your time. – ards Jul 22 '21 at 16:13