Home > OS >  JMeter reads the old version of csv
JMeter reads the old version of csv

Time:04-28

I am trying to test some APIs but first I need to get session keys for each customer I use in the test. I have a CSV file with customer login information and read it with each thread.

I have the following form in my JMeter file.

  • CSV Data Set Config - User Login Info
  • Set username, password - one for each iteration
  • Setup Thread Group
    • BeanShell Sampler to delete the sessionKeys.csv
File file = new File("C:/user/sessionKeys.csv");
if (file.exists() && file.isFile()) {
    file.delete();
}
  • Login Thread - ThreadCount = threadCount, Loop = 1
    • Login Request
      • BeanShell PostPrecessor to create file and append Session Keys to sessionKeys.csv
if("${sessionKey}" != "not_found")
{
    File file = new File("C:/user/sessionKeys.csv");

    FileWriter fWriter = new FileWriter(file, true);
    BufferedWriter buff = new BufferedWriter(fWriter);
    
    
    buff.write("${sessionKey}\n");
    
    buff.close();
    fWriter.close();
}
  • CSV Data Set Config - Session Keys
  • API Call Thread - ThreadCount = threadCount, Loop = loop
    • GetData Request

And I noticed that even if the file is actually deleted, created and filled with new sessionKeys, first a few requests uses the old sessionKeys in the file before it was deleted.

I have tried adding constant timer or changing the structure of the JMeter file but nothing worked.

CodePudding user response:

It looks like the CSV Config element in your test plan exists outside of the thread group and so will be called first before the file is deleted and recreated.

In your case it might be simpler to just store the session key as a JMeter property so that it can be accessed in all thread groups. You can store it using Groovy like props.put("${sessionKey}", sessionKey) or via JMeter functions like ${__setProperty("sessionKey",${sessionKey})}.

The property can then be accessed again using the property function like ${__P(sessionKey,)}.

CodePudding user response:

Take a look at JMeter Test Elements Execution Order

  1. Configuration elements

  2. Pre-Processors

  3. Timers

  4. Sampler

  5. Post-Processors (unless SampleResult is null)

  6. Assertions (unless SampleResult is null)

  7. Listeners (unless SampleResult is null)

CSV Data Set Config is a Configuration Element hence it's executed long before the Beanshell Sampler and this perfectly explains the behaviour you're facing.

So if you need to do some pre-processing of the CSV file you will need to do it in setUp Thread Group

Also be aware that starting from JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so it makes sense considering migrating.

  • Related