You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by Cy...@rcomext.com on 2003/05/06 10:34:17 UTC
New function : CSVRead -- iteratively read a rows/columns of a CSV file.
Guys,
Any chance of including the attached in 1.9 ??
Cheers
Cyrus
----- Forwarded by Cyrus Montakab/External on 06/05/2003 09:33 -----
Cyrus Montakab
Swiss Re Life & Health (London)
UK-EC2Y 9AL London
07/04/2003 14:30 Dept.: IT
Office: MH2
Phone: 44 20 7814 3322
To: jmeter-dev@jakarta.apache.org
cc:
Subject: New function : CSVRead -- iteratively read a rows/columns of a CSV
file.
Hi,
We have had a requirement to load from CSV files. So we wrote 2 new classes
(jmeter functions) to allow this.
Full documentation of the functionality is within the code.
'StringFromFile.java' was used as a template for building these files.
Following all the contribution guidelines (which was'nt tool clear to me) -
I am forwarding this code to this mailing list - so hopefully someone will
help in putting it into the next release.
Cheers
Cyrus
==========================================================================
package org.apache.jmeter.functions;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.log.Hierarchy;
import org.apache.log.Logger;
/**
/**
* @author Cyrus M.
* Syntax is similar to StringFromFile function.
* The function represented by this class allows data to be read from CSV
files.
* The function allows the test to line-thru the data in the CSV file - one
line per each test.
* E.g. inserting the following in the test scripts :
* ${_CSVRead(c:/BOF/abcd.csv,0)} // read (first) line of
'c:/BOF/abcd.csv' , return the 1st column ( represented by the '0'),
* ${_CSVRead(c:/BOF/abcd.csv,1)} // read (first) line of
'c:/BOF/abcd.csv' , return the 1st column ( represented by the '1'),
* ${_CSVRead(c:/BOF/abcd.csv,next())} // Go to next line of
'c:/BOF/abcd.csv'
*
* NOTE: A single instance of file is opened and used for all threads.
* For example, if thread-1 reads the first line and then issues a 'next()'
, then thread-2 will be starting from line-1.
*
* Use CSVRead to isolate the file usage between different threads .
*
*/
/*
* It appears that JMeter instantiates a new copy of each function for
every reference in a Sampler
* or elsewhere.
*/
public class CSVRead extends AbstractFunction implements Serializable
{
transient protected static Logger log = Hierarchy.getDefaultHierarchy
().getLoggerFor(
"jmeter.CSVRead");
private static final String KEY = "__CSVRead"; // Function name (only
1 _)
protected static final List desc = new LinkedList();
protected static FileDataContainer fileData ;
protected String myValue = "<please supply a file>"; // Default value
protected String myName = "CSVRead_"; // Name to store value in
protected Object[] values;
protected BufferedReader myBread; // Buffered reader
protected boolean reopenFile=true; // Set from parameter list one day
...
protected static Hashtable threadData = null;
static {
desc.add(JMeterUtils.getResString("csvread_file_file_name"));
desc.add(JMeterUtils.getResString("column_number"));
}
public CSVRead()
{
myName = "ThreadStringFromFile_";
}
public Object clone()
{
CSVRead newReader = new CSVRead();
return newReader;
}
/**
* @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
*/
public synchronized String execute(SampleResult previousResult, Sampler
currentSampler) throws InvalidVariableException {
try {
JMeterVariables vars = getVariables();
ArrayList processedLines = null;
String fileName = null;
fileName =
((org.apache.jmeter.engine.util.CompoundVariable)values[0]).execute();
myName =
((org.apache.jmeter.engine.util.CompoundVariable)values[1]).execute();
// instantiates the fileDataContainer if one not already
present.
FileDataContainer fileData = getFileData(fileName);
// if argument is 'next' - go to the next line
if (myName.equals("next()") || myName.equals("next") ) {
fileData.incrementRowPosition();
storeCurrentLine(null);
}
// see if we already have read a line for this thread ...
processedLines = reloadCurrentLine();
// if no lines associated with this thread - then read, process
and store ...
if (fileData != null && processedLines == null) {
processedLines = (ArrayList) fileData.getNextLine();
this.storeCurrentLine( processedLines);
}
// get the current line number
int columnIndex = 0;
try {
columnIndex = Integer.parseInt(myName);
myValue = (String) processedLines.get(columnIndex);
} catch (Exception e) {
myValue = "";
}
log.debug (Thread.currentThread().getName() + ">>>> execute ("+
fileName + " , " + myName + ") " + this.hashCode());
} catch (Exception e) {
log.error ("execute" , e) ;
}
return myValue;
}
/**
* @see org.apache.jmeter.functions.Function#getArgumentDesc()
*/
public List getArgumentDesc() {
return desc;
}
/**
* get the FileDataContainer
*/
protected synchronized FileDataContainer getFileData (String fileName)
throws java.io.IOException {
if (fileData == null) {
fileData = load(fileName);
}
return fileData;
}
/**
* reset - resets the file - so the file is read in the next iteration
*/
protected String getId() {
return "" + Thread.currentThread().hashCode();
}
/**
* @see org.apache.jmeter.functions.Function#getReferenceKey()
*/
public String getReferenceKey() {
return KEY;
}
/**
* Insert the method's description here.
* Creation date: (24/03/2003 17:11:30)
* @return java.util.Hashtable
*/
protected static synchronized java.util.Hashtable getThreadData() {
if (threadData == null) {
threadData = new Hashtable();
}
return threadData;
}
/**
* @see org.apache.jmeter.functions.Function#execute(SampleResult,
Sampler)
*/
private synchronized FileDataContainer load(String fileName)
throws java.io.IOException {
FileDataContainer fileData = new FileDataContainer();
openFile(fileName);
if (null != myBread) {// Did we open the file?
try {
String line = myBread.readLine();
while (line != null) {
fileData.addLine( line );
line = myBread.readLine();
}
myBread.close();
setFileData(fileData);
return fileData;
} catch (java.io.IOException e) {
log.error("load(" + fileName + ")" ,e);
throw e;
}
}
return fileData;
}
private void openFile( String fileName ){
try {
FileReader fis = new FileReader(fileName);
myBread = new BufferedReader(fis);
} catch (Exception e) {
log.error("openFile",e);
}
}
/**
* this is for version 1.8.1 only -
* @deprecated
*/
protected ArrayList reloadCurrentLine() throws InvalidVariableException {
log.debug (getId() + "reloaded " + getThreadData().get(getId()));
return (ArrayList) getThreadData().get(getId());
}
/**
* reset - resets the file - so the file is read in the next iteration
*/
protected synchronized void reset() {
log.debug (getId() + "reseting .... " );
this.setFileData(null);
this.threadData = new Hashtable();
}
/**
* @see set the FileDataContainer
*/
protected synchronized void setFileData (FileDataContainer newValue) {
fileData = newValue;
}
/**
* @see org.apache.jmeter.functions.Function#setParameters(String)
*/
public void setParameters(Collection parameters)throws
InvalidVariableException {
log.debug (getId() + "setParameter - Collection" + parameters);
reset();
values = parameters.toArray();
if ( values.length > 2 )
throw new InvalidVariableException();
}
/**
* this is for version 1.8.1 only -
* @deprecated
*/
public void storeCurrentLine(ArrayList currentLine) throws
InvalidVariableException {
String id = getId();
log.debug (id + "storing " + currentLine);
if (currentLine == null) {
getThreadData().remove(id);
} else {
getThreadData().put( id , currentLine);
}
}
}
-------------------------------------------
package org.apache.jmeter.functions;
import java.util.*;
import org.apache.log.Hierarchy;
import org.apache.log.Logger;
/**
* Insert the type's description here.
* Creation date: (20/03/2003 09:37:44)
* @author: Cyrus Montakab
*/
public class FileDataContainer {
private ArrayList fileData;
// keeping track on which row was last read
private int rowPosition =-1;
transient private static Logger log = Hierarchy.getDefaultHierarchy
().getLoggerFor("jmeter.FileDataContainer");
/**
* FileDataContainer constructor comment.
*/
public FileDataContainer() {
super();
this.fileData = new ArrayList();
}
/**
* FileDataContainer constructor comment.
*/
public FileDataContainer(ArrayList newFileData) {
super();
fileData = newFileData;
}
/**
* Insert the method's description here.
* Creation date: (20/03/2003 09:39:50)
* @param newRowPosition int
*/
public void addLine(String newLine) {
fileData.add(processNextCSVLine(newLine));
}
/**
* Insert the method's description here.
* Creation date: (20/03/2003 09:39:50)
* @return java.util.ArrayList
*/
public java.util.ArrayList getNextLine() {
ArrayList result = null;
if (fileData != null && fileData.size() > 0) {
// ensure row position has been initialized
if (rowPosition == -1) {
rowPosition = 0;
}
result = (ArrayList) fileData.get(rowPosition);
}
return result;
}
/**
* Insert the method's description here.
* Creation date: (20/03/2003 09:39:50)
* @return int
*/
public int getRowPosition() {
return rowPosition;
}
/**
* Insert the method's description here.
* Creation date: (20/03/2003 09:39:50)
* @param newRowPosition int
*/
public void incrementRowPosition() {
if (this.fileData != null && fileData.size() > 0) {
rowPosition++;
if (rowPosition >= fileData.size()) {
rowPosition = 0;
}
}
// log.debug (Thread.currentThread().getName() + " >>>>
incrementRowPosition - returning : "+ rowPosition);
}
/**
*
* @param theLine
* @return
*/
protected ArrayList processNextCSVLine ( String theLine ) {
final String DELIMITER = ",";
ArrayList result = new ArrayList();
try {
int startIndex = 0;
int endIndex = theLine.indexOf( DELIMITER );
int i = 0;
while (endIndex > -1 ) {
result.add( theLine.substring(startIndex, endIndex).trim() );
startIndex = endIndex+1;
endIndex = theLine.indexOf( DELIMITER , startIndex);
i++;
}
result.add( theLine.substring(startIndex).trim() );
} catch (Exception e) {
log.error(" DataLoader.processNextCSVLine(" + theLine + ")" , e);
}
return result;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org
Re: New function : CSVRead -- iteratively read a rows/columns of a CSV file.
Posted by ms...@apache.org.
Yes, thanks for the reminder. I have it in my stack of things to do.
-Mike
On 6 May 2003 at 9:34, Cyrus_Montakab@rcomext.com wrote:
> Guys,
> Any chance of including the attached in 1.9 ??
>
> Cheers
> Cyrus
>
>
> ----- Forwarded by Cyrus Montakab/External on 06/05/2003 09:33 -----
>
> Cyrus Montakab
> Swiss Re Life & Health (London)
> UK-EC2Y 9AL London
>
> 07/04/2003 14:30 Dept.: IT
> Office: MH2
> Phone: 44 20 7814 3322
>
> To: jmeter-dev@jakarta.apache.org
> cc:
> Subject: New function : CSVRead --
iteratively read a rows/columns of a CSV
> file.
>
>
>
>
> Hi,
> We have had a requirement to load from CSV files. So we wrote 2 new
classes
> (jmeter functions) to allow this.
> Full documentation of the functionality is within the code.
> 'StringFromFile.java' was used as a template for building these files.
>
> Following all the contribution guidelines (which was'nt tool clear to me) -
> I am forwarding this code to this mailing list - so hopefully someone will
> help in putting it into the next release.
>
> Cheers
> Cyrus
>
========================================================
==================
>
>
>
> package org.apache.jmeter.functions;
>
> import java.util.*;
> import java.io.BufferedReader;
> import java.io.FileReader;
> import java.io.Serializable;
> import java.util.Collection;
> import java.util.LinkedList;
> import java.util.List;
> import java.util.ArrayList;
> import java.util.HashMap;
>
> import org.apache.jmeter.samplers.SampleResult;
> import org.apache.jmeter.samplers.Sampler;
> import org.apache.jmeter.threads.JMeterVariables;
> import org.apache.jmeter.util.JMeterUtils;
> import org.apache.log.Hierarchy;
> import org.apache.log.Logger;
>
>
> /**
> /**
> * @author Cyrus M.
> * Syntax is similar to StringFromFile function.
> * The function represented by this class allows data to be read from CSV
> files.
> * The function allows the test to line-thru the data in the CSV file - one
> line per each test.
> * E.g. inserting the following in the test scripts :
> * ${_CSVRead(c:/BOF/abcd.csv,0)} // read (first) line of
> 'c:/BOF/abcd.csv' , return the 1st column ( represented by the '0'),
> * ${_CSVRead(c:/BOF/abcd.csv,1)} // read (first) line of
> 'c:/BOF/abcd.csv' , return the 1st column ( represented by the '1'),
> * ${_CSVRead(c:/BOF/abcd.csv,next())} // Go to next line of
> 'c:/BOF/abcd.csv'
> *
> * NOTE: A single instance of file is opened and used for all threads.
> * For example, if thread-1 reads the first line and then issues a 'next()'
> , then thread-2 will be starting from line-1.
> *
> * Use CSVRead to isolate the file usage between different threads .
> *
> */
>
> /*
> * It appears that JMeter instantiates a new copy of each function for
> every reference in a Sampler
> * or elsewhere.
> */
>
> public class CSVRead extends AbstractFunction implements Serializable
> {
> transient protected static Logger log = Hierarchy.getDefaultHierarchy
> ().getLoggerFor(
> "jmeter.CSVRead");
>
> private static final String KEY = "__CSVRead"; // Function name (only
> 1 _)
>
> protected static final List desc = new LinkedList();
>
> protected static FileDataContainer fileData ;
>
> protected String myValue = "<please supply a file>"; // Default value
> protected String myName = "CSVRead_"; // Name to store value in
> protected Object[] values;
> protected BufferedReader myBread; // Buffered reader
> protected boolean reopenFile=true; // Set from parameter list one day
> ...
>
> protected static Hashtable threadData = null;
>
> static {
> desc.add(JMeterUtils.getResString("csvread_file_file_name"));
> desc.add(JMeterUtils.getResString("column_number"));
> }
>
>
> public CSVRead()
> {
> myName = "ThreadStringFromFile_";
> }
> public Object clone()
> {
> CSVRead newReader = new CSVRead();
> return newReader;
> }
> /**
> * @see org.apache.jmeter.functions.Function#execute(SampleResult,
Sampler)
> */
> public synchronized String execute(SampleResult previousResult, Sampler
> currentSampler) throws InvalidVariableException {
> try {
>
> JMeterVariables vars = getVariables();
> ArrayList processedLines = null;
> String fileName = null;
>
> fileName =
> ((org.apache.jmeter.engine.util.CompoundVariable)values[0]).execute();
> myName =
> ((org.apache.jmeter.engine.util.CompoundVariable)values[1]).execute();
>
> // instantiates the fileDataContainer if one not already
> present.
> FileDataContainer fileData = getFileData(fileName);
>
> // if argument is 'next' - go to the next line
> if (myName.equals("next()") || myName.equals("next") ) {
> fileData.incrementRowPosition();
> storeCurrentLine(null);
> }
>
> // see if we already have read a line for this thread ...
> processedLines = reloadCurrentLine();
>
> // if no lines associated with this thread - then read, process
> and store ...
> if (fileData != null && processedLines == null) {
> processedLines = (ArrayList) fileData.getNextLine();
> this.storeCurrentLine( processedLines);
> }
>
>
> // get the current line number
> int columnIndex = 0;
> try {
> columnIndex = Integer.parseInt(myName);
> myValue = (String) processedLines.get(columnIndex);
> } catch (Exception e) {
> myValue = "";
> }
>
> log.debug (Thread.currentThread().getName() + ">>>> execute ("+
> fileName + " , " + myName + ") " + this.hashCode());
>
> } catch (Exception e) {
> log.error ("execute" , e) ;
> }
> return myValue;
>
> }
> /**
> * @see org.apache.jmeter.functions.Function#getArgumentDesc()
> */
> public List getArgumentDesc() {
> return desc;
> }
> /**
> * get the FileDataContainer
> */
> protected synchronized FileDataContainer getFileData (String fileName)
> throws java.io.IOException {
> if (fileData == null) {
> fileData = load(fileName);
> }
>
> return fileData;
> }
> /**
> * reset - resets the file - so the file is read in the next iteration
> */
>
> protected String getId() {
> return "" + Thread.currentThread().hashCode();
> }
>
>
> /**
> * @see org.apache.jmeter.functions.Function#getReferenceKey()
> */
> public String getReferenceKey() {
> return KEY;
> }
> /**
> * Insert the method's description here.
> * Creation date: (24/03/2003 17:11:30)
> * @return java.util.Hashtable
> */
> protected static synchronized java.util.Hashtable getThreadData() {
> if (threadData == null) {
> threadData = new Hashtable();
> }
>
> return threadData;
> }
> /**
> * @see org.apache.jmeter.functions.Function#execute(SampleResult,
> Sampler)
> */
> private synchronized FileDataContainer load(String fileName)
> throws java.io.IOException {
> FileDataContainer fileData = new FileDataContainer();
> openFile(fileName);
>
> if (null != myBread) {// Did we open the file?
>
> try {
> String line = myBread.readLine();
> while (line != null) {
> fileData.addLine( line );
> line = myBread.readLine();
> }
> myBread.close();
> setFileData(fileData);
> return fileData;
> } catch (java.io.IOException e) {
> log.error("load(" + fileName + ")" ,e);
> throw e;
> }
> }
> return fileData;
> }
> private void openFile( String fileName ){
> try {
> FileReader fis = new FileReader(fileName);
> myBread = new BufferedReader(fis);
> } catch (Exception e) {
> log.error("openFile",e);
> }
> }
> /**
> * this is for version 1.8.1 only -
> * @deprecated
> */
>
> protected ArrayList reloadCurrentLine() throws InvalidVariableException {
> log.debug (getId() + "reloaded " + getThreadData().get(getId()));
>
> return (ArrayList) getThreadData().get(getId());
> }
>
>
> /**
> * reset - resets the file - so the file is read in the next iteration
> */
>
> protected synchronized void reset() {
> log.debug (getId() + "reseting .... " );
> this.setFileData(null);
> this.threadData = new Hashtable();
> }
>
>
> /**
> * @see set the FileDataContainer
> */
> protected synchronized void setFileData (FileDataContainer newValue) {
> fileData = newValue;
> }
> /**
> * @see org.apache.jmeter.functions.Function#setParameters(String)
> */
> public void setParameters(Collection parameters)throws
> InvalidVariableException {
> log.debug (getId() + "setParameter - Collection" + parameters);
>
> reset();
> values = parameters.toArray();
>
> if ( values.length > 2 )
> throw new InvalidVariableException();
>
> }
> /**
> * this is for version 1.8.1 only -
> * @deprecated
> */
>
> public void storeCurrentLine(ArrayList currentLine) throws
> InvalidVariableException {
> String id = getId();
> log.debug (id + "storing " + currentLine);
>
> if (currentLine == null) {
> getThreadData().remove(id);
> } else {
> getThreadData().put( id , currentLine);
> }
> }
>
>
> }
>
>
>
>
>
>
>
>
> -------------------------------------------
> package org.apache.jmeter.functions;
>
> import java.util.*;
> import org.apache.log.Hierarchy;
> import org.apache.log.Logger;
>
> /**
> * Insert the type's description here.
> * Creation date: (20/03/2003 09:37:44)
> * @author: Cyrus Montakab
> */
> public class FileDataContainer {
> private ArrayList fileData;
>
> // keeping track on which row was last read
> private int rowPosition =-1;
>
> transient private static Logger log = Hierarchy.getDefaultHierarchy
> ().getLoggerFor("jmeter.FileDataContainer");
>
> /**
> * FileDataContainer constructor comment.
> */
> public FileDataContainer() {
> super();
> this.fileData = new ArrayList();
> }
> /**
> * FileDataContainer constructor comment.
> */
> public FileDataContainer(ArrayList newFileData) {
> super();
> fileData = newFileData;
> }
> /**
> * Insert the method's description here.
> * Creation date: (20/03/2003 09:39:50)
> * @param newRowPosition int
> */
> public void addLine(String newLine) {
> fileData.add(processNextCSVLine(newLine));
> }
> /**
> * Insert the method's description here.
> * Creation date: (20/03/2003 09:39:50)
> * @return java.util.ArrayList
> */
> public java.util.ArrayList getNextLine() {
> ArrayList result = null;
> if (fileData != null && fileData.size() > 0) {
>
> // ensure row position has been initialized
> if (rowPosition == -1) {
> rowPosition = 0;
> }
>
> result = (ArrayList) fileData.get(rowPosition);
> }
> return result;
> }
> /**
> * Insert the method's description here.
> * Creation date: (20/03/2003 09:39:50)
> * @return int
> */
> public int getRowPosition() {
> return rowPosition;
> }
> /**
> * Insert the method's description here.
> * Creation date: (20/03/2003 09:39:50)
> * @param newRowPosition int
> */
> public void incrementRowPosition() {
> if (this.fileData != null && fileData.size() > 0) {
> rowPosition++;
> if (rowPosition >= fileData.size()) {
> rowPosition = 0;
> }
> }
> // log.debug (Thread.currentThread().getName() + " >>>>
> incrementRowPosition - returning : "+ rowPosition);
> }
> /**
> *
> * @param theLine
> * @return
> */
> protected ArrayList processNextCSVLine ( String theLine ) {
> final String DELIMITER = ",";
> ArrayList result = new ArrayList();
>
> try {
> int startIndex = 0;
> int endIndex = theLine.indexOf( DELIMITER );
> int i = 0;
>
> while (endIndex > -1 ) {
> result.add( theLine.substring(startIndex, endIndex).trim() );
> startIndex = endIndex+1;
> endIndex = theLine.indexOf( DELIMITER , startIndex);
> i++;
> }
>
> result.add( theLine.substring(startIndex).trim() );
>
> } catch (Exception e) {
> log.error(" DataLoader.processNextCSVLine(" + theLine + ")" , e);
> }
>
> return result;
> }
> }
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org
>
--
Michael Stover
mstover1@apache.org
Yahoo IM: mstover_ya
ICQ: 152975688
AIM: mstover777
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org