Skip to content

Commit b642b5a

Browse files
authored
perf(example): add PerfExample5 and PerfExample6 (#2860)
1 parent 3d8be7f commit b642b5a

File tree

6 files changed

+211
-27
lines changed

6 files changed

+211
-27
lines changed

hugegraph-server/hugegraph-example/src/main/java/org/apache/hugegraph/example/PerfExample1.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import org.apache.hugegraph.HugeFactory;
2525
import org.apache.hugegraph.backend.BackendException;
2626
import org.apache.hugegraph.schema.SchemaManager;
27+
import org.apache.hugegraph.util.Log;
2728
import org.apache.tinkerpop.gremlin.structure.T;
2829
import org.apache.tinkerpop.gremlin.structure.Vertex;
30+
import org.slf4j.Logger;
2931

3032
import com.datastax.driver.core.exceptions.NoHostAvailableException;
3133

@@ -34,6 +36,8 @@
3436
*/
3537
public class PerfExample1 extends PerfExampleBase {
3638

39+
private static final Logger LOG = Log.logger(PerfExample1.class);
40+
3741
public static void main(String[] args) throws Exception {
3842
PerfExample1 tester = new PerfExample1();
3943
tester.test(args);

hugegraph-server/hugegraph-example/src/main/java/org/apache/hugegraph/example/PerfExample2.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import org.apache.hugegraph.HugeFactory;
2525
import org.apache.hugegraph.backend.BackendException;
2626
import org.apache.hugegraph.schema.SchemaManager;
27+
import org.apache.hugegraph.util.Log;
2728
import org.apache.tinkerpop.gremlin.structure.T;
2829
import org.apache.tinkerpop.gremlin.structure.Vertex;
30+
import org.slf4j.Logger;
2931

3032
import com.datastax.driver.core.exceptions.NoHostAvailableException;
3133

@@ -34,6 +36,8 @@
3436
*/
3537
public class PerfExample2 extends PerfExampleBase {
3638

39+
private static final Logger LOG = Log.logger(PerfExample2.class);
40+
3741
public static void main(String[] args) throws Exception {
3842
PerfExample2 tester = new PerfExample2();
3943
tester.test(args);

hugegraph-server/hugegraph-example/src/main/java/org/apache/hugegraph/example/PerfExample4.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
public class PerfExample4 extends PerfExample3 {
3131

32-
private static final Logger LOG = Log.logger(PerfExample3.class);
32+
private static final Logger LOG = Log.logger(PerfExample4.class);
3333

3434
/**
3535
* Main method
@@ -79,9 +79,4 @@ protected void testQueryVertex(GraphManager graph,
7979
LOG.info(">>>> query by range index, cost: {}ms", elapsed(current));
8080
}
8181
}
82-
83-
protected static long elapsed(long start) {
84-
long current = System.currentTimeMillis();
85-
return current - start;
86-
}
8782
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to You under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*/
17+
18+
package org.apache.hugegraph.example;
19+
20+
import java.util.Iterator;
21+
22+
import org.apache.hugegraph.HugeFactory;
23+
import org.apache.hugegraph.type.define.Directions;
24+
import org.apache.hugegraph.util.Log;
25+
import org.apache.tinkerpop.gremlin.structure.Edge;
26+
import org.slf4j.Logger;
27+
28+
/**
29+
* Perf test for: query vertices/adj-edges by ids
30+
*/
31+
public class PerfExample5 extends PerfExample3 {
32+
33+
private static final Logger LOG = Log.logger(PerfExample5.class);
34+
35+
/**
36+
* Main method
37+
* @param args 3 arguments, 1st should be 1, meaning single thread,
38+
* product of 2nd and 3rd is total number of "person" vertices
39+
* @throws InterruptedException
40+
*/
41+
public static void main(String[] args) throws Exception {
42+
PerfExample5 tester = new PerfExample5();
43+
tester.test(args);
44+
45+
// Stop daemon thread
46+
HugeFactory.shutdown(30L);
47+
}
48+
49+
@Override
50+
protected void testQueryVertex(GraphManager graph,
51+
int threads,
52+
int thread,
53+
int multiple) {
54+
int totalV = 0;
55+
long start = System.currentTimeMillis();
56+
for (int i = 0; i < multiple; i++) {
57+
int j = 0;
58+
for (Object id : this.vertices) {
59+
if (j++ % threads != thread) {
60+
continue;
61+
}
62+
graph.queryVertex(id);
63+
totalV++;
64+
}
65+
}
66+
long cost = elapsed(start);
67+
LOG.info("Query {} vertices with thread({}): {} vertices/s",
68+
totalV, thread, totalV * 1000 / cost);
69+
}
70+
71+
@Override
72+
protected void testQueryEdge(GraphManager graph,
73+
int threads,
74+
int thread,
75+
int multiple) {
76+
int totalV = 0;
77+
int totalE = 0;
78+
long start = System.currentTimeMillis();
79+
80+
for (int i = 0; i < multiple; i++) {
81+
int j = 0;
82+
for (Object id : this.vertices) {
83+
if (j++ % threads != thread) {
84+
continue;
85+
}
86+
87+
Iterator<Edge> edges = graph.queryVertexEdge(id, Directions.OUT);
88+
while (edges.hasNext()) {
89+
edges.next();
90+
totalE++;
91+
}
92+
totalV++;
93+
}
94+
}
95+
long cost = elapsed(start);
96+
LOG.info("Query {} edges of vertices({}) with thread({}): " +
97+
"{} vertices/s, {} edges/s",
98+
totalE, totalV, thread,
99+
totalV * 1000 / cost, totalE * 1000 / cost);
100+
}
101+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to You under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*/
17+
18+
package org.apache.hugegraph.example;
19+
20+
import org.apache.hugegraph.HugeFactory;
21+
import org.apache.hugegraph.util.Log;
22+
import org.apache.tinkerpop.gremlin.structure.T;
23+
import org.apache.tinkerpop.gremlin.structure.Vertex;
24+
import org.slf4j.Logger;
25+
26+
/**
27+
* Perf test for: query vertices/adj-edges by ids with edges insert
28+
*/
29+
public class PerfExample6 extends PerfExample5 {
30+
31+
private static final Logger LOG = Log.logger(PerfExample6.class);
32+
33+
/**
34+
* Main method
35+
* @param args 3 arguments, 1st should be 1, meaning single thread,
36+
* product of 2nd and 3rd is total number of "person" vertices
37+
* @throws InterruptedException
38+
*/
39+
public static void main(String[] args) throws Exception {
40+
PerfExample6 tester = new PerfExample6();
41+
tester.test(args);
42+
43+
// Stop daemon thread
44+
HugeFactory.shutdown(30L);
45+
}
46+
47+
@Override
48+
protected void testInsert(GraphManager graph, int times, int multiple) {
49+
final int TIMES = times * multiple;
50+
final int BATCH = 100;
51+
long total = 0;
52+
Vertex lastV = null;
53+
// Insert in order
54+
for (int i = 0; i < TIMES; i++) {
55+
for (int j = 0; j < BATCH; j++) {
56+
String name = String.format("p-%08d", total++);
57+
Vertex v = graph.addVertex(T.label, "person", "name", name,
58+
"city", "Hongkong", "age", 3);
59+
this.vertices.add(v.id());
60+
61+
if (lastV != null) {
62+
v.addEdge("knows", lastV);
63+
}
64+
lastV = v;
65+
}
66+
graph.tx().commit();
67+
}
68+
LOG.info("Insert {} vertices and {} edges", total, total - 1L);
69+
}
70+
}

hugegraph-server/hugegraph-example/src/main/java/org/apache/hugegraph/example/PerfExampleBase.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public abstract class PerfExampleBase {
5151
public static final int SOFTWARE_NUM = 30;
5252
public static final int EDGE_NUM = 100;
5353

54-
protected static final Logger LOG = Log.logger(PerfExampleBase.class);
54+
private static final Logger LOG = Log.logger(PerfExampleBase.class);
5555

5656
protected Set<Object> vertices = Collections.newSetFromMap(new ConcurrentHashMap<>());
5757
protected boolean profile = false;
@@ -66,6 +66,8 @@ public int test(String[] args) throws Exception {
6666
int times = Integer.parseInt(args[1]);
6767
int multiple = Integer.parseInt(args[2]);
6868
this.profile = Boolean.parseBoolean(args[3]);
69+
System.out.printf("Run: threads=%s times=%s multiple=%s profile=%s\n",
70+
threadCount, times, multiple, this.profile);
6971

7072
// NOTE: this test with HugeGraph is for local,
7173
// change it into a client if test with restful server from remote
@@ -118,7 +120,7 @@ public void testQueryVertexPerf(GraphManager graph, int threadCount, int times,
118120
this.testQueryVertex(graph, threadCount, i, multiple);
119121
}, threadCount);
120122

121-
final long size = (long) (PERSON_NUM + SOFTWARE_NUM) * threadCount * times;
123+
final long size = this.vertices.size();
122124
LOG.info("Query rate with threads: {} vertices/s, " +
123125
"query total vertices {}, cost time: {}ms",
124126
size * 1000 / cost, size, cost);
@@ -130,7 +132,7 @@ public void testQueryEdgePerf(GraphManager graph, int threadCount, int times, in
130132
this.testQueryEdge(graph, threadCount, i, multiple);
131133
}, threadCount);
132134

133-
final long size = (long) (PERSON_NUM + SOFTWARE_NUM) * threadCount * times;
135+
final long size = this.vertices.size();
134136
LOG.info("Query rate with threads: {} vedges/s, " +
135137
"query total vedges {}, cost time: {}ms",
136138
size * 1000 / cost, size, cost);
@@ -188,26 +190,29 @@ protected long execute(GraphManager graph, Consumer<Integer> task,
188190

189191
protected abstract void testInsert(GraphManager graph, int times, int multiple);
190192

191-
protected void testQueryVertex(GraphManager graph, int threads, int thread, int multiple) {
192-
int i = 0;
193-
int j = 0;
194-
int total = 0;
195-
for (Object id : this.vertices) {
196-
if (i++ % multiple != 0) {
197-
continue;
198-
}
199-
if (j++ % threads != thread) {
200-
continue;
201-
}
202-
203-
LOG.debug("Query vertex {}: {}", i, id);
204-
Vertex vertex = graph.queryVertex(id);
205-
if (!vertex.id().equals(id)) {
206-
LOG.warn("Query vertex by id {} returned {}", id, vertex);
193+
protected void testQueryVertex(GraphManager graph,
194+
int threads,
195+
int thread,
196+
int multiple) {
197+
int totalV = 0;
198+
long start = System.currentTimeMillis();
199+
for (int i = 0; i < multiple; i++) {
200+
int j = 0;
201+
for (Object id : this.vertices) {
202+
if (j++ % threads != thread) {
203+
continue;
204+
}
205+
LOG.debug("Query vertex {}: {}", i, id);
206+
Vertex vertex = graph.queryVertex(id);
207+
if (!vertex.id().equals(id)) {
208+
LOG.warn("Query vertex by id {} returned {}", id, vertex);
209+
}
210+
totalV++;
207211
}
208-
total++;
209212
}
210-
LOG.debug("Query vertices with thread({}): {}", thread, total);
213+
long cost = elapsed(start);
214+
LOG.info("Query {} vertices with thread({}): {} vertices/s",
215+
totalV, thread, totalV * 1000 / cost);
211216
}
212217

213218
protected void testQueryEdge(GraphManager graph, int threads, int thread, int multiple) {
@@ -235,6 +240,11 @@ protected void testQueryEdge(GraphManager graph, int threads, int thread, int mu
235240
totalV, thread, totalE);
236241
}
237242

243+
protected static long elapsed(long start) {
244+
long current = System.currentTimeMillis();
245+
return current - start;
246+
}
247+
238248
protected static class GraphManager {
239249

240250
private final HugeGraph hugegraph;

0 commit comments

Comments
 (0)