diff --git a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample1.java b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample1.java index 7bece68967..9e014c5bb6 100644 --- a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample1.java +++ b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample1.java @@ -36,7 +36,7 @@ */ public class PerfExample1 extends PerfExampleBase { - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws Exception { PerfExample1 tester = new PerfExample1(); tester.test(args); @@ -46,11 +46,11 @@ public static void main(String[] args) throws InterruptedException { @Override protected void initSchema(SchemaManager schema) { - schema.propertyKey("name").asText().create(); - schema.propertyKey("age").asInt().create(); - schema.propertyKey("lang").asText().create(); - schema.propertyKey("date").asText().create(); - schema.propertyKey("price").asInt().create(); + schema.propertyKey("name").asText().ifNotExist().create(); + schema.propertyKey("age").asInt().ifNotExist().create(); + schema.propertyKey("lang").asText().ifNotExist().create(); + schema.propertyKey("date").asText().ifNotExist().create(); + schema.propertyKey("price").asInt().ifNotExist().create(); schema.vertexLabel("person") .properties("name", "age") diff --git a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample2.java b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample2.java index 24550a4950..50ed4c33b4 100644 --- a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample2.java +++ b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample2.java @@ -36,7 +36,7 @@ */ public class PerfExample2 extends PerfExampleBase { - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws Exception { PerfExample2 tester = new PerfExample2(); tester.test(args); @@ -46,7 +46,7 @@ public static void main(String[] args) throws InterruptedException { @Override protected void initSchema(SchemaManager schema) { - schema.propertyKey("name").asText().create(); + schema.propertyKey("name").asText().ifNotExist().create(); schema.vertexLabel("person") .useAutomaticId() diff --git a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample3.java b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample3.java index 7f0a555c1a..4f981a8921 100644 --- a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample3.java +++ b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample3.java @@ -34,7 +34,7 @@ public class PerfExample3 extends PerfExampleBase { private static final Logger LOG = Log.logger(PerfExample3.class); - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws Exception { PerfExample3 tester = new PerfExample3(); tester.test(args); @@ -46,24 +46,26 @@ public static void main(String[] args) throws InterruptedException { protected void initSchema(SchemaManager schema) { // Schema changes will be commit directly into the back-end LOG.info("=============== propertyKey ================"); - schema.propertyKey("id").asInt().create(); - schema.propertyKey("name").asText().create(); - schema.propertyKey("age").asInt().valueSingle().create(); - schema.propertyKey("city").asText().create(); + schema.propertyKey("id").asInt().ifNotExist().create(); + schema.propertyKey("name").asText().ifNotExist().create(); + schema.propertyKey("age").asInt().valueSingle().ifNotExist().create(); + schema.propertyKey("city").asText().ifNotExist().create(); LOG.info("=============== vertexLabel ================"); schema.vertexLabel("person") .properties("name", "age", "city") .primaryKeys("name") - .create(); + .ifNotExist().create(); LOG.info("=============== vertexLabel & index ================"); schema.indexLabel("personByCity") - .onV("person").secondary().by("city").create(); + .onV("person").secondary().by("city") + .ifNotExist().create(); schema.indexLabel("personByAge") - .onV("person").range().by("age").create(); + .onV("person").range().by("age") + .ifNotExist().create(); LOG.info("=============== edgeLabel ================"); diff --git a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample4.java b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample4.java index 815c7c45f6..d9041cd684 100644 --- a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample4.java +++ b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample4.java @@ -40,7 +40,7 @@ public class PerfExample4 extends PerfExample3 { * product of 2nd and 3rd is total number of "person" vertices * @throws InterruptedException */ - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws Exception { PerfExample4 tester = new PerfExample4(); tester.test(args); diff --git a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExampleBase.java b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExampleBase.java index 073ca071a2..76a0ad8fff 100644 --- a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExampleBase.java +++ b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExampleBase.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CyclicBarrier; import java.util.function.Consumer; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; @@ -56,20 +57,22 @@ public abstract class PerfExampleBase { protected Set vertices = Collections.newSetFromMap( new ConcurrentHashMap()); + protected boolean profile = false; - public int test(String[] args) throws InterruptedException { - if (args.length != 3) { - System.out.println("Usage: threadCount times multiple"); + public int test(String[] args) throws Exception { + if (args.length != 4) { + System.out.println("Usage: threadCount times multiple profile"); return -1; } int threadCount = Integer.parseInt(args[0]); int times = Integer.parseInt(args[1]); int multiple = Integer.parseInt(args[2]); + this.profile = Boolean.parseBoolean(args[3]); // NOTE: this test with HugeGraph is for local, change it into // client if test with restful server from remote - HugeGraph hugegraph = ExampleUtil.loadGraph(true, true); + HugeGraph hugegraph = ExampleUtil.loadGraph(true, this.profile); GraphManager graph = new GraphManager(hugegraph); initSchema(hugegraph.schema()); @@ -101,34 +104,34 @@ public void testInsertPerf(GraphManager graph, int threadCount, int times, int multiple) - throws InterruptedException { + throws Exception { // Total vertices/edges long n = threadCount * times * multiple; long vertices = (PERSON_NUM + SOFTWARE_NUM) * n; long edges = EDGE_NUM * n; - long cost = this.execute(i -> { + long cost = this.execute(graph, i -> { this.testInsert(graph, times, multiple); - graph.close(); }, threadCount); LOG.info("Insert rate with threads: {} vertices/s & {} edges/s, " + "insert total {} vertices & {} edges, cost time: {}ms", vertices * 1000 / cost, edges * 1000 / cost, vertices, edges, cost); + + graph.clearVertexCache(); } public void testQueryVertexPerf(GraphManager graph, int threadCount, int times, int multiple) - throws InterruptedException { - long cost = this.execute(i -> { + throws Exception { + long cost = this.execute(graph, i -> { this.testQueryVertex(graph, threadCount, i, multiple); - graph.close(); }, threadCount); - final int size = (PERSON_NUM + SOFTWARE_NUM) * threadCount * times; + final long size = (PERSON_NUM + SOFTWARE_NUM) * threadCount * times; LOG.info("Query rate with threads: {} vertices/s, " + "query total vertices {}, cost time: {}ms", size * 1000 / cost, size, cost); @@ -138,42 +141,62 @@ public void testQueryEdgePerf(GraphManager graph, int threadCount, int times, int multiple) - throws InterruptedException { - long cost = this.execute(i -> { + throws Exception { + long cost = this.execute(graph, i -> { this.testQueryEdge(graph, threadCount, i, multiple); - graph.close(); }, threadCount); - final int size = (PERSON_NUM + SOFTWARE_NUM) * threadCount * times; + final long size = (PERSON_NUM + SOFTWARE_NUM) * threadCount * times; LOG.info("Query rate with threads: {} vedges/s, " + "query total vedges {}, cost time: {}ms", size * 1000 / cost, size, cost); } - protected long execute(Consumer task, int threadCount) - throws InterruptedException { + protected long execute(GraphManager graph, Consumer task, + int threadCount) throws Exception { + CyclicBarrier startBarrier = new CyclicBarrier(threadCount + 1); + CyclicBarrier endBarrier = new CyclicBarrier(threadCount + 1); List threads = new ArrayList<>(threadCount); for (int i = 0; i < threadCount; i++) { int j = i; Thread t = new Thread(() -> { + graph.initEnv(); + try { + startBarrier.await(); + } catch (Exception e) { + throw new RuntimeException(e); + } + task.accept(j); - LOG.info("option = {}", PerfUtil.instance().toECharts()); + + try { + endBarrier.await(); + } catch (Exception e) { + throw new RuntimeException(e); + } + + if (this.profile) { + LOG.info("option = {}", PerfUtil.instance().toECharts()); + } + graph.destroyEnv(); }); threads.add(t); } - long beginTime = System.currentTimeMillis(); - for (Thread t : threads) { t.start(); } + startBarrier.await(); + long beginTime = System.currentTimeMillis(); + + endBarrier.await(); + long endTime = System.currentTimeMillis(); + for (Thread t : threads) { t.join(); } - long endTime = System.currentTimeMillis(); - return endTime - beginTime; } @@ -244,6 +267,15 @@ public GraphManager(HugeGraph hugegraph) { this.hugegraph = hugegraph; } + public void initEnv() { + // Cost about 6s + this.hugegraph.graphTransaction(); + } + + public void destroyEnv() { + this.hugegraph.closeTx(); + } + public Transaction tx() { return this.hugegraph.tx(); } @@ -252,10 +284,6 @@ public GraphTraversalSource traversal() { return this.hugegraph.traversal(); } - public void close() { - this.hugegraph.close(); - } - public Vertex addVertex(Object... keyValues) { HugeVertex v = (HugeVertex) this.hugegraph.addVertex(keyValues); this.cache.update(v.id(), v.resetTx()); @@ -268,6 +296,10 @@ public Vertex getVertex(Object id) { })); } + public void clearVertexCache() { + this.cache.clear(); + } + public Vertex queryVertex(Object id) { return this.hugegraph.vertices(id).next(); } diff --git a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample1ThreadTest.java b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/ThreadRangePerfTest.java similarity index 62% rename from hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample1ThreadTest.java rename to hugegraph-example/src/main/java/com/baidu/hugegraph/example/ThreadRangePerfTest.java index 92ea000ff2..f7e76c2778 100644 --- a/hugegraph-example/src/main/java/com/baidu/hugegraph/example/PerfExample1ThreadTest.java +++ b/hugegraph-example/src/main/java/com/baidu/hugegraph/example/ThreadRangePerfTest.java @@ -21,16 +21,17 @@ import org.slf4j.Logger; +import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.util.Log; -public class PerfExample1ThreadTest { +public class ThreadRangePerfTest { - private static final Logger LOG = Log.logger(PerfExample1ThreadTest.class); + private static final Logger LOG = Log.logger(ThreadRangePerfTest.class); - public static void main(String[] args) throws InterruptedException { - if (args.length != 5) { + public static void main(String[] args) throws Exception { + if (args.length != 6) { System.out.println("Usage: minThread maxThread threadStep " + - "times multiple"); + "times multiple profile"); return; } @@ -39,19 +40,23 @@ public static void main(String[] args) throws InterruptedException { int threadStep = Integer.parseInt(args[2]); int times = Integer.parseInt(args[3]); int multiple = Integer.parseInt(args[4]); + boolean profile = Boolean.parseBoolean(args[5]); - args = new String[3]; + String[] newargs = new String[4]; for (int i = minThread; i <= maxThread; i += threadStep) { - args[0] = String.valueOf(i); - args[1] = String.valueOf(times); - args[2] = String.valueOf(multiple); + int threads = i; + newargs[0] = String.valueOf(threads); + newargs[1] = String.valueOf(times); + newargs[2] = String.valueOf(multiple); + newargs[3] = String.valueOf(profile); LOG.info("==================================="); - LOG.info("threads: {}, times: {}, multiple: {}", - i, times, multiple); - PerfExample1.main(args); + LOG.info("threads: {}, times: {}, multiple: {}, profile: {}", + threads, times, multiple, profile); + new PerfExample1().test(newargs); } - System.exit(0); + // Stop daemon thread + HugeGraph.shutdown(30L); } }