【发布时间】:2013-12-05 20:20:59
【问题描述】:
我很想知道(下面的)程序是否可以改进以更快地执行。看起来,当我们有约束时(90 秒有约束,1 秒没有),事务的 close() 会花费很多时间
以下是程序的输出:
2013-12-05 14:30:20,399 [main] INFO net.ahm.graph.Lab - ############# WAVE 1 #############
2013-12-05 14:30:20,399 [main] INFO net.ahm.graph.Lab - TIME TO BEGIN TX (m-sec): 0
2013-12-05 14:30:45,296 [main] INFO net.ahm.graph.Lab - TIME TO CREATE 10000 nodes (m-sec): 24897
2013-12-05 14:30:45,296 [main] INFO net.ahm.graph.Lab - TIME TO MARK SUCCESS (m-sec): 0
2013-12-05 14:32:20,372 [main] INFO net.ahm.graph.Lab - TIME TO COMMIT (m-sec): 95076
2013-12-05 14:32:20,372 [main] INFO net.ahm.graph.Lab - ############# WAVE 2 #############
2013-12-05 14:32:20,372 [main] INFO net.ahm.graph.Lab - TIME TO BEGIN TX (m-sec): 0
2013-12-05 14:32:47,168 [main] INFO net.ahm.graph.Lab - TIME TO CREATE 10000 nodes (m-sec): 26796
2013-12-05 14:32:47,168 [main] INFO net.ahm.graph.Lab - TIME TO MARK SUCCESS (m-sec): 0
2013-12-05 14:34:16,040 [main] INFO net.ahm.graph.Lab - TIME TO COMMIT (m-sec): 88872
2013-12-05 14:34:16,040 [main] INFO net.ahm.graph.Lab - ############# WAVE 3 #############
2013-12-05 14:34:16,040 [main] INFO net.ahm.graph.Lab - TIME TO BEGIN TX (m-sec): 0
2013-12-05 14:34:44,423 [main] INFO net.ahm.graph.Lab - TIME TO CREATE 10000 nodes (m-sec): 28383
2013-12-05 14:34:44,423 [main] INFO net.ahm.graph.Lab - TIME TO MARK SUCCESS (m-sec): 0
2013-12-05 14:36:16,964 [main] INFO net.ahm.graph.Lab - TIME TO COMMIT (m-sec): 92541
2013-12-05 14:36:16,964 [main] INFO net.ahm.graph.Lab - ############# WAVE 4 #############
2013-12-05 14:36:16,964 [main] INFO net.ahm.graph.Lab - TIME TO BEGIN TX (m-sec): 0
2013-12-05 14:36:43,376 [main] INFO net.ahm.graph.Lab - TIME TO CREATE 10000 nodes (m-sec): 26412
2013-12-05 14:36:43,376 [main] INFO net.ahm.graph.Lab - TIME TO MARK SUCCESS (m-sec): 0
2013-12-05 14:38:13,156 [main] INFO net.ahm.graph.Lab - TIME TO COMMIT (m-sec): 89780
2013-12-05 14:38:13,156 [main] INFO net.ahm.graph.Lab - ############# WAVE 5 #############
2013-12-05 14:38:13,156 [main] INFO net.ahm.graph.Lab - TIME TO BEGIN TX (m-sec): 0
2013-12-05 14:38:39,459 [main] INFO net.ahm.graph.Lab - TIME TO CREATE 10000 nodes (m-sec): 26303
2013-12-05 14:38:39,459 [main] INFO net.ahm.graph.Lab - TIME TO MARK SUCCESS (m-sec): 0
2013-12-05 14:40:10,581 [main] INFO net.ahm.graph.Lab - TIME TO COMMIT (m-sec): 91122
2013-12-05 14:40:10,582 [Thread-5] INFO net.ahm.graph.Lab - ### GRAPHDB SHUTDOWNHOOK INVOKED !!!
下面是测试程序:
import java.io.File;
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.graphdb.schema.ConstraintDefinition;
import org.neo4j.graphdb.schema.Schema;
import org.neo4j.kernel.impl.util.FileUtils;
public class Lab {
private static final Logger LOG = Logger.getLogger(Lab.class);
public static void main(String[] args) throws Exception {
FileUtils.deleteRecursively(new File("graphdb"));
GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder("graphdb")
.setConfig(GraphDatabaseSettings.use_memory_mapped_buffers, "true")
.setConfig(GraphDatabaseSettings.keep_logical_logs, "false").setConfig(GraphDatabaseSettings.query_cache_size, "1000")
.setConfig(GraphDatabaseSettings.relationship_grab_size, "1000").newGraphDatabase();
registerShutdownHook(graphDb);
try (Transaction tx = graphDb.beginTx()) {
Schema schema = graphDb.schema();
createConstraint(schema, "TEST", "COUNT");
tx.success();
}
int count = 0;
long time;
for (int i = 1; i <= 5; i++) {
LOG.info("############# WAVE " + i + " #############");
time = System.currentTimeMillis();
Transaction tx = graphDb.beginTx();
LOG.info("TIME TO BEGIN TX (m-sec): " + (System.currentTimeMillis() - time));
try {
time = System.currentTimeMillis();
for (int j = 1; j <= 10000; j++) {
count++;
Node n = graphDb.createNode(DynamicLabel.label("TEST"));
n.setProperty("COUNT", count);
}
LOG.info("TIME TO CREATE 10000 nodes (m-sec): " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
tx.success();
LOG.info("TIME TO MARK SUCCESS (m-sec): " + (System.currentTimeMillis() - time));
} finally {
time = System.currentTimeMillis();
tx.close();
LOG.info("TIME TO COMMIT (m-sec): " + (System.currentTimeMillis() - time));
}
}
}
private static void createConstraint(Schema schema, String label, String propertyName) {
Iterator<ConstraintDefinition> constraints = schema.getConstraints(DynamicLabel.label(label)).iterator();
if (constraints == null || !constraints.hasNext()) {
try {
schema.constraintFor(DynamicLabel.label(label)).assertPropertyIsUnique(propertyName).create();
} catch (org.neo4j.graphdb.ConstraintViolationException ex) {
LOG.error("CONSTRAINT ALREADY DEFINED ON: " + label);
}
}
}
private static void registerShutdownHook(final GraphDatabaseService graphDb) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
LOG.info("### GRAPHDB SHUTDOWNHOOK INVOKED !!!");
graphDb.shutdown();
}
});
}
}
我注释了对 createConstraint() 的调用,以下是程序的输出:
2013-12-05 15:34:34,686 [main] INFO net.ahm.graph.Lab - ############# WAVE 1 #############
2013-12-05 15:34:34,686 [main] INFO net.ahm.graph.Lab - TIME TO BEGIN TX (m-sec): 0
2013-12-05 15:34:35,529 [main] INFO net.ahm.graph.Lab - TIME TO CREATE 10000 nodes (m-sec): 843
2013-12-05 15:34:35,529 [main] INFO net.ahm.graph.Lab - TIME TO MARK SUCCESS (m-sec): 0
2013-12-05 15:34:37,635 [main] INFO net.ahm.graph.Lab - TIME TO COMMIT (m-sec): 2106
2013-12-05 15:34:37,635 [main] INFO net.ahm.graph.Lab - ############# WAVE 2 #############
2013-12-05 15:34:37,635 [main] INFO net.ahm.graph.Lab - TIME TO BEGIN TX (m-sec): 0
2013-12-05 15:34:37,729 [main] INFO net.ahm.graph.Lab - TIME TO CREATE 10000 nodes (m-sec): 94
2013-12-05 15:34:37,729 [main] INFO net.ahm.graph.Lab - TIME TO MARK SUCCESS (m-sec): 0
2013-12-05 15:34:38,915 [main] INFO net.ahm.graph.Lab - TIME TO COMMIT (m-sec): 1186
2013-12-05 15:34:38,915 [main] INFO net.ahm.graph.Lab - ############# WAVE 3 #############
2013-12-05 15:34:38,915 [main] INFO net.ahm.graph.Lab - TIME TO BEGIN TX (m-sec): 0
2013-12-05 15:34:38,963 [main] INFO net.ahm.graph.Lab - TIME TO CREATE 10000 nodes (m-sec): 48
2013-12-05 15:34:38,963 [main] INFO net.ahm.graph.Lab - TIME TO MARK SUCCESS (m-sec): 0
2013-12-05 15:34:39,684 [main] INFO net.ahm.graph.Lab - TIME TO COMMIT (m-sec): 721
2013-12-05 15:34:39,684 [main] INFO net.ahm.graph.Lab - ############# WAVE 4 #############
2013-12-05 15:34:39,684 [main] INFO net.ahm.graph.Lab - TIME TO BEGIN TX (m-sec): 0
2013-12-05 15:34:39,715 [main] INFO net.ahm.graph.Lab - TIME TO CREATE 10000 nodes (m-sec): 31
2013-12-05 15:34:39,715 [main] INFO net.ahm.graph.Lab - TIME TO MARK SUCCESS (m-sec): 0
2013-12-05 15:34:40,464 [main] INFO net.ahm.graph.Lab - TIME TO COMMIT (m-sec): 749
2013-12-05 15:34:40,464 [main] INFO net.ahm.graph.Lab - ############# WAVE 5 #############
2013-12-05 15:34:40,464 [main] INFO net.ahm.graph.Lab - TIME TO BEGIN TX (m-sec): 0
2013-12-05 15:34:40,527 [main] INFO net.ahm.graph.Lab - TIME TO CREATE 10000 nodes (m-sec): 63
2013-12-05 15:34:40,527 [main] INFO net.ahm.graph.Lab - TIME TO MARK SUCCESS (m-sec): 0
2013-12-05 15:34:41,416 [main] INFO net.ahm.graph.Lab - TIME TO COMMIT (m-sec): 889
2013-12-05 15:34:41,432 [Thread-4] INFO net.ahm.graph.Lab - ### GRAPHDB SHUTDOWNHOOK INVOKED !!!
【问题讨论】:
-
用一个简单的索引替换约束并在插入之前查找存在将 commit() 时间更改为一秒,同时保持插入时间几乎相同。我应该直接转储约束,还是会进一步改进?
标签: java performance neo4j