mongo 单机性能测试

来源:互联网 发布:fire 7 tablet 知乎 编辑:程序博客网 时间:2024/05/16 12:34

各位小伙伴,今天我们来用java压一下单节点,同机器的mongo。

2.7 GHz Intel Core i5

8 GB 1867 MHz DDR3

核总数:2

import com.mongodb.MongoClient;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoDatabase;import org.bson.Document;/** * Created by tarner on 17/9/19. */public class Application {    public static void main(String[] args) throws Exception {        MongoClient mongoClient = new MongoClient("127.0.0.1");        MongoDatabase test = mongoClient.getDatabase("test");        MongoCollection<Document> test1 = test.getCollection("test1");        long beginTime = System.currentTimeMillis();        //写100万数据        //test1//        int i = 0;//        while (i < 1000000) {//            test1.insertOne(new Document("a" + i, "va" + i));//            i++;//        }        //qps=10,416        //test2//        int j = 0;//        int i = 0;//        while (i < 1000) {////            ArrayList<Document> list = new ArrayList<>(10000);////            for (int k = 0; k < 10000; k++) {//                list.add(new Document("a" + j, "va" + j));//                j++;////            }//            i++;////            test1.insertMany(list);//        }//        qps = 111,111        //test3//        CompletableFuture<Void> v1 = CompletableFuture.runAsync(() -> {//            int j = 0;//            int i = 0;//            while (i < 500) {////                ArrayList<Document> list = new ArrayList<>();////                for (int k = 0; k < 1000; k++) {//                    list.add(new Document("a" + j, "va" + j));//                    j++;////                }//                i++;////                test1.insertMany(list);//            }//        });////        CompletableFuture<Void> v2 = CompletableFuture.runAsync(() -> {//            int j = 0;//            int i = 500;//            while (i < 1000) {////                ArrayList<Document> list = new ArrayList<>();////                for (int k = 0; k < 1000; k++) {//                    list.add(new Document("a" + j, "va" + j));//                    j++;////                }//                i++;////                test1.insertMany(list);//            }//        });//////////        v1.join();//        v2.join();        //qps=156,250        long endTime = System.currentTimeMillis();        long userTime = endTime - beginTime;        System.out.println(userTime);        mongoClient.close();    }}

在这里,我一共进行了三次试验,都是写入1,000,000条数据。只计算写入mongo的耗时。

第一次试验,是每次写1条数据,写1000000,从结果来看qps=10416不是很高,个人觉得这里因为是同步的,如果多建立一些线程,会有提高,不过因为线程切换,估计也好不到哪里去。

第二次试验,每次写1000条,写1000次。从结果qps=111,111。已经快了十倍。


第三次试验,两个线程,每次1000条,每个线程500次。从结果来看qps=156,250。又提示了不少。




原创粉丝点击