Skip to content

Instantly share code, notes, and snippets.

@gerrard00
Created November 13, 2018 17:56
Show Gist options
  • Save gerrard00/8f45e47412deabe00537b97470ce4194 to your computer and use it in GitHub Desktop.
Save gerrard00/8f45e47412deabe00537b97470ce4194 to your computer and use it in GitHub Desktop.
Neptune Concurrency 2
const gremlin = require('gremlin');
const uuidv4 = require('uuid/v4');
const { t: { id }, cardinality } = gremlin.process;
const __ = gremlin.process.statics;
const hostname = process.env.NEPTUNE_HOST;
const testToRun = process.argv[2];
const threadsRequested = process.argv[3] ? Number(process.argv[3], 10) : 5;
async function runTest(testSpec, idValue, threadNumber) {
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
const dc = new DriverRemoteConnection(`ws://${hostname}/gremlin`);
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
const testResult = { threadNumber };
try {
let query =
g.V()
.hasLabel('OrgNode_CONST')
.has(id, idValue).fold()
.coalesce(
__.unfold(),
__.addV('OrgNode_CONST').property(id, idValue)
);
// only set a property value if a generator was provided
if (testSpec.propertyNameGenerator) {
const propertyName = testSpec.propertyNameGenerator(threadNumber)
const propertyValue = testSpec.propertyValueGenerator(threadNumber);
// change the property
query = query.property(cardinality.single, propertyName, propertyValue)
}
const queryResult = await query.iterate();
testResult.success = true;
} catch(err) {
testResult.success = false;
testResult.error = err.message;
}
return testResult;
}
async function runTests(testSpec, idValue, threads) {
if (testSpec.preCreate) {
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
const dc = new DriverRemoteConnection(`ws://${hostname}/gremlin`);
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
await g.addV('OrgNode_CONST').property(id, idValue).next();
}
const promises = [...new Array(threads)]
.map((_, index) => runTest(testSpec, idValue, index + 1));
return Promise.all(promises);
}
const tests = [
{
description: 'new-node-no-property',
preCreate: false,
propertyNameGenerator: null,
propertyValueGenerator: null,
},
{
description: 'existing-node-no-property',
preCreate: true,
propertyNameGenerator: null,
propertyValueGenerator: null,
},
{
description: 'new-node-static-property',
preCreate: false,
propertyNameGenerator: () => 'some-property',
propertyValueGenerator: () => 'some-value',
},
{
description: 'existing-node-static-property',
preCreate: true,
propertyNameGenerator: () => 'some-property',
propertyValueGenerator: () => 'some-value',
},
{
description: 'new-node-dynamic-property-value',
preCreate: false,
propertyNameGenerator: () => 'some-property',
propertyValueGenerator: (idx) => `some-value-${idx}`,
},
{
description: 'existing-node-dynamic-property-value',
preCreate: true,
propertyNameGenerator: () => 'some-property',
propertyValueGenerator: (idx) => `some-value-${idx}`,
},
{
description: 'new-node-dynamic-property-name',
preCreate: false,
propertyNameGenerator: (idx) => `some-property-${idx}`,
propertyValueGenerator: (idx) => `some-value-${idx}`,
},
{
description: 'existing-node-dynamic-property-name',
preCreate: true,
propertyNameGenerator: (idx) => `some-property-${idx}`,
propertyValueGenerator: (idx) => `some-value-${idx}`,
},
];
(async function() {
try {
const testSpec = tests.find(test => test.description === testToRun);
if (!testSpec) {
console.error(`Unknown test ${testToRun}.`);
process.exit(-1);
}
const idValue = uuidv4();
const allTestResults = await runTests(testSpec, idValue, threadsRequested);
const sortedTestResults = allTestResults.sort((a, b) => a - b);
const finalResults = {
testName: testToRun,
idValue,
results: sortedTestResults,
};
console.log(JSON.stringify(finalResults, null, 2));
const failureCount = allTestResults.filter(result => !result.success).length;
process.exit(failureCount);
} catch (err) {
console.error(err);
process.exit(2);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment