Skip to content

Commit

Permalink
limit result set
Browse files Browse the repository at this point in the history
  • Loading branch information
frschwab committed May 14, 2019
1 parent 4d612f3 commit ca96394
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -545,33 +546,14 @@ private String getStringFromResultSet(ResultSet rs) throws SQLException, Unsuppo
jmvars.putObject(currentResultVariable, results);
}
int j = 0;
while (rs.next()) {
Map<String, Object> 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
Expand All @@ -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<Map<String, Object>> results, int j)
throws SQLException, UnsupportedEncodingException {
Map<String, Object> 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) {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public JDBCTestElementBeanInfoSupport(Class<? extends TestBean> beanClass) {
"variableNames", // $NON-NLS-1$
"resultVariable", // $NON-NLS-1$
"queryTimeout", // $NON-NLS-1$
"resultSetMaxRows", // $NON-NLS-1$
"resultSetHandler" // $NON-NLS-1$
});

Expand Down Expand Up @@ -80,7 +81,11 @@ public JDBCTestElementBeanInfoSupport(Class<? extends TestBean> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ca96394

Please sign in to comment.