-
-
Save lutovich/02c1a320f6e02dd5f20d62555af9c3c4 to your computer and use it in GitHub Desktop.
Try to reproduce EPIPE error in node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"async": "^2.1.4", | |
"neo4j-driver": "^1.1.0", | |
"uuid": "^3.0.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var eachLimit = require('async/eachLimit'); | |
var uuid = require("uuid"); | |
var neo4j = require('neo4j-driver').v1; | |
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "test")); | |
var usersCount = 10000; | |
var productCount = 20000; | |
var queriesCount = 100000; | |
var userIds = randomUuids(usersCount); | |
var productIds = randomUuids(productCount); | |
var queryParams = randomQueryParams(queriesCount, userIds, productIds); | |
createUsers(userIds).then(function () { | |
createProducts(productIds).then(function () { | |
var stats = {queriesExecuted: 0}; | |
eachLimit(queryParams, 8, function (params, callback) { | |
executeQuery(params, callback, stats); | |
}, queriesCompleted); | |
}); | |
}); | |
driver.onError = function (error) { | |
console.log('Driver instantiation failed ', error); | |
}; | |
function executeQuery(params, callback, stats) { | |
var query = | |
'MATCH (u:User{flake_id:{uid}}), (v:Variant{product_id:{subcr_plan}}) ' + | |
'CREATE (u)-[:created]->(o:Order{flake_id:{oid}})-[:contains]->(v), ' + | |
'(u)-[:paid_with]->(t:Transaction{flake_id:{tid}})-[:paid_for]->(o) ' + | |
'SET ' + | |
'o.created_at = {order_created_at}, ' + | |
'o.updated_at = o.created_at, ' + | |
'o.source = \'ACN\', ' + | |
'o.state = \'paid\', ' + | |
't.amount_in_cents = {amountInCents}, ' + | |
't.gateway_used = \'stripe\', ' + | |
't.masked_card_number = {maskedCardNumber}, ' + | |
't.currency = {currency}, ' + | |
't.card_type = {cardType}, ' + | |
't.type = \'Credit Card\', ' + | |
't.gateway_transaction_id = {txId}, ' + | |
't.created_at = {tx_created_at}, ' + | |
't.updated_at = t.created_at ' + | |
'RETURN u, o, t'; | |
var session = driver.session(); | |
session.run(query, params).then(function (result) { | |
session.close(); | |
stats.queriesExecuted++; | |
if (stats.queriesExecuted % 1000 === 0) { | |
console.log('Executed ' + stats.queriesExecuted + ' queries'); | |
} | |
if (result.records.length === 0) { | |
callback(new Error('Query did not return results')); | |
} else { | |
callback(); | |
} | |
}).catch(function (error) { | |
callback(error); | |
}); | |
} | |
function queriesCompleted(error) { | |
if (error) { | |
console.log('Async queries failed: ', error); | |
process.exit(1); | |
} else { | |
console.log('Async queries finished'); | |
process.exit(); | |
} | |
} | |
function createUsers(userIds) { | |
var session = driver.session(); | |
return session.run('UNWIND {userIds} AS userId CREATE (u:User{flake_id: userId})', {userIds: userIds}) | |
.then(function (result) { | |
var nodesCreated = result.summary.counters.nodesCreated(); | |
if (nodesCreated !== userIds.length) { | |
throw 'Wrong amount of users created: ' + nodesCreated; | |
} | |
console.log('Users created'); | |
session.close(); | |
}).catch(function (error) { | |
console.log('User creation failed: ', error); | |
}); | |
} | |
function createProducts(productIds) { | |
var session = driver.session(); | |
return session.run('UNWIND {productIds} AS productId CREATE (v:Variant{product_id: productId})', {productIds: productIds}) | |
.then(function (result) { | |
var nodesCreated = result.summary.counters.nodesCreated(); | |
if (nodesCreated !== productIds.length) { | |
throw 'Wrong amount of products created: ' + nodesCreated; | |
} | |
console.log('Products created'); | |
session.close(); | |
}).catch(function (error) { | |
console.log('Product creation failed: ', error); | |
}); | |
} | |
function randomQueryParams(count, userIds, productIds) { | |
var result = []; | |
for (var i = 0; i < count; i++) { | |
result.push({ | |
uid: randomOf(userIds), | |
subcr_plan: randomOf(productIds), | |
order_created_at: Date.now(), | |
tx_created_at: Date.now(), | |
oid: uuid.v4(), | |
tid: uuid.v4(), | |
amountInCents: Math.random(), | |
maskedCardNumber: uuid.v4(), | |
currency: uuid.v4(), | |
cardType: uuid.v4(), | |
txId: uuid.v4() | |
}); | |
} | |
return result; | |
} | |
function randomUuids(count) { | |
var result = []; | |
for (var i = 0; i < count; i++) { | |
result.push(uuid.v4()); | |
} | |
return result; | |
} | |
function randomOf(array) { | |
return array[Math.floor(Math.random() * array.length)]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment