Skip to content

Commit

Permalink
fix bug: perf example cost time should exclude time of env-init
Browse files Browse the repository at this point in the history
Change-Id: Iba9964542dc43c8c4d4feb0bb6a2883309e9326a
  • Loading branch information
javeme authored and zhoney committed Jan 11, 2019
1 parent 28217e9 commit 1885132
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 ================");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,20 +57,22 @@ public abstract class PerfExampleBase {

protected Set<Object> vertices = Collections.newSetFromMap(
new ConcurrentHashMap<Object, Boolean>());
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());
Expand Down Expand Up @@ -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);
Expand All @@ -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<Integer> task, int threadCount)
throws InterruptedException {
protected long execute(GraphManager graph, Consumer<Integer> task,
int threadCount) throws Exception {
CyclicBarrier startBarrier = new CyclicBarrier(threadCount + 1);
CyclicBarrier endBarrier = new CyclicBarrier(threadCount + 1);
List<Thread> 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;
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -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());
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
}

0 comments on commit 1885132

Please sign in to comment.