apache / jmeter

Apache JMeter open-source load testing tool for analyzing and measuring the performance of a variety of services
https://jmeter.apache.org/
Apache License 2.0
7.98k stars 2.03k forks source link

Fixes to StringFromFile #1104

Closed asfimport closed 20 years ago

asfimport commented 21 years ago

tjsb (Bug 20152): The patch file (to follow) fixes the StringFromFile function:

Patch follows.

Severity: minor OS: other

asfimport commented 21 years ago

tjsb (migrated from Bugzilla): Created attachment SFF.patch: Various patches to StringFromFile

SFF.patch ````diff Index: StringFromFile.java =================================================================== RCS file: /home/cvspublic/jakarta-jmeter/src/functions/org/apache/jmeter/functions/StringFromFile.java,v retrieving revision 1.5 diff -u -r1.5 StringFromFile.java --- StringFromFile.java 3 May 2003 15:34:33 -0000 1.5 +++ StringFromFile.java 22 May 2003 12:52:41 -0000 @@ -1,3 +1,58 @@ +/* + * ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001,2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache JMeter" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache JMeter", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + package org.apache.jmeter.functions; import java.io.BufferedReader; @@ -16,11 +71,34 @@ import org.apache.log.Logger; -/* - * It appears that JMeter instantiates a new copy of each function for every reference in a Sampler - * or elsewhere. - */ +/** + * StringFromFile (Function) + * + * @author default + * + * @version $Id$ + * + * Function to read a String from a text file + * + * Parameters: + * - file name + * - variable name (optional - defaults to StringFromFile_) + * + * Returns: + * - the next line from the file - or **ERR** if an error occurs + * - value is also saved in the variable for later re-use. + * + * Ensure that different variable names are used for each call to the function + * + * + * Notes: + * - JMeter instantiates a copy of each function for every reference in a Sampler + * or elsewhere; each instance will open its own copy of the the file + * - the file name is resolved at file (re-)open time + * - the output variable name is resolved every time the function is invoked + * + */ public class StringFromFile extends AbstractFunction implements Serializable { transient private static Logger log = Hierarchy.getDefaultHierarchy().getLoggerFor( @@ -40,6 +118,7 @@ private Object[] values; private BufferedReader myBread; // Buffered reader private boolean reopenFile=true; // Set from parameter list one day ... + private String fileName; // needed for error messages public StringFromFile() { @@ -48,15 +127,21 @@ public Object clone() { StringFromFile newReader = new StringFromFile(); + if (log.isDebugEnabled()){ // Skip expensive paramter creation .. + log.debug(this+"::StringFromFile.clone()",new Throwable("debug")); + } + return newReader; } - private void openFile( String fileName ){ + private void openFile(){ + fileName = ((CompoundVariable)values[0]).execute(); try { FileReader fis = new FileReader(fileName); myBread = new BufferedReader(fis); + log.info("Opened "+fileName); } catch (Exception e) { - log.error("openFile",e); + log.error("Error in openFile "+fileName,e); } } @@ -68,44 +153,55 @@ JMeterVariables vars = getVariables(); - String fileName = ((CompoundVariable)values[0]).execute(); - myName = ((CompoundVariable)values[1]).execute(); - - if(myBread == null) - { - openFile(fileName); - } - + if (values.length >= 2) { + myName = ((CompoundVariable)values[1]).execute(); + } + myValue="**ERR**"; if (null != myBread) {// Did we open the file? try { String line = myBread.readLine(); if (line == null && reopenFile) { // EOF, re-open file + log.info("Reached EOF on "+fileName); myBread.close(); - openFile(fileName); + openFile(); line = myBread.readLine(); } myValue = line; } catch (Exception e) { - log.error("Token",e); + log.error("Error reading file "+fileName,e); } } vars.put(myName,myValue); + + log.debug(this+"::StringFromFile.execute() value " + myValue); + return myValue; } /* (non-Javadoc) + * Parameters: + * - file name + * - variable name (optional) + * * @see org.apache.jmeter.functions.Function#setParameters(Collection) */ public void setParameters(Collection parameters) throws InvalidVariableException { + log.debug(this+"::StringFromFile.setParameters()"); + values = parameters.toArray(); - if ( values.length > 2 ) + if (( values.length > 2 ) || (values.length < 1)) { throw new InvalidVariableException(); + } + + openFile(); + + log.info("Variable name: "+ myName); } ````
asfimport commented 21 years ago

tjsb (migrated from Bugzilla): Created attachment crlast.patch: Updates to component_reference docs

crlast.patch ````diff Index: component_reference.xml =================================================================== RCS file: /home/cvspublic/jakarta-jmeter/xdocs/usermanual/component_reference.xml,v retrieving revision 1.34 diff -u -r1.34 component_reference.xml --- component_reference.xml 13 Feb 2003 21:50:39 -0000 1.34 +++ component_reference.xml 22 May 2003 15:48:50 -0000 @@ -2,7 +2,7 @@ - User's Manual: Samplers + User's Manual: Component Reference @@ -1180,8 +1180,8 @@ - -

The sumInt function can be used to compute the sum of two or more integer values. + +

The intsum function can be used to compute the sum of two or more integer values.

@@ -1193,6 +1193,35 @@
+ + +

+ The StringFromFile function can be used to read strings from a text file. + This is useful for running tests that require lots of variable data. + For example when testing a banking application, 100s or 1000s of different account numbers might be required. +

+

+ Each time it is called it reads the next line from the file. + When the end of the file is reached, it will start reading again from the beginning. + If there are multiple references to the function in a test script, each will open the file independently, + even if the file names are the same. + [If the value is to be used again elsewhere, use different variable names for each function call.] +

+

If an error occurs opening or reading the file, then the function returns the string "**ERR**"

+
+ + + Path to the file name. + (The path can be relative to the JMeter launch directory) + + + A reference name - refName - for reusing the value created by this function. + Stored values are of the form ${refName}. + + +

The file name parameter is resolved when the file is opened or re-opened.

+

The reference name parameter (if supplied) is resolved every time the function is executed.

+
````
asfimport commented 20 years ago

Mike Stover (migrated from Bugzilla): patch applied - thank you!