Search in sources :

Example 1 with UnifiedSyncTest

use of com.mongodb.client.unified.UnifiedSyncTest in project mongo-java-driver by mongodb.

the class WorkloadExecutor method main.

public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        System.out.println("Usage: AstrolabeTestRunner <path to workload spec JSON file> <path to results directory>");
        System.exit(1);
    }
    String pathToWorkloadFile = args[0];
    String pathToResultsDirectory = args[1];
    LOGGER.info("Max memory (GB): " + (Runtime.getRuntime().maxMemory() / 1_073_741_824.0));
    LOGGER.info("Path to workload file: '" + pathToWorkloadFile + "'");
    LOGGER.info("Path to results directory: '" + pathToResultsDirectory + "'");
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        LOGGER.info("Running shutdown hook");
        terminateLoop = true;
        try {
            if (!terminationLatch.await(1, TimeUnit.MINUTES)) {
                LOGGER.warn("Terminating after waiting for 1 minute for results to be written");
            } else {
                LOGGER.info("Terminating.");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }));
    BsonDocument fileDocument;
    try (FileReader reader = new FileReader(pathToWorkloadFile)) {
        fileDocument = new BsonDocumentCodec().decode(new JsonReader(reader), DecoderContext.builder().build());
    }
    LOGGER.info("Executing workload: " + fileDocument.toJson(JsonWriterSettings.builder().indent(true).build()));
    BsonArray testArray = fileDocument.getArray("tests");
    if (testArray.size() != 1) {
        throw new IllegalArgumentException("Expected exactly one test");
    }
    BsonDocument testDocument = testArray.get(0).asDocument();
    UnifiedTest unifiedTest = new UnifiedSyncTest(fileDocument.getString("schemaVersion").getValue(), fileDocument.getArray("runOnRequirements", null), fileDocument.getArray("createEntities", new BsonArray()), fileDocument.getArray("initialData", new BsonArray()), testDocument) {

        @Override
        protected boolean terminateLoop() {
            return terminateLoop;
        }
    };
    try {
        unifiedTest.setUp();
        unifiedTest.shouldPassAllOutcomes();
        Entities entities = unifiedTest.getEntities();
        long iterationCount = -1;
        if (entities.hasIterationCount("iterations")) {
            iterationCount = entities.getIterationCount("iterations");
        }
        long successCount = -1;
        if (entities.hasSuccessCount("successes")) {
            successCount = entities.getSuccessCount("successes");
        }
        BsonArray errorDocuments = null;
        long errorCount = 0;
        if (entities.hasErrorDocuments("errors")) {
            errorDocuments = entities.getErrorDocuments("errors");
            errorCount = errorDocuments.size();
        }
        BsonArray failureDocuments = null;
        long failureCount = 0;
        if (entities.hasFailureDocuments("failures")) {
            failureDocuments = entities.getFailureDocuments("failures");
            failureCount = failureDocuments.size();
        }
        BsonArray eventDocuments = new BsonArray();
        if (entities.hasEvents("events")) {
            eventDocuments = new BsonArray(entities.getEvents("events"));
        }
        BsonDocument eventsDocument = new BsonDocument().append("errors", errorDocuments == null ? new BsonArray() : errorDocuments).append("failures", failureDocuments == null ? new BsonArray() : failureDocuments).append("events", eventDocuments);
        BsonDocument resultsDocument = new BsonDocument().append("numErrors", new BsonInt64(errorCount)).append("numFailures", new BsonInt64(failureCount)).append("numSuccesses", new BsonInt64(successCount)).append("numIterations", new BsonInt64(iterationCount));
        writeFile(eventsDocument, Paths.get(pathToResultsDirectory, "events.json"));
        writeFile(resultsDocument, Paths.get(pathToResultsDirectory, "results.json"));
    } finally {
        unifiedTest.cleanUp();
        terminationLatch.countDown();
    }
}
Also used : UnifiedSyncTest(com.mongodb.client.unified.UnifiedSyncTest) Entities(com.mongodb.client.unified.Entities) BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) JsonReader(org.bson.json.JsonReader) FileReader(java.io.FileReader) UnifiedTest(com.mongodb.client.unified.UnifiedTest) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Aggregations

Entities (com.mongodb.client.unified.Entities)1 UnifiedSyncTest (com.mongodb.client.unified.UnifiedSyncTest)1 UnifiedTest (com.mongodb.client.unified.UnifiedTest)1 FileReader (java.io.FileReader)1 BsonArray (org.bson.BsonArray)1 BsonDocument (org.bson.BsonDocument)1 BsonInt64 (org.bson.BsonInt64)1 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)1 JsonReader (org.bson.json.JsonReader)1