neo4j测试代码

业务需求测试neo4j在服务器的集群性能,写这个测试用例,使用多线程进行测试

package cn.ideal.neo4j.performance.test;

import org.neo4j.driver.v1.*;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import static org.neo4j.driver.v1.Values.parameters;

public class Neo4jPerformanceTest {

    final static SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Set dataSet = new HashSet();
    ConcurrentLinkedQueue queue;

    static AtomicInteger scCount=new AtomicInteger(0); //成功的

    static AtomicInteger flCount=new AtomicInteger(0); // 插入失败的

    ConcurrentLinkedQueue errQ = new ConcurrentLinkedQueue();
    int dataCount =10000000;
    int threadCount = 10;
分池
    {
        int neo4jCodeCount = 10;
        for (int i = 0; i < dataCount ; i++) {
            String adName = "ad" + (i % neo4jCodeCount + 1);
            // System.out.println(adName);
            dataSet.add(new DataModel(adName, "ssp"+(i % 3 + 1), ft.format(new Date()), "pv", i));
        }
        queue = new ConcurrentLinkedQueue(dataSet);
    }

    private final Driver driver;

    public Neo4jPerformanceTest(String uri, String user, String password) {
        driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
    }
    public void close() throws Exception {
        driver.close();
    }
//创建节点调用neo4j
    public void insertNode(String taslName) {

        try {
            Session session = driver.session();

            StatementResult statementResult = session.run("CREATE (a:Person {name: {name}, title: {title}})", parameters("name", taslName, "title", "King"));
            System.out.println(statementResult);
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @SuppressWarnings("unused")
    public static void main(String[] args) throws Exception {
        Neo4jPerformanceTest performanceTest = new Neo4jPerformanceTest("bolt://192.168.30.69:7687", "neo4j", "123456");
        System.out.println(performanceTest.dataSet.size());
//
        FileWriter writer = new FileWriter("createtime.txt",true);
        BufferedWriter bw = new BufferedWriter(writer);
        bw.newLine();
        bw.write("开始创建时间:"+ft.format(new Date()));
        bw.newLine();
        performanceTest.invokeNeo4j(bw,performanceTest.threadCount,performanceTest);
        bw.close();
        writer.close();
        performanceTest.close();
    }

//线程
    class InvokeTask implements Runnable {

        String taskName;
        Neo4jPerformanceTest neo4jPerformanceTest;

        public InvokeTask(String taskName,Neo4jPerformanceTest neo4jPerformanceTest) {
            super();
            this.taskName = taskName;
            this.neo4jPerformanceTest=neo4jPerformanceTest;
        }

        public void run() {
            {
                while (!queue.isEmpty()) {
                    System.out.println("task: "+ taskName + " 余下数据量:"+queue.size());
                    DataModel data = queue.poll();
                    if (data == null) {
                        break;
                    }
                    try {
//                        invoke(taskName, data);
                        neo4jPerformanceTestLixuejun.insertNode(taskName);
                        scCount.addAndGet(1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }
//调用neo4j
    private void invokeNeo4j(BufferedWriter bw, int threadCount,Neo4jPerformanceTest performanceTest) throws IOException {

        errQ.clear();

        ExecutorService executor = (ExecutorService) Executors.newFixedThreadPool(threadCount);

        long starttime = System.currentTimeMillis();
        try {
            for (int i = 0; i < threadCount; i++) {
                executor.submit(new InvokeTask("task_" + i,performanceTest));
            }

            if (executor != null) {
                executor.shutdown();
                while (true) {
                    if (executor.isTerminated()) {
                        break;
                    }
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            long endtime = System.currentTimeMillis();
            long ss = endtime - starttime;
            bw.write(threadCount + " 个线程创建 " + dataSet.size() + " 条数据耗时:" + ss+"。"+"创建成功:"+scCount+" 条 ; 失败:"+flCount+" 条");
            bw.newLine();

            if (!errQ.isEmpty()) {
                FileWriter writer = new FileWriter("errData.txt",true);
                BufferedWriter bw1 = new BufferedWriter(writer);
                bw1.write(threadCount + " 个线程创建 " + dataSet.size() + " 条数据耗时:" + ss );
                bw1.newLine();
                while (!errQ.isEmpty()) {
                    String data = errQ.poll();
                    if (data == null) {
                        break;
                    }
                    bw1.write(data);
                    bw1.newLine();
                }
                bw1.close();
                writer.close();
            }

        }
    }
}

你可能感兴趣的:(知识图谱)