jmeter-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apurv30 <apur...@gmail.com>
Subject Re: Setting CSV file name as a variable
Date Tue, 15 Mar 2011 06:22:45 GMT
Hello Dan,

I have written a program that can read from a text file with values written
as..
no_of_threads=45

The code parses the line, splits the strings and stores the value in a
variable  and parses it into an interger..

It also has the regex pattern required to do a search replacement. I did it
for an xml file including quotes.
If you want .csv file you can check source forge for code that reads from
file, and to replace u can use below code.

The sample code is copy pasted below..

import java.util.*;
import java.io.*;
import java.util.regex.*;
import java.io.IOException;

****** to read code from file *******
String target = "";
String path = "values.txt";
String[] s = null;

try {
            File file = new File(path);
            BufferedReader br = new BufferedReader(
                                new InputStreamReader(
                                new FileInputStream(file)));                 

            br.mark(200); // specify a large enough = total no of characters
in file
           
            target = "no_of_users";
            while((line = br.readLine()) != null){           
                    if(line.indexOf(target) != -1){
                    br.reset();
                    break;                   
                    }
                }
                s = line.split("\\=");           
                no_of_users = s[1];   

} catch(IOException e){
                    System.out.println("Error: Unable to Load Values from
file check Values.txt " + e.getMessage());
                }


******* for text replacement in a file  including pattern matching
***********

try{         
            File file = new File(test_file);           
            // Read the entire contents of jmeter file
            BufferedReader reader = new BufferedReader( new FileReader
(file));
            line  = null;
            StringBuilder stringBuilder = new StringBuilder();
            String ls = System.getProperty("line.separator");
            while( ( line = reader.readLine() ) != null ) {
                stringBuilder.append( line );
                stringBuilder.append( ls );
            }
            reader.close();

// Replace Number of Threads
            Pattern pattern =
Pattern.compile("ThreadGroup.num_threads\\\">[0-9]*");         
            Matcher matcher = pattern.matcher(stringBuilder);
            String output =
matcher.replaceAll("ThreadGroup.num_threads\\\">" + no_of_users + "");

/* 
the code replace the value in the string 
"ThreadGroup.num_threads">any_integer_value
with the value specified in the variable - no_of_users
for random text u can specify [a-z]*[A-Z]*[0-9]*
*/


BufferedWriter out = new BufferedWriter(new FileWriter(test_file));
            out.write(output);
            out.close();           
        }
        catch(Exception e)
        {
            System.out.println("Error : " + e.getMessage());
            e.printStackTrace();           
        }


Cheers,
Apoorv Kodera

--
View this message in context: http://jmeter.512774.n5.nabble.com/Setting-CSV-file-name-as-a-variable-tp3556572p3673800.html
Sent from the JMeter - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org


Mime
View raw message