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 se...@apache.org on 2008/11/04 02:27:51 UTC

svn commit: r711154 - in /jakarta/jmeter/trunk: docs/images/screenshots/jdbctest/ src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/ xdocs/ xdocs/images/screenshots/jdbctest/ xdocs/usermanual/

Author: sebb
Date: Mon Nov  3 17:27:51 2008
New Revision: 711154

URL: http://svn.apache.org/viewvc?rev=711154&view=rev
Log:
JDBC Request can optionally save the results of Select statements to variables.

Modified:
    jakarta/jmeter/trunk/docs/images/screenshots/jdbctest/jdbc-request.png
    jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSampler.java
    jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerBeanInfo.java
    jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerResources.properties
    jakarta/jmeter/trunk/xdocs/changes.xml
    jakarta/jmeter/trunk/xdocs/images/screenshots/jdbctest/jdbc-request.png
    jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml

Modified: jakarta/jmeter/trunk/docs/images/screenshots/jdbctest/jdbc-request.png
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/docs/images/screenshots/jdbctest/jdbc-request.png?rev=711154&r1=711153&r2=711154&view=diff
==============================================================================
Binary files - no diff available.

Modified: jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSampler.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSampler.java?rev=711154&r1=711153&r2=711154&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSampler.java (original)
+++ jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSampler.java Mon Nov  3 17:27:51 2008
@@ -39,6 +39,7 @@
 import org.apache.jmeter.samplers.Entry;
 import org.apache.jmeter.samplers.SampleResult;
 import org.apache.jmeter.testbeans.TestBean;
+import org.apache.jmeter.threads.JMeterVariables;
 import org.apache.jmeter.util.JMeterUtils;
 import org.apache.jorphan.collections.Data;
 import org.apache.jorphan.logging.LoggingManager;
@@ -53,6 +54,10 @@
 
     private static final Logger log = LoggingManager.getLoggerForClass();
 
+    private static final String COMMA = ","; // $NON-NLS-1$
+
+    private static final String UNDERSCORE = "_"; // $NON-NLS-1$
+
     // This value is used for both the connection (perConnCache) and statement (preparedStatementMap) caches.
     // TODO - do they have to be the same size?
     private static final int MAX_ENTRIES =
@@ -69,6 +74,7 @@
     // TODO - should the encoding be configurable?
     private static final String ENCODING = "UTF-8"; // $NON-NLS-1$
 
+    // key: name (lowercase) from java.sql.Types; entry: corresponding int value
     private static final Map mapJdbcNameToInt;
 
     static {
@@ -108,6 +114,7 @@
     private String queryType = SELECT;
     private String queryArguments = ""; // $NON-NLS-1$
     private String queryArgumentsTypes = ""; // $NON-NLS-1$
+    private String variableNames = ""; // $NON-NLS-1$
 
     /**
      *  Cache of PreparedStatements stored in a per-connection basis. Each entry of this
@@ -195,7 +202,7 @@
             } else if (PREPARED_SELECT.equals(_queryType)) {
                 pstmt = getPreparedStatement(conn);
                 setArguments(pstmt);
-                pstmt.executeQuery();
+                pstmt.executeQuery(); // FindBugs: the statement is closed in resultSetsToString()
                 String sb = resultSetsToString(pstmt,true,null);
                 res.setResponseData(sb.getBytes(ENCODING));
             } else if (PREPARED_UPDATE.equals(_queryType)) {
@@ -257,6 +264,7 @@
                     rs = pstmt.getResultSet();
                     Data data = getDataFromResultSet(rs);
                     sb.append(data.toString()).append("\n"); // $NON-NLS-1$
+                    data.getDataAsText();
                 } finally {
                     close(rs);
                 }
@@ -289,8 +297,8 @@
         if (getQueryArguments().trim().length()==0) {
             return new int[]{};
         }
-        String[] arguments = getQueryArguments().split(","); // $NON-NLS-1$
-        String[] argumentsTypes = getQueryArgumentsTypes().split(","); // $NON-NLS-1$
+        String[] arguments = getQueryArguments().split(COMMA);
+        String[] argumentsTypes = getQueryArgumentsTypes().split(COMMA);
         if (arguments.length != argumentsTypes.length) {
             throw new SQLException("number of arguments ("+arguments.length+") and number of types ("+argumentsTypes.length+") are not equal");
         }
@@ -405,16 +413,46 @@
             data.addHeader(dbCols[i]);
         }
 
+        JMeterVariables jmvars = null;
+        String varnames[] = getVariableNames().split(COMMA);
+        if (varnames.length > 0){
+            jmvars = getThreadContext().getVariables();
+        }
+        int j = 0;
         while (rs.next()) {
+            j++;
             data.next();
             for (int i = 0; i < numColumns; i++) {
                 Object o = rs.getObject(i + 1);
                 if (o instanceof byte[]) {
-                    o = new String((byte[]) o);
+                    o = new String((byte[]) o); // TODO what charset applies here?
                 }
                 data.addColumnValue(dbCols[i], o);
+                if (jmvars != null && i < varnames.length) {
+                    String name = varnames[i].trim();
+                    if (name.length()>0){ // Save the value in the variable if present
+                        jmvars.put(name+UNDERSCORE+j, o == null ? null : o.toString());
+                    }
+                }
+            }
+        }
+        // Remove any additional values from previous sample
+        for(int i=0; i < varnames.length; i++){
+            String name = varnames[i].trim();
+            if (name.length()>0 && jmvars != null){
+                final String varCount = name+"_#"; // $NON-NLS-1$
+                // Get the previous count
+                String prevCount = jmvars.get(varCount);
+                if (prevCount != null){
+                    int prev = Integer.parseInt(prevCount);
+                    for (int n=j+1; n <= prev; n++ ){
+                        jmvars.remove(name+UNDERSCORE+n);
+                    }
+                }
+                jmvars.put(varCount, Integer.toString(j)); // save the current count
             }
         }
+        
         return data;
     }
 
@@ -517,4 +555,18 @@
     public void setQueryArgumentsTypes(String queryArgumentsType) {
         this.queryArgumentsTypes = queryArgumentsType;
     }
+
+    /**
+     * @return the variableNames
+     */
+    public String getVariableNames() {
+        return variableNames;
+    }
+
+    /**
+     * @param variableNames the variableNames to set
+     */
+    public void setVariableNames(String variableNames) {
+        this.variableNames = variableNames;
+    }
 }

