Created
December 1, 2022 16:45
-
-
Save jexp/b9bd3705493980ef5937c86b14e76ba8 to your computer and use it in GitHub Desktop.
Neo4j single transaction testing using java core api and drivers, using https://jbang.dev
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
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//JAVA 17 | |
//DEPS org.neo4j:neo4j:5.2.0 | |
import static java.lang.System.*; | |
import org.neo4j.dbms.api.*; | |
import org.neo4j.graphdb.*; | |
import java.nio.file.Path; | |
import java.util.Map; | |
import java.util.stream.*; | |
import java.util.concurrent.TimeUnit; | |
public class TestDb { | |
private static int TX=1_000; | |
private final static String PATH = "test"; | |
// jbang test-db | |
// CN: Executing 10000 transactions took 204 seconds resulting in 49,02 tx/s | |
// CN3: Executing 1000 transactions took 20 seconds resulting in 50,00 tx/s | |
public static void main(String... args) { | |
if (args.length > 0) TX = Integer.parseInt(args[0]); | |
out.println("Starting db"); | |
var dbs = new DatabaseManagementServiceBuilder(Path.of(PATH)).build(); | |
var db = dbs.database("neo4j"); | |
out.println("Started db"); | |
for (int i=0;i<100;i++) createNodeCypher(db, i); | |
out.println("Finished Warmup, starting measurement"); | |
long start = currentTimeMillis(); | |
IntStream.range(0,TX) | |
// .parallel() | |
.forEach(i -> createNodeCypher(db,i)); | |
long delta = TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis()-start); | |
out.printf("Executing %d transactions took %d seconds resulting in %.2f tx/s%n", TX, delta, (float)(TX)/delta); | |
out.println("Shutting down db"); | |
dbs.shutdown(); | |
out.println("Shut down db"); | |
} | |
private static void createNodeCypherTx(GraphDatabaseService db, int id) { | |
try (var tx = db.beginTx()) { | |
tx.execute("CREATE (:Account {id:$id})",Map.of("id",id)).close(); | |
tx.commit(); | |
} | |
} | |
private static void createNodeCoreAPI(GraphDatabaseService db, int id) { | |
try (var tx = db.beginTx()) { | |
var node = tx.createNode(Label.label("Account")); | |
node.setProperty("id",id); | |
tx.commit(); | |
} | |
} | |
private static void createNodeCypher(GraphDatabaseService db, int id) { | |
db.executeTransactionally("CREATE (:Account {id:$id})",Map.of("id",id)); | |
} | |
} |
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
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//JAVA 17 | |
//DEPS org.neo4j.driver:neo4j-java-driver:5.3.0 | |
import static java.lang.System.*; | |
import org.neo4j.driver.*; | |
import java.util.stream.*; | |
import java.util.Map; | |
import java.util.concurrent.TimeUnit; | |
public class TestDriver { | |
// source test-db-credentials-a738e93c.env | |
// NEO4J_URI=$NEO4J_URI NEO4J_PASSWORD=$NEO4J_PASSWORD jbang test-driver | |
public static void main(String... args) { | |
var URI = getenv("NEO4J_URI")==null ? "neo4j://localhost" : getenv("NEO4J_URI"); | |
var PASS = getenv("NEO4J_PASSWORD")==null ? "test" : getenv("NEO4J_PASSWORD"); | |
var TX = getenv("NEO4J_TRANSACTIONS")==null ? 1000 : Integer.parseInt(getenv("NEO4J_TRANSACTIONS")); | |
var driver = GraphDatabase.driver(URI,AuthTokens.basic("neo4j",PASS)); | |
out.println("Connected to db: "+URI); | |
try (var session = newSession(driver)) { | |
IntStream.range(0,100).forEach(i -> createNode(session, i)); | |
} | |
out.println("Finished Warmup"); | |
int PROCS = Runtime.getRuntime().availableProcessors(); | |
int perThread = TX / PROCS; | |
out.printf("Starting measurement %d Transactions with %d threads %n",TX, PROCS); | |
long start = currentTimeMillis(); | |
IntStream.range(0, PROCS) | |
.parallel() | |
.mapToObj(i -> newSession(driver)) | |
.peek(s -> IntStream.range(0,perThread).forEach(i-> createNode(s,i))) | |
.forEach(s -> s.close()); | |
long delta = TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis()-start); | |
out.printf("Executing %d transactions with %d threads took %d seconds resulting in %.2f tx/s%n", TX, PROCS, delta, (float)(TX)/delta); | |
out.println("Shutting down driver"); | |
driver.close(); | |
out.println("Shut down driver"); | |
} | |
private static Session newSession(Driver driver) { | |
return driver.session(SessionConfig.forDatabase("neo4j")); | |
} | |
private static void createNode(Session session, int id) { | |
session.executeWrite(tx -> { | |
tx.run("CREATE (:Account {id:$id})",Map.of("id",id)).consume(); | |
return null; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment