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

The following examples show how to use org.apache.jmeter.samplers.SampleResult#setDataType() . 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: HttpTestBean.java    From BotServiceStressToolkit with MIT License 6 votes vote down vote up
@Override
public SampleResult sample(Entry entry) {
	SampleResult res = new SampleResult();
	res.setSampleLabel(getName());
	res.sampleStart();
	res.setDataType(SampleResult.TEXT);

	try {
		JMeterContext context = getThreadContext();
		JMeterVariables vars = context.getVariables();

		String body = Unirest.get("http://www.uol.com.br").asString().getBody();

		res.setResponseData(body, null);
		res.sampleEnd();
		res.setSuccessful(true);

	} catch (UnirestException e) {
		res.setResponseData(e.toString(), null);
		res.sampleEnd();
		res.setSuccessful(false);
	} 

	return res;
}
 
Example 2
Source File: HonoSenderSampler.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public SampleResult sample(final Entry entry) {

    final SampleResult res = new SampleResult();
    res.setDataType(SampleResult.TEXT);
    res.setResponseOK();
    res.setResponseCodeOK();
    res.setSampleLabel(getName());
    if (getMessageCountPerSamplerRunAsInt() == 1) {
        honoSender.send(res, getDeviceId(), isWaitForDeliveryResult());
    } else {
        honoSender.send(res, getMessageCountPerSamplerRunAsInt(), getDeviceId(),
                isWaitForDeliveryResult());
    }
    return res;
}
 
Example 3
Source File: AbstractIPSampler.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public SampleResult sample(Entry entry) {
    SampleResult res = new SampleResult();
    res.setSampleLabel(getName());
    res.setSamplerData(getRequestData());
    res.sampleStart();
    res.setDataType(SampleResult.TEXT);
    res.setSuccessful(true);
    res.setResponseCode(RC200);
    try {
        res.setResponseData(processIO(res));
    } catch (Exception ex) {
        if (!(ex instanceof SocketTimeoutException)) {
            log.error(getHostName(), ex);
        }
        res.sampleEnd();
        res.setSuccessful(false);
        res.setResponseCode(RC500);
        res.setResponseMessage(ex.toString());
        res.setResponseData((ex.toString() + CRLF + ExceptionUtils.getStackTrace(ex)).getBytes());
    }

    return res;
}
 
Example 4
Source File: JSONToXMLConverter.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public SampleResult sample(Entry e) {
    SampleResult result = new SampleResult();

    result.setSampleLabel(getName());
    result.setSamplerData(this.getJsonInput());
    result.setDataType(SampleResult.TEXT);

    result.sampleStart();

    if (!getJsonInput().equalsIgnoreCase("")) {
        try {
            this.convertToXML();
            result.setResponseData(this.getXmlOutput().getBytes());
            result.setSuccessful(true);
        } catch (Exception e1) {
            result.setResponseData(e1.getMessage().getBytes());
            result.setSuccessful(false);
        }
    }

    result.sampleEnd();
    return result;
}
 
Example 5
Source File: TopicMessageGeneratorClient.java    From hedera-mirror-node with Apache License 2.0 5 votes vote down vote up
/**
 * Runs test by calling sampler to manage topic message table. Reports success based on successful db operations
 *
 * @return Sample result
 */
@Override
public SampleResult runTest(JavaSamplerContext context) {
    SampleResult result = new SampleResult();

    try {
        // establish db connection in test to ensure failures are reported
        connectionHandler = new ConnectionHandler(host, port, dbName, dbUser, dbPassword);
        sampler = new TopicMessageGeneratorSampler(connectionHandler);

        MessageGenerator messageGen = MessageGenerator.builder()
                .topicNum(context.getLongParameter("topicID", 0))
                .historicMessagesCount(context.getIntParameter("historicMessagesCount", 0))
                .futureMessagesCount(context.getIntParameter("newTopicsMessageCount", 0))
                .topicMessageEmitCycles(context.getIntParameter("topicMessageEmitCycles", 0))
                .newTopicsMessageDelay(context.getLongParameter("newTopicsMessageDelay", 0L))
                .deleteFromSequence(context.getLongParameter("delSeqFrom", -1L))
                .build();

        result.sampleStart();
        sampler.populateTopicMessages(messageGen);
        result.sampleEnd();
        result.setResponseMessage("Successfully performed populateTopicMessages");
        result.setResponseCodeOK();
        result.setSuccessful(true);
    } catch (Exception ex) {
        log.error("Error populating topic messages", ex);
        StringWriter stringWriter = new StringWriter();
        ex.printStackTrace(new PrintWriter(stringWriter));
        result.sampleEnd();
        result.setResponseMessage("Exception: " + ex);
        result.setResponseData(stringWriter.toString().getBytes());
        result.setDataType(SampleResult.TEXT);
        result.setResponseCode("500");
    }

    return result;
}
 
