Search in sources :

Example 1 with StatisticalSampleResult

use of org.apache.jmeter.samplers.StatisticalSampleResult in project jmeter by apache.

the class CSVSaveService method makeResultFromDelimitedString.

/**
     * Make a SampleResult given a set of tokens
     * 
     * @param parts
     *            tokens parsed from the input
     * @param saveConfig
     *            the save configuration (may be updated)
     * @param lineNumber the line number (for error reporting)
     * @return the sample result
     * 
     * @throws JMeterError
     */
private static SampleEvent makeResultFromDelimitedString(final String[] parts, // may be updated
final SampleSaveConfiguration saveConfig, final long lineNumber) {
    SampleResult result = null;
    // $NON-NLS-1$
    String hostname = "";
    long timeStamp = 0;
    long elapsed = 0;
    String text = null;
    // Save the name for error reporting
    String field = null;
    int i = 0;
    try {
        if (saveConfig.saveTimestamp()) {
            field = TIME_STAMP;
            text = parts[i++];
            if (saveConfig.printMilliseconds()) {
                try {
                    // see if this works
                    timeStamp = Long.parseLong(text);
                } catch (NumberFormatException e) {
                    // it did not, let's try some other formats
                    log.warn("Cannot parse timestamp: '{}', will try following formats {}", text, Arrays.asList(DATE_FORMAT_STRINGS));
                    boolean foundMatch = false;
                    for (String fmt : DATE_FORMAT_STRINGS) {
                        SimpleDateFormat dateFormat = new SimpleDateFormat(fmt);
                        dateFormat.setLenient(false);
                        try {
                            Date stamp = dateFormat.parse(text);
                            timeStamp = stamp.getTime();
                            log.warn("Setting date format to: {}", fmt);
                            saveConfig.setDateFormat(fmt);
                            foundMatch = true;
                            break;
                        } catch (ParseException pe) {
                            log.info("{} did not match {}, trying next date format", text, fmt);
                        }
                    }
                    if (!foundMatch) {
                        throw new ParseException("No date-time format found matching " + text, -1);
                    }
                }
            } else if (saveConfig.strictDateFormatter() != null) {
                Date stamp = saveConfig.strictDateFormatter().parse(text);
                timeStamp = stamp.getTime();
            } else {
                // can this happen?
                final String msg = "Unknown timestamp format";
                log.warn(msg);
                throw new JMeterError(msg);
            }
        }
        if (saveConfig.saveTime()) {
            field = CSV_ELAPSED;
            text = parts[i++];
            elapsed = Long.parseLong(text);
        }
        if (saveConfig.saveSampleCount()) {
            result = new StatisticalSampleResult(timeStamp, elapsed);
        } else {
            result = new SampleResult(timeStamp, elapsed);
        }
        if (saveConfig.saveLabel()) {
            field = LABEL;
            text = parts[i++];
            result.setSampleLabel(text);
        }
        if (saveConfig.saveCode()) {
            field = RESPONSE_CODE;
            text = parts[i++];
            result.setResponseCode(text);
        }
        if (saveConfig.saveMessage()) {
            field = RESPONSE_MESSAGE;
            text = parts[i++];
            result.setResponseMessage(text);
        }
        if (saveConfig.saveThreadName()) {
            field = THREAD_NAME;
            text = parts[i++];
            result.setThreadName(text);
        }
        if (saveConfig.saveDataType()) {
            field = DATA_TYPE;
            text = parts[i++];
            result.setDataType(text);
        }
        if (saveConfig.saveSuccess()) {
            field = SUCCESSFUL;
            text = parts[i++];
            result.setSuccessful(Boolean.valueOf(text).booleanValue());
        }
        if (saveConfig.saveAssertionResultsFailureMessage()) {
            i++;
        // TODO - should this be restored?
        }
        if (saveConfig.saveBytes()) {
            field = CSV_BYTES;
            text = parts[i++];
            result.setBytes(Long.parseLong(text));
        }
        if (saveConfig.saveSentBytes()) {
            field = CSV_SENT_BYTES;
            text = parts[i++];
            result.setSentBytes(Long.parseLong(text));
        }
        if (saveConfig.saveThreadCounts()) {
            field = CSV_THREAD_COUNT1;
            text = parts[i++];
            result.setGroupThreads(Integer.parseInt(text));
            field = CSV_THREAD_COUNT2;
            text = parts[i++];
            result.setAllThreads(Integer.parseInt(text));
        }
        if (saveConfig.saveUrl()) {
            i++;
        // TODO: should this be restored?
        }
        if (saveConfig.saveFileName()) {
            field = CSV_FILENAME;
            text = parts[i++];
            result.setResultFileName(text);
        }
        if (saveConfig.saveLatency()) {
            field = CSV_LATENCY;
            text = parts[i++];
            result.setLatency(Long.parseLong(text));
        }
        if (saveConfig.saveEncoding()) {
            field = CSV_ENCODING;
            text = parts[i++];
            result.setEncodingAndType(text);
        }
        if (saveConfig.saveSampleCount()) {
            field = CSV_SAMPLE_COUNT;
            text = parts[i++];
            result.setSampleCount(Integer.parseInt(text));
            field = CSV_ERROR_COUNT;
            text = parts[i++];
            result.setErrorCount(Integer.parseInt(text));
        }
        if (saveConfig.saveHostname()) {
            field = CSV_HOSTNAME;
            hostname = parts[i++];
        }
        if (saveConfig.saveIdleTime()) {
            field = CSV_IDLETIME;
            text = parts[i++];
            result.setIdleTime(Long.parseLong(text));
        }
        if (saveConfig.saveConnectTime()) {
            field = CSV_CONNECT_TIME;
            text = parts[i++];
            result.setConnectTime(Long.parseLong(text));
        }
        if (i + saveConfig.getVarCount() < parts.length) {
            log.warn("Line: {}. Found {} fields, expected {}. Extra fields have been ignored.", lineNumber, parts.length, i);
        }
    } catch (NumberFormatException | ParseException e) {
        if (log.isWarnEnabled()) {
            log.warn("Error parsing field '{}' at line {}. {}", field, lineNumber, e.toString());
        }
        throw new JMeterError(e);
    } catch (ArrayIndexOutOfBoundsException e) {
        log.warn("Insufficient columns to parse field '{}' at line {}", field, lineNumber);
        throw new JMeterError(e);
    }
    return new SampleEvent(result, "", hostname);
}
Also used : Date(java.util.Date) SampleEvent(org.apache.jmeter.samplers.SampleEvent) JMeterError(org.apache.jorphan.util.JMeterError) StatisticalSampleResult(org.apache.jmeter.samplers.StatisticalSampleResult) StatisticalSampleResult(org.apache.jmeter.samplers.StatisticalSampleResult) SampleResult(org.apache.jmeter.samplers.SampleResult) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 SampleEvent (org.apache.jmeter.samplers.SampleEvent)1 SampleResult (org.apache.jmeter.samplers.SampleResult)1 StatisticalSampleResult (org.apache.jmeter.samplers.StatisticalSampleResult)1 JMeterError (org.apache.jorphan.util.JMeterError)1