Search in sources :

Example 1 with MongoDB

use of org.apache.jmeter.protocol.mongodb.mongo.MongoDB in project jmeter by apache.

the class MongoSourceElement method testStarted.

@Override
public void testStarted() {
    if (log.isDebugEnabled()) {
        log.debug(getTitle() + " testStarted");
    }
    MongoClientOptions.Builder builder = MongoClientOptions.builder().autoConnectRetry(getAutoConnectRetry()).connectTimeout(getConnectTimeout()).connectionsPerHost(getConnectionsPerHost()).maxAutoConnectRetryTime(getMaxAutoConnectRetryTime()).maxWaitTime(getMaxWaitTime()).socketKeepAlive(getSocketKeepAlive()).socketTimeout(getSocketTimeout()).threadsAllowedToBlockForConnectionMultiplier(getThreadsAllowedToBlockForConnectionMultiplier());
    if (getSafe()) {
        builder.writeConcern(WriteConcern.SAFE);
    } else {
        builder.writeConcern(new WriteConcern(getWriteOperationNumberOfServers(), getWriteOperationTimeout(), getFsync(), getWaitForJournaling(), getContinueOnInsertError()));
    }
    MongoClientOptions mongoOptions = builder.build();
    if (log.isDebugEnabled()) {
        log.debug("options : " + mongoOptions.toString());
    }
    if (getThreadContext().getVariables().getObject(getSource()) != null) {
        if (log.isWarnEnabled()) {
            log.warn(getSource() + " has already been defined.");
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug(getSource() + "  is being defined.");
        }
        try {
            getThreadContext().getVariables().putObject(getSource(), new MongoDB(MongoUtils.toServerAddresses(getConnection()), mongoOptions));
        } catch (UnknownHostException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) MongoClientOptions(com.mongodb.MongoClientOptions) WriteConcern(com.mongodb.WriteConcern) MongoDB(org.apache.jmeter.protocol.mongodb.mongo.MongoDB)

Example 2 with MongoDB

use of org.apache.jmeter.protocol.mongodb.mongo.MongoDB in project jmeter by apache.

the class MongoScriptSampler method sample.

@Override
public SampleResult sample(Entry e) {
    trace("sample()");
    SampleResult res = new SampleResult();
    String data = getScript();
    res.setSampleLabel(getTitle());
    res.setResponseCodeOK();
    // $NON-NLS-1$
    res.setResponseCode("200");
    res.setSuccessful(true);
    res.setResponseMessageOK();
    res.setSamplerData(data);
    res.setDataType(SampleResult.TEXT);
    // $NON-NLS-1$
    res.setContentType("text/plain");
    res.sampleStart();
    try {
        MongoDB mongoDB = MongoSourceElement.getMongoDB(getSource());
        MongoScriptRunner runner = new MongoScriptRunner();
        DB db = mongoDB.getDB(getDatabase(), getUsername(), getPassword());
        res.latencyEnd();
        Object result = runner.evaluate(db, data);
        EvalResultHandler handler = new EvalResultHandler();
        String resultAsString = handler.handle(result);
        res.setResponseData(resultAsString.getBytes());
    } catch (Exception ex) {
        // $NON-NLS-1$
        res.setResponseCode("500");
        res.setSuccessful(false);
        res.setResponseMessage(ex.toString());
        res.setResponseData(ex.getMessage().getBytes());
    } finally {
        res.sampleEnd();
    }
    return res;
}
Also used : SampleResult(org.apache.jmeter.samplers.SampleResult) MongoDB(org.apache.jmeter.protocol.mongodb.mongo.MongoDB) DB(com.mongodb.DB) MongoDB(org.apache.jmeter.protocol.mongodb.mongo.MongoDB) EvalResultHandler(org.apache.jmeter.protocol.mongodb.mongo.EvalResultHandler)

Aggregations

MongoDB (org.apache.jmeter.protocol.mongodb.mongo.MongoDB)2 DB (com.mongodb.DB)1 MongoClientOptions (com.mongodb.MongoClientOptions)1 WriteConcern (com.mongodb.WriteConcern)1 UnknownHostException (java.net.UnknownHostException)1 EvalResultHandler (org.apache.jmeter.protocol.mongodb.mongo.EvalResultHandler)1 SampleResult (org.apache.jmeter.samplers.SampleResult)1