jmeter-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <seb...@gmail.com>
Subject Re: How to read a zipped text file and iterate over all the lines
Date Tue, 09 Jul 2013 10:13:49 GMT
On 9 July 2013 10:28, Janusz Kowalczyk <kowalczykjanusz@gmail.com> wrote:
> The thing is that I don't wan't to unzip those files, but just read
> directly from them.

In that case, I suggest you look at the function StringFromFile or the
CSV DataSet test element and copy what they do in your own code.

> OK, maybe easier way would be just returning a list or a map of variables,
> so that I could iterate over it?
>
> ps. I put this strange loop just for the sake of the demo, so it doesn't
> read more that 10 lines at a time :)
>
>
>
> On 8 July 2013 23:35, Deepak Shetty <shettyd@gmail.com> wrote:
>
>> this.vars.put(this.outputVariable, line); ==> Always overwrites the current
>> value
>> should be the last line however unless your loop is also incorrect
>>
>> But the better way is to get your data in the form you want first , then
>> start the test rather than the test writing some code to read the data i.e.
>> unzip your files whatever before you mai n test begins (as part of your
>> build or as a startup threadgroup )
>>
>>
>> On Mon, Jul 8, 2013 at 12:42 PM, Janusz Kowalczyk <
>> kowalczykjanusz@gmail.com
>> > wrote:
>>
>> > Hi All,
>> >
>> > I'd like to create a script that would read a zipped text file, and for
>> > example for each line (passed as variable) send a HTTP request.
>> >
>> > So far, I've written a script that works partially :)
>> > Partially, because sending a new line to log.info() prints out this new
>> > line (which is good :) ), but when I set the value of a variable to newly
>> > read line, then the value of that variable remains the same and is always
>> > equal to the first line of that text file.
>> >
>> > Please find the code below. To test it:
>> > - create a simple text file with just few lines of random text,
>> > - zip that text file
>> > - Add a JSR223 preprocessor & set the script language to Java
>> > - configure the script accordingly
>> > - add a HTTP sampler and add ${CONTEXT} to the "RAW Post Body" field
>> > - check the msg value of the http request in "View Results Tree"
>> >
>> >
>> > import java.io.*;
>> > import java.util.zip.ZipEntry;
>> > import java.util.zip.ZipFile;
>> > import org.apache.log.Logger;
>> > import org.apache.jmeter.threads.JMeterVariables;
>> >
>> > public class JmeterZipReader
>> > {
>> >     private String zipFile;
>> >     private Logger log;
>> >     private String outputVariable;
>> >     private JMeterVariables vars;
>> >
>> >     JmeterZipReader(String zipFile, Logger log, String outputVariable,
>> > JMeterVariables vars){
>> >         this.zipFile = zipFile;
>> >         this.log = log;
>> >         this.outputVariable = outputVariable;
>> >         this.vars = vars;
>> >     }
>> >
>> >     // textFile is the name of the text file inside the zip file
>> >     public void read(String textFile){
>> >         ZipFile zip;
>> >         ZipEntry ze;
>> >         InputStream input;
>> >         BufferedReader br;
>> >
>> >         try {
>> >             zip=new ZipFile(this.zipFile);
>> >             ze=zip.getEntry(textFile);
>> >             input = zip.getInputStream(ze);
>> >             br = new BufferedReader(new InputStreamReader(input,
>> > "US-ASCII"));
>> >
>> >             String line;
>> >             int cnt = 0;
>> >             while((line = br.readLine()) != null && cnt <= 10) {
>> >                 this.vars.put(this.outputVariable, line);
>> >                 log.info(line);
>> >                 cnt += 1;
>> >             }
>> >
>> >             br.close();
>> >             zip.close();
>> >         }
>> >         catch (Exception e) {
>> >             log.error("Unhandled exception:");
>> >             e.printStackTrace();
>> >         }
>> >     }
>> > }
>> >
>> > JmeterZipReader jzr = new JmeterZipReader("/path/to/a/test.zip", log,
>> > "CONTEXT", ctx.getVariables());
>> > jzr.read("test.csv");
>> >
>> >
>> >
>> > Many thanks,
>> > Janusz
>> >
>>

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


Mime
View raw message