MongoBenckMarkTest.java
3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.glsx.mongo;
import com.glsx.connection.MysqlConn;
import com.glsx.util.DataUtils;
import com.glsx.util.DateUtils;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
/**
* 基准测试 测试mysql1000万行时,JSON查询性能
*/
public class MongoBenckMarkTest {
public static void main(String[] args) {
long bgnTime = System.currentTimeMillis();
MongoClient mongoClient = new MongoClient("192.168.3.221" , 27017);
MongoDatabase database = mongoClient.getDatabase("glsx_scrm_label");
MongoCollection<Document> collection = database.getCollection("user_tag");
// Document document = new Document("title", "MongoDB").
// append("description", "database").
// append("likes", 100).
// append("by", "Fly");
List<Document> documents = new ArrayList<Document>();
Long startOneId = 100000000000000L;
Random random = new Random();
for (int i = 0; i < 10000000; i++) { // 构建1000w基础数据
Document document = new Document("one_id", Long.toString(startOneId+i)).
append("merchant_id", 400000+random.nextInt(1200));
Document document1 = getRandomDocumentJson(document, random);
documents.add(document1);
if (i % 5999 == 0) {
collection.insertMany(documents);
documents = new ArrayList<>();
}
if (i % 99999 == 0) System.out.println("sumCount=" + i);
}
if (documents.size() > 0 ) {
collection.insertMany(documents);
}
System.out.println("The job total cost " + (System.currentTimeMillis()-bgnTime)/1000 + "s");
}
private static Document getRandomDocumentJson(Document document, Random random) {
// Document document = new Document();
String kvs = "";
int tagCount = random.nextInt(30)+1;
int staffCount = random.nextInt(2) + 1;
for (int index = 0; index < tagCount; index++) {
Document staffDocument = new Document();
for (int i = 0; i < staffCount; i++) {
if (staffCount == 1 && random.nextInt(4) % 2 == 0){
staffDocument.append("type", 0);
String staffId = "staff_system";
staffDocument.append(staffId,
new Document("value", random.nextInt(2400)+1).append("update_time", DataUtils.getToday()));
}
else{
staffDocument.append("type", 1);
String staffId = "staff_" + 1000 + i;
staffDocument.append(staffId,
new Document("value", random.nextInt(2400)+1).append("update_time", DataUtils.getToday()));
}
}
return document.append("g"+random.nextInt(1200)+1, staffDocument);
}
return document;
}
private static String getRandomJson(Random random) {
String kvs = "";
int tagCount = random.nextInt(30)+1;
for (int index = 0; index < tagCount; index++) {
kvs += "\"g" + random.nextInt(1200)+1 + "\":\"tag_" + random.nextInt(2400)+1 + "\",";
}
return "{" + kvs.substring(0,kvs.length()-1) + "}";
}
}