人的一生一般会有七次机会。你只需要抓住其中的一两次机会,一下子就能拉开跟同龄人之间的距离,甚至改变人生方向、改变命运。
一、基础
适合MongoDB的场景
- 无需要跨文档或跨表的事务及复杂的join查询支持
- 敏捷迭代的业务,需求变动频繁,数据模型无法确定
- 如游戏玩家数据
- 存储的数据格式灵活,不固定,或属于半结构化数据
- 业务并发访问量大,需数千的QPS
- TB级以上的海量数据存储,且数据量不断增加
- 要求存储的数据持久化、不丢失
- 需要99.999%的数据高可用性
- 需要大量的地理位置查询、文本查询
- 游戏、电商、社交、视频直播、物流、监控、日志数据存储,第三方信息抓取、地图、打车、外卖等
安装MongoDB,省略
- 笔者安装目录:
/usr/local/mongodb-4.2.5
- 笔者安装目录:
运行
cd /usr/local/mongodb-4.2.5 &&./bin/mongod -f 27017.conf
27017.conf
1
2
3
4
5
6
7
8#笔者使用了复制集模式,通过开启27017、27018、27019三个端口实现
dbpath = /usr/local/mongodb-4.2.5/data/27017
logpath = /usr/local/mongodb-4.2.5/logs/27017/mongo.log
port = 27017
fork = true
logappend=true
replSet = my_replset
查看运行状态
ps -ef|grep mongo
1
501 32276 1 0 5:39下午 ?? 0:10.03 ./bin/mongod -f 27017.conf
TPCC事务:TPCC是TPC组织联合Oracle、IBM、思科等企业制定的单机事务数据库测试基准。它模拟了一个仓储物流的业务场景,包括新建订单、付款、发货、订单查询、库存查询五个事务操作。执行TPCC测试程序,将会得到被测系统的吞吐量、时延、事务完成数等信息。
二、使用
sysbench
克隆代码:
git clone https://github.com/tmcallaghan/sysbench-mongodb.git mongo-sysbench
切换目录:
cd mongo-sysbench
安装依赖
wget https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongo-java-driver/3.9.1/mongo-java-driver-3.9.1.jar
wget https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver-core/3.9.1/mongodb-driver-core-3.9.1.jar
wget https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver-legacy/3.9.1/mongodb-driver-legacy-3.9.1.jar
修改配置文件
config.bash
,注掉了用户名和密码1
2
3
4
5
6
7
8# database username on DB_NAME
#export USERNAME=<replace-with-username>
# database password to use for USERNAME
#export PASSWORD=<replace-with-password>
# name of the server to connect to
export MONGO_SERVER=127.0.0.1执行测试
./run.simple.bash
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
262021年11月27日 星期六 17时54分18秒 CST | sysbench benchmark duration = 00:00:00:30
************************************************************************
final interval(s)
************************************************************************
Thread[main,5,main] auto commit = Y
Thread[main,5,main] run seconds = 600
Thread[main,5,main] oltp range size = 100
Thread[main,5,main] oltp point selects = 10
Thread[main,5,main] oltp simple ranges = 1
Thread[main,5,main] oltp sum ranges = 1
Thread[main,5,main] oltp order ranges = 1
Thread[main,5,main] oltp distinct ranges = 1
Thread[main,5,main] oltp index updates = 1
Thread[main,5,main] oltp non index updates = 1
Thread[main,5,main] oltp inserts = 1
Thread[main,5,main] write concern = ACKNOWLEDGED
Thread[main,5,main] maximum tps (global) = 999999
Thread[main,5,main] maximum tps (per thread) = 31250
Thread[main,5,main] Server:Port = 127.0.0.1:27017
Thread[main,5,main] seed = 1638006795
Thread[main,5,main] userName =
Thread[main,5,main] read preference = secondaryPreferred
Thread[main,5,main] use SSL = false
2021年11月27日 星期六 17时54分18秒 CST | sysbench benchmark duration = 00:00:00:30可通过调整配置文件
config.bash
里的参数多次验证可自定义脚本进行测试
YCSB:cat mongodb-binding/README.md,查看使用方法
安装MongoDB并启动服务
安装Java
安装mvn
- brew install mvn
下载源码包,笔者用curl较慢,wget较快
curl -O --location https://github.com/brianfrankcooper/YCSB/releases/download/0.17.0/ycsb-0.17.0.tar.gz
wget https://github.com/brianfrankcooper/YCSB/releases/download/0.17.0/ycsb-0.17.0.tar.gz
mkdir mongo-ycsb
解压文件
tar -zxvf ycsb-0.17.0.tar.gz -C mongo-ycsb
cd mongo-ycsb/ycsb-0.17.0
./bin/ycsb load mongodb-async -s -P workloads/workloada > outputLoad.txt
1
2
3
4
5Loading workload...
Starting test.
2021-11-28 01:02:51:219 0 sec: 0 operations; est completion in 0 second
DBWrapper: report latency for each error is false and specific error codes to track for latency are: []
2021-11-28 01:02:51:356 0 sec: 0 operations; est completion in 106751991167300 days 15 hours [CLEANUP: Count=1, Max=999, Min=999, Avg=999, 90=999, 99=999, 99.9=999, 99.99=999] [INSERT: Count=0, Max=0, Min=9223372036854775807, Avg=NaN, 90=0, 99=0, 99.9=0, 99.99=0] [INSERT-FAILED: Count=1, Max=89535, Min=89472, Avg=89504, 90=89535, 99=89535, 99.9=89535, 99.99=89535]./bin/ycsb run mongodb-async -s -P workloads/workloada > outputRun.txt
1
2
3
4
5Loading workload...
Starting test.
2021-11-28 01:01:04:933 0 sec: 0 operations; est completion in 0 second
DBWrapper: report latency for each error is false and specific error codes to track for latency are: []
2021-11-28 01:01:05:477 0 sec: 1000 operations; 1838.24 current ops/sec; [READ: Count=496, Max=3859, Min=92, Avg=247.48, 90=335, 99=772, 99.9=3859, 99.99=3859] [CLEANUP: Count=1, Max=1193, Min=1193, Avg=1193, 90=1193, 99=1193, 99.9=1193, 99.99=1193] [UPDATE: Count=504, Max=106751, Min=209, Avg=669.33, 90=611, 99=1188, 99.9=9151, 99.99=106751]
mongo-perf
git clone https://github.com/mongodb/mongo-perf.git
cd mongo-perf
pip install -r requirements.txt
testcases有各种测试脚本,当然也可以根据需求自定义脚本
- python benchrun.py -f testcases/simple_insert.js -t 1 2 4
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605MongoDB shell version v4.0.11
connecting to: mongodb://localhost:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d364c43c-6a62-4576-99c8-ffb57db0e798") }
MongoDB server version: 4.2.5
WARNING: shell and server versions do not match
db version: 4.2.5
2261279b51ea13df08ae708ff278f0679c59dc32
load('util/utils.js');
load('util/docGenerators.js');
load('testcases/simple_insert.js');
mongoPerfRunTests([1, 2, 4], 1, 1, 5, 1, "%", [], 0, {"writeCmdMode": "true", "readCmdMode": "false", "writeConcern": {"j": "false"}}, false, false, false, {"traceOnly": false});
Insert.Empty
1 7269.969787553751
2 11636.676242500396
4 15145.821655075235
Insert.EmptyCapped
1 6178.765014942947
2 9150.603016602348
4 9791.203774341779
Insert.EmptyCapped.SeqIntID
1 5579.4225743621355
2 8780.241197661215
4 9466.785694439503
Insert.JustID
1 6453.887723781281
2 12039.786947622359
4 16016.953457184602
Insert.IntVector
1 717.5318343558479
2 961.691778284588
4 1115.8261508892645
Insert.SeqIntID.Indexed
1 6108.172264291902
2 9288.215849655635
4 13964.509173907272
Insert.IntIDUpsert
1 5083.715985604563
2 9069.28052532995
4 12185.743531658685
Insert.JustNum
1 6654.262064448619
2 11584.422657189818
4 16059.032638414492
Insert.JustNumIndexed
1 6211.335337140367
2 10886.082883453479
4 14292.314880578622
InsertIndexedStringsSimpleCollation
1 6285.506950441219
2 9731.579107866964
4 12697.789255975225
InsertIndexedStringsNonSimpleCollation
1 6327.584862114689
2 9951.564032732394
4 13156.144294716169
Insert.UniqueIndex
1 5571.34526527612
2 9236.483711087776
4 12684.164055158411
Insert.UniqueIndexCompound
1 5861.723296677158
2 9692.254573277096
4 12662.311170272338
Insert.UniqueIndexCompoundReverse
1 5804.876936608945
2 8765.051039724734
4 13390.150569004967
Insert.LargeDocVector
1 425.33101130996556
2 605.821724914963
4 709.6603909879481
Finished Testing.
{
"errors": [
0
],
"end": "2021-11-27T17:28:22.165Z",
"results": [
{
"name": "Insert.Empty",
"results": {
"1": {
"ops_per_sec": 7269.969787553751,
"ops_per_sec_values": [
7269.969787553751
],
"error_values": [
0
]
},
"start": "2021-11-27T17:23:37.815Z",
"2": {
"ops_per_sec": 11636.676242500396,
"ops_per_sec_values": [
11636.676242500396
],
"error_values": [
0
]
},
"end": "2021-11-27T17:23:55.410Z",
"4": {
"ops_per_sec": 15145.821655075235,
"ops_per_sec_values": [
15145.821655075235
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.EmptyCapped",
"results": {
"1": {
"ops_per_sec": 6178.765014942947,
"ops_per_sec_values": [
6178.765014942947
],
"error_values": [
0
]
},
"start": "2021-11-27T17:23:55.410Z",
"2": {
"ops_per_sec": 9150.603016602348,
"ops_per_sec_values": [
9150.603016602348
],
"error_values": [
0
]
},
"end": "2021-11-27T17:24:13.360Z",
"4": {
"ops_per_sec": 9791.203774341779,
"ops_per_sec_values": [
9791.203774341779
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.EmptyCapped.SeqIntID",
"results": {
"1": {
"ops_per_sec": 5579.4225743621355,
"ops_per_sec_values": [
5579.4225743621355
],
"error_values": [
0
]
},
"start": "2021-11-27T17:24:13.360Z",
"2": {
"ops_per_sec": 8780.241197661215,
"ops_per_sec_values": [
8780.241197661215
],
"error_values": [
0
]
},
"end": "2021-11-27T17:24:30.739Z",
"4": {
"ops_per_sec": 9466.785694439503,
"ops_per_sec_values": [
9466.785694439503
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.JustID",
"results": {
"1": {
"ops_per_sec": 6453.887723781281,
"ops_per_sec_values": [
6453.887723781281
],
"error_values": [
0
]
},
"start": "2021-11-27T17:24:30.739Z",
"2": {
"ops_per_sec": 12039.786947622359,
"ops_per_sec_values": [
12039.786947622359
],
"error_values": [
0
]
},
"end": "2021-11-27T17:24:48.510Z",
"4": {
"ops_per_sec": 16016.953457184602,
"ops_per_sec_values": [
16016.953457184602
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.IntVector",
"results": {
"1": {
"ops_per_sec": 717.5318343558479,
"ops_per_sec_values": [
717.5318343558479
],
"error_values": [
0
]
},
"start": "2021-11-27T17:24:48.510Z",
"2": {
"ops_per_sec": 961.691778284588,
"ops_per_sec_values": [
961.691778284588
],
"error_values": [
0
]
},
"end": "2021-11-27T17:25:07.409Z",
"4": {
"ops_per_sec": 1115.8261508892645,
"ops_per_sec_values": [
1115.8261508892645
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.SeqIntID.Indexed",
"results": {
"1": {
"ops_per_sec": 6108.172264291902,
"ops_per_sec_values": [
6108.172264291902
],
"error_values": [
0
]
},
"start": "2021-11-27T17:25:07.409Z",
"2": {
"ops_per_sec": 9288.215849655635,
"ops_per_sec_values": [
9288.215849655635
],
"error_values": [
0
]
},
"end": "2021-11-27T17:25:28.502Z",
"4": {
"ops_per_sec": 13964.509173907272,
"ops_per_sec_values": [
13964.509173907272
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.IntIDUpsert",
"results": {
"1": {
"ops_per_sec": 5083.715985604563,
"ops_per_sec_values": [
5083.715985604563
],
"error_values": [
0
]
},
"start": "2021-11-27T17:25:28.503Z",
"2": {
"ops_per_sec": 9069.28052532995,
"ops_per_sec_values": [
9069.28052532995
],
"error_values": [
0
]
},
"end": "2021-11-27T17:25:46.102Z",
"4": {
"ops_per_sec": 12185.743531658685,
"ops_per_sec_values": [
12185.743531658685
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.JustNum",
"results": {
"1": {
"ops_per_sec": 6654.262064448619,
"ops_per_sec_values": [
6654.262064448619
],
"error_values": [
0
]
},
"start": "2021-11-27T17:25:46.102Z",
"2": {
"ops_per_sec": 11584.422657189818,
"ops_per_sec_values": [
11584.422657189818
],
"error_values": [
0
]
},
"end": "2021-11-27T17:26:03.679Z",
"4": {
"ops_per_sec": 16059.032638414492,
"ops_per_sec_values": [
16059.032638414492
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.JustNumIndexed",
"results": {
"1": {
"ops_per_sec": 6211.335337140367,
"ops_per_sec_values": [
6211.335337140367
],
"error_values": [
0
]
},
"start": "2021-11-27T17:26:03.679Z",
"2": {
"ops_per_sec": 10886.082883453479,
"ops_per_sec_values": [
10886.082883453479
],
"error_values": [
0
]
},
"end": "2021-11-27T17:26:23.326Z",
"4": {
"ops_per_sec": 14292.314880578622,
"ops_per_sec_values": [
14292.314880578622
],
"error_values": [
0
]
}
}
},
{
"name": "InsertIndexedStringsSimpleCollation",
"results": {
"1": {
"ops_per_sec": 6285.506950441219,
"ops_per_sec_values": [
6285.506950441219
],
"error_values": [
0
]
},
"start": "2021-11-27T17:26:23.326Z",
"2": {
"ops_per_sec": 9731.579107866964,
"ops_per_sec_values": [
9731.579107866964
],
"error_values": [
0
]
},
"end": "2021-11-27T17:26:42.433Z",
"4": {
"ops_per_sec": 12697.789255975225,
"ops_per_sec_values": [
12697.789255975225
],
"error_values": [
0
]
}
}
},
{
"name": "InsertIndexedStringsNonSimpleCollation",
"results": {
"1": {
"ops_per_sec": 6327.584862114689,
"ops_per_sec_values": [
6327.584862114689
],
"error_values": [
0
]
},
"start": "2021-11-27T17:26:42.433Z",
"2": {
"ops_per_sec": 9951.564032732394,
"ops_per_sec_values": [
9951.564032732394
],
"error_values": [
0
]
},
"end": "2021-11-27T17:27:01.964Z",
"4": {
"ops_per_sec": 13156.144294716169,
"ops_per_sec_values": [
13156.144294716169
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.UniqueIndex",
"results": {
"1": {
"ops_per_sec": 5571.34526527612,
"ops_per_sec_values": [
5571.34526527612
],
"error_values": [
0
]
},
"start": "2021-11-27T17:27:01.964Z",
"2": {
"ops_per_sec": 9236.483711087776,
"ops_per_sec_values": [
9236.483711087776
],
"error_values": [
0
]
},
"end": "2021-11-27T17:27:22.438Z",
"4": {
"ops_per_sec": 12684.164055158411,
"ops_per_sec_values": [
12684.164055158411
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.UniqueIndexCompound",
"results": {
"1": {
"ops_per_sec": 5861.723296677158,
"ops_per_sec_values": [
5861.723296677158
],
"error_values": [
0
]
},
"start": "2021-11-27T17:27:22.438Z",
"2": {
"ops_per_sec": 9692.254573277096,
"ops_per_sec_values": [
9692.254573277096
],
"error_values": [
0
]
},
"end": "2021-11-27T17:27:42.656Z",
"4": {
"ops_per_sec": 12662.311170272338,
"ops_per_sec_values": [
12662.311170272338
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.UniqueIndexCompoundReverse",
"results": {
"1": {
"ops_per_sec": 5804.876936608945,
"ops_per_sec_values": [
5804.876936608945
],
"error_values": [
0
]
},
"start": "2021-11-27T17:27:42.656Z",
"2": {
"ops_per_sec": 8765.051039724734,
"ops_per_sec_values": [
8765.051039724734
],
"error_values": [
0
]
},
"end": "2021-11-27T17:28:03.126Z",
"4": {
"ops_per_sec": 13390.150569004967,
"ops_per_sec_values": [
13390.150569004967
],
"error_values": [
0
]
}
}
},
{
"name": "Insert.LargeDocVector",
"results": {
"1": {
"ops_per_sec": 425.33101130996556,
"ops_per_sec_values": [
425.33101130996556
],
"error_values": [
0
]
},
"start": "2021-11-27T17:28:03.126Z",
"2": {
"ops_per_sec": 605.821724914963,
"ops_per_sec_values": [
605.821724914963
],
"error_values": [
0
]
},
"end": "2021-11-27T17:28:22.165Z",
"4": {
"ops_per_sec": 709.6603909879481,
"ops_per_sec_values": [
709.6603909879481
],
"error_values": [
0
]
}
}
}
],
"start": "2021-11-27T17:23:37.814Z",
"storageEngine": "wiredTiger",
"basicFields": {
"crudOptions": {
"writeCmdMode": "true",
"readCmdMode": "false",
"writeConcern": {
"j": false
}
},
"commit": "2261279b51ea13df08ae708ff278f0679c59dc32",
"version": "4.2.5",
"platform": "deprecated"
}
}
py-tpcc
git clone https://github.com/apavlo/py-tpcc.git mongo-tpcc
cd mongo-tpcc/pytpcc
python tpcc.py –print-config mongodb > mongodb.config
- 报错
ImportError: No module named pymongo
- pip install pymongo
- Requirement already satisfied: pymongo in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (2.7.2)
- 多Python版本导致的
- 查看自默认安装路径
python -m site
- /Users/xxxx/Library/Python/2.7/lib/python/site-packages
- 重新安装并指定位置
pip install pymongo --target=/Users/xxxx/Library/Python/2.7/lib/python/site-packages/
- 重新运行
python tpcc.py --print-config mongodb > mongodb.config
- 报错
python tpcc.py --config=mongodb.config mongodb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1711-28-2021 02:05:22 [<module>:234] INFO : Initializing TPC-C benchmark using MongodbDriver
11-28-2021 02:05:22 [<module>:244] INFO : Loading TPC-C benchmark data using MongodbDriver
11-28-2021 02:12:01 [loadFinish:354] INFO : Finished loading tables
11-28-2021 02:12:01 [execute:056] INFO : Executing benchmark for 60 seconds
==================================================================
Data Loading Time: 398 seconds
Execution Results after 60 seconds
------------------------------------------------------------------
Executed Time (µs) Rate
DELIVERY 63 10632275.8198 5.93 txn/s
NEW_ORDER 745 23894075.6321 31.18 txn/s
ORDER_STATUS 79 1405402.66037 56.21 txn/s
PAYMENT 788 21150436.6398 37.26 txn/s
STOCK_LEVEL 70 2821108.1028 24.81 txn/s
------------------------------------------------------------------
TOTAL 1745 59903298.8548 29.13 txn/s