Example 6
Source File: DubboSample.java    From jmeter-plugins-for-apache-dubbo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
   public SampleResult sample(Entry entry) {
       SampleResult res = new SampleResult();
       res.setSampleLabel(getName());
       //构造请求数据
       res.setSamplerData(getSampleData());
       //调用dubbo
       res.setResponseData(JsonUtils.toJson(callDubbo(res)), StandardCharsets.UTF_8.name());
       //构造响应数据
       res.setDataType(SampleResult.TEXT);
       return res;
   }
 
Example 7
Source File: HonoReceiverSampler.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public SampleResult sample(final Entry entry) {
    final SampleResult res = new SampleResult();
    res.setResponseOK();
    res.setDataType(SampleResult.TEXT);
    res.setSampleLabel(getName());
    honoReceiver.sample(res);
    return res;
}
 
Example 8
Source File: KafkaProducerSampler.java    From kafkameter with Apache License 2.0 5 votes vote down vote up
/**
 * Factory for creating new {@link SampleResult}s.
 */
private SampleResult newSampleResult() {
  SampleResult result = new SampleResult();
  result.setDataEncoding(ENCODING);
  result.setDataType(SampleResult.TEXT);
  return result;
}
 
Example 9
Source File: MultiTopicHCSClient.java    From hedera-mirror-node with Apache License 2.0 4 votes vote down vote up
private SampleResult sequentialRun() {
    SampleResult result = new SampleResult();
    int successSamplesCount = 0;
    result.sampleStart();

    try {
        HCSSamplerResult response = null;
        for (TopicSubscription subscription : consensusServiceReactiveSamplers.keySet()) {
            if (subscription.getMilliSecWaitBefore() > 0) {
                log.debug("Waiting {} ms before subscribing", subscription.getMilliSecWaitBefore());
                Thread.sleep(subscription.getMilliSecWaitBefore(), 0);
            }

            MessageListener listener = MessageListener.builder()
                    .historicMessagesCount(subscription.getHistoricMessagesCount())
                    .futureMessagesCount(subscription.getIncomingMessageCount())
                    .messagesLatchWaitSeconds(subscription.getSubscribeTimeoutSeconds())
                    .build();

            response = consensusServiceReactiveSamplers.get(subscription).subscribeTopic(listener);

            if (!response.isSuccess()) {
                log.debug("Failure in subscribe topic test : " + subscription);
            } else {
                successSamplesCount++;
                log.debug("Successfully performed subscription : " + subscription);
            }
        }

        result.setResponseMessage("Successfully performed subscriptions");
        result.setResponseCodeOK();
        result.sampleEnd();
        result.setResponseData(response.toString().getBytes());
        result.setSuccessful(response.isSuccess());
    } catch (Exception ex) {
        log.error("Error subscribing to topic", ex);

        StringWriter stringWriter = new StringWriter();
        ex.printStackTrace(new PrintWriter(stringWriter));

        result.setResponseMessage("Failure in subscribes");
        result.setResponseCode("500");
        result.sampleEnd();
        result.setResponseMessage("Exception: " + ex);
        result.setResponseData(stringWriter.toString().getBytes());
        result.setDataType(SampleResult.TEXT);
        result.setResponseCode("500");
    }

    log.info("{} out of {} samples passed", successSamplesCount, clientCount);

    return result;
}
 
Example 10
Source File: SingleTopicHCSClient.java    From hedera-mirror-node with Apache License 2.0 4 votes vote down vote up
/**
 * Runs test by calling sampler subscribeTopic. Reports success based on call response from sampler
 */
