From ca96394e0e4913f7b2407a6bcf7f843d92959310 Mon Sep 17 00:00:00 2001 From: frschwab Date: Mon, 13 May 2019 15:56:24 +0200 Subject: [PATCH] limit result set --- .../jdbc/AbstractJDBCTestElement.java | 100 +++++++++++++----- .../jdbc/JDBCTestElementBeanInfoSupport.java | 7 +- .../JDBCPostProcessorResources.properties | 2 + .../JDBCPreProcessorResources.properties | 2 + .../sampler/JDBCSamplerResources.properties | 2 + 5 files changed, 85 insertions(+), 28 deletions(-) diff --git a/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/AbstractJDBCTestElement.java b/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/AbstractJDBCTestElement.java index 86450c32576..75a499676cc 100644 --- a/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/AbstractJDBCTestElement.java +++ b/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/AbstractJDBCTestElement.java @@ -128,6 +128,7 @@ public abstract class AbstractJDBCTestElement extends AbstractTestElement implem private String resultSetHandler = RS_STORE_AS_STRING; private String resultVariable = ""; // $NON-NLS-1$ private String queryTimeout = ""; // $NON-NLS-1$ + private String resultSetMaxRows = ""; // $NON-NLS-1$ private static final int MAX_RETAIN_SIZE = JMeterUtils.getPropDefault("jdbcsampler.max_retain_result_size", 64 * 1024); @@ -545,33 +546,14 @@ private String getStringFromResultSet(ResultSet rs) throws SQLException, Unsuppo jmvars.putObject(currentResultVariable, results); } int j = 0; - while (rs.next()) { - Map row = null; - j++; - for (int i = 1; i <= numColumns; i++) { - Object o = rs.getObject(i); - if(results != null) { - if(row == null) { - row = new HashMap<>(numColumns); - results.add(row); - } - row.put(meta.getColumnLabel(i), o); - } - if (o instanceof byte[]) { - o = new String((byte[]) o, ENCODING); - } - sb.append(o); - if (i==numColumns){ - sb.append('\n'); - } else { - sb.append('\t'); - } - if (i <= varNames.length) { // i starts at 1 - String name = varNames[i - 1].trim(); - if (name.length()>0){ // Save the value in the variable if present - jmvars.put(name+UNDERSCORE+j, o == null ? null : o.toString()); - } - } + int k = getIntegerResultSetMaxRows(); + if (k < 0) { + while (rs.next()) { + j = processRow(rs, meta, sb, numColumns, jmvars, varNames, results, j); + } + } else { + while (j < k && rs.next()) { + j = processRow(rs, meta, sb, numColumns, jmvars, varNames, results, j); } } // Remove any additional values from previous sample @@ -594,6 +576,39 @@ private String getStringFromResultSet(ResultSet rs) throws SQLException, Unsuppo return sb.toString(); } + private int processRow(ResultSet rs, ResultSetMetaData meta, StringBuilder sb, int numColumns, + JMeterVariables jmvars, String[] varNames, List> results, int j) + throws SQLException, UnsupportedEncodingException { + Map row = null; + j++; + for (int i = 1; i <= numColumns; i++) { + Object o = rs.getObject(i); + if(results != null) { + if(row == null) { + row = new HashMap<>(numColumns); + results.add(row); + } + row.put(meta.getColumnLabel(i), o); + } + if (o instanceof byte[]) { + o = new String((byte[]) o, ENCODING); + } + sb.append(o); + if (i==numColumns){ + sb.append('\n'); + } else { + sb.append('\t'); + } + if (i <= varNames.length) { // i starts at 1 + String name = varNames[i - 1].trim(); + if (name.length()>0){ // Save the value in the variable if present + jmvars.put(name+UNDERSCORE+j, o == null ? null : o.toString()); + } + } + } + return j; + } + public static void close(Connection c) { try { if (c != null) { @@ -655,6 +670,37 @@ public void setQueryTimeout(String queryTimeout) { this.queryTimeout = queryTimeout; } + /** + * @return the integer representation resultSetMaxRows + */ + public int getIntegerResultSetMaxRows() { + int maxrows; + if(StringUtils.isEmpty(resultSetMaxRows)) { + return -1; + } else { + try { + maxrows = Integer.parseInt(resultSetMaxRows); + } catch (NumberFormatException nfe) { + maxrows = -1; + } + } + return maxrows; + } + + /** + * @return the resultSetMaxRows + */ + public String getResultSetMaxRows() { + return resultSetMaxRows ; + } + + /** + * @param resultSetMaxRows max number of rows to iterate through the ResultSet + */ + public void setResultSetMaxRows(String resultSetMaxRows) { + this.resultSetMaxRows = resultSetMaxRows; + } + public String getQuery() { return query; } diff --git a/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/JDBCTestElementBeanInfoSupport.java b/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/JDBCTestElementBeanInfoSupport.java index e53566431e8..944aeef444e 100644 --- a/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/JDBCTestElementBeanInfoSupport.java +++ b/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/JDBCTestElementBeanInfoSupport.java @@ -44,6 +44,7 @@ public JDBCTestElementBeanInfoSupport(Class beanClass) { "variableNames", // $NON-NLS-1$ "resultVariable", // $NON-NLS-1$ "queryTimeout", // $NON-NLS-1$ + "resultSetMaxRows", // $NON-NLS-1$ "resultSetHandler" // $NON-NLS-1$ }); @@ -80,7 +81,11 @@ public JDBCTestElementBeanInfoSupport(Class beanClass) { p = property("queryTimeout"); // $NON-NLS-1$ p.setValue(NOT_UNDEFINED, Boolean.TRUE); p.setValue(DEFAULT, ""); - + + p = property("resultSetMaxRows"); // $NON-NLS-1$ + p.setValue(NOT_UNDEFINED, Boolean.TRUE); + p.setValue(DEFAULT, ""); + p = property("queryType"); // $NON-NLS-1$ p.setValue(NOT_UNDEFINED, Boolean.TRUE); p.setValue(DEFAULT, AbstractJDBCTestElement.SELECT); diff --git a/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/processor/JDBCPostProcessorResources.properties b/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/processor/JDBCPostProcessorResources.properties index 8807c57a9f5..1d73d549965 100644 --- a/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/processor/JDBCPostProcessorResources.properties +++ b/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/processor/JDBCPostProcessorResources.properties @@ -34,3 +34,5 @@ resultVariable.displayName=Result variable name resultVariable.shortDescription=Name of the JMeter variable that stores the result set objects in a list of maps for looking up results by column name. queryTimeout.displayName=Query timeout queryTimeout.shortDescription=The timeout of statement measured in seconds +resultSetMaxRows.displayName=Limit ResultSet +resultSetMaxRows.shortDescription=Maximum number of rows to iterate through the ResultSet diff --git a/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/processor/JDBCPreProcessorResources.properties b/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/processor/JDBCPreProcessorResources.properties index ffe2a135c52..2a56ceee055 100644 --- a/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/processor/JDBCPreProcessorResources.properties +++ b/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/processor/JDBCPreProcessorResources.properties @@ -34,3 +34,5 @@ resultVariable.displayName=Result variable name resultVariable.shortDescription=Name of the JMeter variable that stores the result set objects in a list of maps for looking up results by column name. queryTimeout.displayName=Query timeout queryTimeout.shortDescription=The timeout of statement measured in seconds +resultSetMaxRows.displayName=Limit ResultSet +resultSetMaxRows.shortDescription=Maximum number of rows to iterate through the ResultSet diff --git a/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerResources.properties b/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerResources.properties index 9690536e9c6..db1b71dac4c 100644 --- a/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerResources.properties +++ b/src/protocol/jdbc/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerResources.properties @@ -34,3 +34,5 @@ resultVariable.displayName=Result variable name resultVariable.shortDescription=Name of the JMeter variable that stores the result set objects in a list of maps for looking up results by column name. queryTimeout.displayName=Query timeout (s) queryTimeout.shortDescription=The timeout of statement measured in seconds +resultSetMaxRows.displayName=Limit ResultSet +resultSetMaxRows.shortDescription=Maximum number of rows to iterate through the ResultSet