Modified: jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerBeanInfo.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerBeanInfo.java?rev=711154&r1=711153&r2=711154&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerBeanInfo.java (original)
+++ jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerBeanInfo.java Mon Nov  3 17:27:51 2008
@@ -43,7 +43,8 @@
                 "queryType", // $NON-NLS-1$
                 "query", // $NON-NLS-1$
                 "queryArguments", // $NON-NLS-1$
-                "queryArgumentsTypes" // $NON-NLS-1$
+                "queryArgumentsTypes", // $NON-NLS-1$
+                "variableNames", // $NON-NLS-1$
                 });
 
         PropertyDescriptor p = property("dataSource"); // $NON-NLS-1$
@@ -58,6 +59,9 @@
         p.setValue(NOT_UNDEFINED, Boolean.TRUE);
         p.setValue(DEFAULT, "");
 
+        p = property("variableNames"); // $NON-NLS-1$
+        p.setValue(NOT_UNDEFINED, Boolean.TRUE);
+        p.setValue(DEFAULT, "");
 
         p = property("queryType"); // $NON-NLS-1$
         p.setValue(NOT_UNDEFINED, Boolean.TRUE);

Modified: jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerResources.properties
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerResources.properties?rev=711154&r1=711153&r2=711154&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerResources.properties (original)
+++ jakarta/jmeter/trunk/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerResources.properties Mon Nov  3 17:27:51 2008
@@ -23,7 +23,9 @@
 dataSource.displayName=Variable Name
 dataSource.shortDescription=Name of the JMeter variable that the connection pool is bound to.
 queryArguments.displayName=Parameter values
-queryArguments.shortDescription=SQL parameter values
+queryArguments.shortDescription=SQL parameter values (comma separated)
 queryArgumentsTypes.displayName=Parameter types
-queryArgumentsTypes.shortDescription=JDBC Type names from java.sql.Types. VARCHAR, INTEGER, etc. 
+queryArgumentsTypes.shortDescription=JDBC Type names from java.sql.Types. VARCHAR, INTEGER, etc. (comma separated)
+variableNames.displayName=Variable names
+variableNames.shortDescription=Output variable names for each column  (comma separated)
 

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=711154&r1=711153&r2=711154&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Mon Nov  3 17:27:51 2008
@@ -169,6 +169,7 @@
 <li>Add classname field to TCP Sampler GUIs</li>
 <li>Apache SOAP 2.3.1 does not give access to HTTP response code/message, so WebService sampler now treats an empty response as an error</li>
 <li>Use Script to evaluate __jexl() function so can have multiple statements.</li>
+<li>JDBC Request can optionally save the results of Select statements to variables.</li>
 </ul>
 
 <h3>Non-functional changes</h3>

Modified: jakarta/jmeter/trunk/xdocs/images/screenshots/jdbctest/jdbc-request.png
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/images/screenshots/jdbctest/jdbc-request.png?rev=711154&r1=711153&r2=711154&view=diff
==============================================================================
Binary files - no diff available.

Modified: jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml?rev=711154&r1=711153&r2=711154&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml (original)
+++ jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml Mon Nov  3 17:27:51 2008
@@ -309,12 +309,32 @@
 
 </component>
 
-<component name="JDBC Request" index="&sect-num;.1.3"  width="449" height="335" screenshot="jdbctest/jdbc-request.png">
+<component name="JDBC Request" index="&sect-num;.1.3"  width="427" height="334" screenshot="jdbctest/jdbc-request.png">
 
 <description><p>This sampler lets you send an JDBC Request (an SQL query) to a database.</p>
 <p>Before using this you need to set up a
 <complink name="JDBC Connection Configuration"/> Configuration element
-</p></description>
+</p>
+<p>
+If the Variable Names list is provided, then for each row returned by a Select statement, the variables are set up
+with the value of the corresponding column (if a variable name is provided), and the count of rows is also set up.
+For example, if the Select statement returns 2 rows of 3 columns, and the variable list is <code>A,,C</code>,
+then the following variables will be set up:
+<pre>
+A_#=2 (number of rows)
+A_1=column 1, row 1
+A_2=column 1, row 2
+C_#=2 (number of rows)
+C_1=column 3, row 1
+C_2=column 3, row 2
+</pre>
+If the Select statement returns zero rows, then the A_# and C_# variables would be set to 0, and no other variables would be set.
+</p>
+<p>
+Old variables are cleared if necessary - e.g. if the first select retrieves 6 rows and a second select returns only 3 rows,
+the additional variables for rows 4, 5 and 6 will be removed.
+</p>
+</description>
 
 <properties>
         <property name="Name" required="No">Descriptive name for this controller that is shown in the tree.</property>
@@ -368,6 +388,7 @@
         <br></br>
         There must be as many values as there are placeholders in the statement.
         </property>
+        <property name="Variable Names" required="No">Comma-separated list of variable names to hold values returned by Select statements</property>
 </properties>
 
 <links>



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