Last active
December 30, 2019 17:32
-
-
Save chrvadala/d2d4d1363e501e678ff932ff5206c739 to your computer and use it in GitHub Desktop.
Neo4j Insert benchmarks
This file contains hidden or 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
const neo4j = require('neo4j-driver'); | |
const uri = 'bolt://localhost:7687' | |
async function init(tx) { | |
await tx.run('MATCH ()-[k]->() DELETE k') | |
await tx.run('MATCH (n) DELETE n') | |
await tx.run('CREATE (p:A)') | |
await tx.run('CREATE (p:B)') | |
} | |
async function createUser(tx, name) { | |
const res = await tx.run( | |
"CREATE (p:User {name: $name}) RETURN p", | |
{name} | |
) | |
const record = res.records[0] | |
const user = record.get('p') | |
return { | |
_id: user.identity.toInt(), | |
...user.properties, | |
} | |
} | |
async function link(tx, relationship) { | |
await tx.run("MATCH(a:A) MATCH(b:B) CREATE (a)-[:" + relationship + "]->(b)", | |
{relationship} | |
) | |
} | |
// async function bulkInsert(driver) { | |
// const K = 100000 | |
// const session = driver.session(); | |
// const users = [] | |
// for (let i = 0; i < K; i++) { | |
// const u = await session.writeTransaction(tx => createUser(tx, `Bar_${i}`)) | |
// users.push(u) | |
// } | |
// await session.close() | |
// return users | |
// } | |
async function bulkInsert(driver) { | |
const K = 100 * 1000 | |
const session = driver.session(); | |
const tx = await session.beginTransaction() | |
const users = [] | |
for (let i = 0; i < K; i++) { | |
const u = await createUser(tx, `Bar_${i}`) | |
users.push(u) | |
} | |
await tx.commit() | |
await session.close() | |
return users | |
} | |
async function bulkLink(driver) { | |
const K = 10 * 1000 | |
const session = driver.session() | |
const tx = await session.beginTransaction() | |
for (let i = 0; i < K; i++) { | |
await link(tx, `FOO_${i}`) | |
} | |
await tx.commit() | |
await session.close() | |
} | |
async function main() { | |
const driver = neo4j.driver(uri); | |
console.time('init') | |
await init(driver.session()) | |
console.timeEnd('init') | |
console.time('bulkInsert') | |
await bulkInsert(driver) | |
console.timeEnd('bulkInsert') | |
console.time('bulkLink') | |
await bulkLink(driver) | |
console.timeEnd('bulkLink') | |
await driver.close() | |
} | |
main().then(console.log).catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Macbook