@Override
public SampleResult runTest(JavaSamplerContext context) {
    SampleResult result = new SampleResult();
    HCSSamplerResult response = null;
    result.sampleStart();

    try {
        MessageListener listener = MessageListener.builder()
                .historicMessagesCount(propHandler.getIntClientTestParam("HistoricMessagesCount", 0, "0"))
                .futureMessagesCount(propHandler.getIntClientTestParam("IncomingMessageCount", 0, "0"))
                .messagesLatchWaitSeconds(propHandler.getIntClientTestParam("SubscribeTimeoutSeconds", 0, "60"))
                .build();

        response = hcsTopicSampler.subscribeTopic(listener);

        result.sampleEnd();
        result.setResponseData(response.toString().getBytes());
        result.setSuccessful(response.isSuccess());

        if (!response.isSuccess()) {
            result.setResponseMessage("Failure in subscribe topic test");
            result.setResponseCode("500");
        } else {
            result.setResponseMessage("Successfully performed subscribe topic test");
            result.setResponseCodeOK();
        }
    } catch (Exception ex) {
        log.error("Error subscribing to topic", ex);

        StringWriter stringWriter = new StringWriter();
        ex.printStackTrace(new PrintWriter(stringWriter));
        result.sampleEnd();
        result.setResponseMessage("Exception: " + ex);
        result.setResponseData(stringWriter.toString().getBytes());
        result.setDataType(SampleResult.TEXT);
        result.setResponseCode("500");
    }

    return result;
}
 
Example 11
Source File: JMeterXMPPSampler.java    From jmeter-bzm-plugins with Apache License 2.0 4 votes vote down vote up
@Override
public SampleResult sample(Entry entry) {
    SampleResult res = new SampleResult();
    res.setSampleLabel(getName());
    res.setDataType(SampleResult.TEXT);
    res.setSuccessful(true);
    res.setResponseCode("200");
    res.setResponseMessage("OK");

    res.sampleStart();
    try {
        if (connConfig == null) {
            throw new RuntimeException("Cannot sample XMPP without XMPP Connection component");
        }

        XMPPConnection conn = getXMPPConnection();

        if (conn == null) {
            throw new RuntimeException("No XMPP Connection available");
        }

        String headers = "Connection ID: " + conn.getConnectionID() + "\r\n";

        String action = getAction();
        if (!conn.isConnected() && !action.equals(Connect.class.getCanonicalName())) {
            log.error("Please call Connect before calling other actions");
            throw new SmackException.NotConnectedException();
        }

        headers += "User: " + conn.getUser() + "\r\n";

        res.setRequestHeaders(headers);

        AbstractXMPPAction actObject = connConfig.getActions().get(action);
        if (actObject.perform(this, res) == null) {
            return null;
        }
    } catch (Exception e) {
        log.error("Error in XMPP Sampler: ", e);
        res.setSuccessful(false);
        res.setResponseCode("500");
        res.setResponseMessage((e.getMessage() == null || e.getMessage().isEmpty()) ? e.toString() : e.getMessage());
        res.setResponseData(ExceptionUtils.getStackTrace(e).getBytes());
    }

    res.sampleEnd();
    return res;
}
 
Example 12
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);
}
 
Example 13
Source File: CassandraSampler.java    From jmeter-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
    public SampleResult sample(Entry e) {
        log.debug("sampling CQL");

        SampleResult res = new SampleResult();
        res.setSampleLabel(getName());
        res.setSamplerData(toString());
        res.setDataType(SampleResult.TEXT);
        res.setContentType("text/plain"); // $NON-NLS-1$
        res.setDataEncoding(ENCODING);

        // Assume we will be successful
        res.setSuccessful(true);
        res.setResponseMessageOK();
        res.setResponseCodeOK();


        res.sampleStart();
        Session conn = null;

        try {
            if(JOrphanUtils.isBlank(getSessionName())) {
                throw new IllegalArgumentException("Variable Name must not be null in "+getName());
            }

            try {
                conn = CassandraConnection.getSession(getSessionName());
            } finally {
                res.latencyEnd(); // use latency to measure connection time
            }
            res.setResponseHeaders(conn.toString());
            res.setResponseData(execute(conn));
        }  catch (Exception ex) {
            res.setResponseMessage(ex.toString());
            res.setResponseCode("000");
            res.setResponseData(ex.getMessage().getBytes());
            res.setSuccessful(false);
        }
// Doesn't apply
//        finally {
//            close(conn);
//        }

        // TODO: process warnings? Set Code and Message to success?
        res.sampleEnd();
        return res;
    }