Java Code Examples for org.apache.jmeter.samplers.SampleResult#setSampleCount()

The following examples show how to use org.apache.jmeter.samplers.SampleResult#setSampleCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SubSampler.java    From mqtt-jmeter with Apache License 2.0 5 votes vote down vote up
private SampleResult produceResult(SampleResult result, String topicName) {
	SubBean bean = batches.poll();
	if(bean == null) { // In "elapsed time" mode, return "dummy" when time is reached
		bean = new SubBean();
	}
	int receivedCount = bean.getReceivedCount();
	List<String> contents = bean.getContents();
	String message = MessageFormat.format("Received {0} of message.", receivedCount);
	StringBuffer content = new StringBuffer("");
	if (isDebugResponse()) {
		for (int i = 0; i < contents.size(); i++) {
			content.append(contents.get(i) + "\n");
		}
	}
	result = fillOKResult(result, bean.getReceivedMessageSize(), message, content.toString());
	if (logger.isLoggable(Level.FINE)) {
		logger.fine("sub [topic]: " + topicName + ", [payload]: " + content.toString());
	}
	
	if(receivedCount == 0) {
		result.setEndTime(result.getStartTime()); // dummy result, rectify sample time
	} else {
		if (isAddTimestamp()) {
			result.setEndTime(result.getStartTime() + (long) bean.getAvgElapsedTime()); // rectify sample time
			result.setLatency((long) bean.getAvgElapsedTime());
		} else {
			result.setEndTime(result.getStartTime()); // received messages w/o timestamp, then we cannot reliably calculate elapsed time
		}
	}
	result.setSampleCount(receivedCount);

	return result;
}
 
Example 2
Source File: HonoReceiver.java    From hono with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Takes a sample.
 *
 * @param result The result to set the sampler's current statistics on.
 */
public void sample(final SampleResult result) {
    synchronized (lock) {
        long elapsed = 0;
        result.setResponseCodeOK();
        result.setSuccessful(errorCount == 0 && !senderClockNotInSync);
        result.setSampleCount(messageCount);
        result.setErrorCount(errorCount); // NOTE: This method does nothing in JMeter 3.3/4.0
        result.setBytes(bytesReceived);
        if (messageCount > 0) {
            if (sampler.isUseSenderTime() && sampleStart != 0 && errorCount == 0) {
                // errorCount has to be 0 here - otherwise totalSampleDeliveryTime doesn't have a correct value
                elapsed = senderClockNotInSync ? -1 : totalSampleDeliveryTime / messageCount;
                result.setStampAndTime(sampleStart, elapsed);
            } else if (sampleStart != 0) {
                elapsed = System.currentTimeMillis() - sampleStart;
                result.setStampAndTime(sampleStart, elapsed);
                result.setIdleTime(elapsed);
            } else {
                LOGGER.warn("sampleStart hasn't been set - setting elapsed time to 0");
                result.setStampAndTime(System.currentTimeMillis(), 0);
                result.setIdleTime(0);
            }
        } else {
            // empty sample
            result.setStampAndTime(System.currentTimeMillis(), 0);
            result.setIdleTime(0);
        }
        String responseMessage = "";
        if (errorCount == 0) {
            final String formatString = sampler.isUseSenderTime() ? "{}: received batch of {} messages; average delivery time: {}ms"
                    : "{}: received batch of {} messages in {}ms";
            LOGGER.info(formatString, sampler.getThreadName(), messageCount, elapsed);
        } else {
            LOGGER.info("{}: received batch of {} messages with {} errors in {}ms", sampler.getThreadName(), messageCount, errorCount, elapsed);
            responseMessage = "got " + errorCount + " invalid messages";
        }
        if (senderClockNotInSync) {
            responseMessage = (responseMessage.isEmpty() ? "" : responseMessage + "; ") + "sender clock not in sync";
            LOGGER.error("The sender time extracted from at least one of the received messages is newer than the receiver time" +
                    " - sender and receiver clocks are not in sync. Consider deactivating usage of the sender time.");
        }
        result.setResponseMessage(responseMessage);

        // reset all fields
        totalSampleDeliveryTime = 0;
        senderClockNotInSync = false;
        bytesReceived = 0;
        messageCount = 0;
        errorCount = 0;
        sampleStart = 0;
    }
}
 
Example 3
Source File: HonoSender.java    From hono with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Publishes multiple messages to Hono.
 *
 * @param sampleResult The result object representing the combined outcome of the samples.
 * @param messageCount The number of messages to send
 * @param deviceId The identifier if the device to send a message for.
 * @param waitForDeliveryResult A flag indicating whether to wait for the result of the send operation.
 */
public void send(final SampleResult sampleResult, final int messageCount, final String deviceId,
                 final boolean waitForDeliveryResult) {
    final long sampleStart = System.currentTimeMillis();
    long addedSendDurations = 0;
    boolean isSuccessful = true;
    String firstResponseErrorMessage = "";
    String firstResponseErrorCode = "";
    long sentBytes = 0;
    int errorCount = 0;
    for (int i = 0; i < messageCount; i++) {
        final SampleResult subResult = new SampleResult();
        subResult.setDataType(SampleResult.TEXT);
        subResult.setResponseOK();
        subResult.setResponseCodeOK();
        subResult.setSampleLabel(sampleResult.getSampleLabel());
        // send the message
        send(subResult, deviceId, waitForDeliveryResult);
        // can't call sampleResult.addSubResult(subResult) here - this would prevent a later invocation of sampleResult.setStampAndTime()
        sampleResult.addRawSubResult(subResult);

        if (!subResult.isSuccessful()) {
            isSuccessful = false;
            errorCount++;
            if (firstResponseErrorMessage.isEmpty()) {
                firstResponseErrorMessage = subResult.getResponseMessage();
                firstResponseErrorCode = subResult.getResponseCode();
            }
        }
        sentBytes += subResult.getSentBytes();
        addedSendDurations += subResult.getTime();
    }
    sampleResult.setSuccessful(isSuccessful);
    final String responseMessage = MessageFormat.format("BatchResult {0}/{1}/{2}", sampler.getEndpoint(), sampler.getTenant(), deviceId);
    if (isSuccessful) {
        sampleResult.setResponseMessage(responseMessage);
    } else {
        sampleResult.setResponseMessage(responseMessage + ": " + errorCount + " errors - first: " + firstResponseErrorMessage);
        sampleResult.setResponseCode(firstResponseErrorCode);
    }
    sampleResult.setSentBytes(sentBytes);
    sampleResult.setSampleCount(messageCount);
    sampleResult.setErrorCount(errorCount); // NOTE: This method does nothing in JMeter 3.3/4.0
    final long averageElapsedTimePerMessage = addedSendDurations / messageCount;
    sampleResult.setStampAndTime(sampleStart, averageElapsedTimePerMessage);
}