The project needs to test the performance of MongoDB, see the next online many practices are tested using YCSB, so began to learn to use YCSB.
Resources:
- YCSB GitHub Address: Https://github.com/brianfrankcooper/YCSB
- Wiki:https://github.com/brianfrankcooper/ycsb/wiki
- Installation reference: Https://github.com/brianfrankcooper/YCSB/tree/master/mongodb
- Some previous Test experience: http://www.sdpnosql.net/index.php/archives/3/http://www.sdpnosql.net/index.php/archives/13/
1 installation
Install JAVA,MVN,YCSB based on a reference document (HTTPS://GITHUB.COM/BRIANFRANKCOOPER/YCSB/TREE/MASTER/MONGODB)
Python is basically installed (2.7.5 version above), Java (requires JDK, not JRE), MVN and YCSB.
1.1 YCSB directory Structure
After installing YCSB, view the YCSB directory as follows:
Drwxr-xr-x. 3 root root (Oct) 18:20 accumulodrwxr-xr-x. 3 root root (Oct) 18:20 aerospikedrwxr-xr-x. 2 root root 05:03 Oct bindrwxr-xr-x. 3 root root (Oct) 18:20 binding-parent-rw-r--r--. 1 root root 877 Oct 18:20 builddrwxr-xr-x. 3 root root (Oct) 18:20 cassandra-rw-r--r--. 1 root root 7216 Oct 18:20 checkstyle.xmldrwxr-xr-x. 4 root root Oct 03:18 coredrwxr-xr-x. 3 root root (Oct) 18:20 couchbasedrwxr-xr-x. 3 root root (Oct) 18:20 distributiondrwxr-xr-x. 4 root root 4096 Oct 18:20 docdrwxr-xr-x. 4 root root the Oct 18:20 dynamodbdrwxr-xr-x. 3 root root (Oct) 18:20 elasticsearchdrwxr-xr-x. 3 root root (Oct) 18:20 gemfiredrwxr-xr-x. 2 root root (Oct) 18:20 hbase094drwxr-xr-x. 3 root root (Oct) 18:20 hbase098drwxr-xr-x. 3 root root (Oct) 18:20 hbase10drwxr-xr-x. 3 root root (Oct) 18:20 hypertabledrwxr-xr-x. 3 root root (Oct) 18:20 infinispandrwxr-xr-x. 3 root root (Oct 10) 18:20 Jdbc-rw-r--r--. 1 root root 8082 Oct 18:20 license.txtdrwxr-xr-x. 3 root root (Oct) 18:20 mapkeeperdrwxr-xr-x. 4 root root 03:19 Oct mongodbdrwxr-xr-x. 3 root root (Oct) 18:20 nosqldb-rw-r--r--. 1 root root 479 Oct 18:20 notice.txtdrwxr-xr-x. 3 root root (Oct) 18:20 orientdb-rw-r--r--. 1 root root 5263 Oct 18:20 pom.xml-rw-r--r--. 1 root root 2033 Oct 18:20 readme.mddrwxr-xr-x. 3 root root (Oct) 18:20 redisdrwxr-xr-x. 3 root root (Oct) 18:20 tarantooldrwxr-xr-x. 3 root root (Oct) 18:20 voldemortdrwxr-xr-x. 2 root root 4096 Oct 01:09 workloads
Here are a few directories to be aware of:
bin: - 目录下有个可执行的ycsb文件,是个python脚本,是用户操作的命令行接口。ycsb主逻辑是:解析命令行、设置java环境,加载java-libs,封装成可以执行的java命令,并执行workloads: - 目录下有各种workload的模板,可以基于workload模板进行个性化修改core: - 包含ycsb里各种核心实现,比如DB的虚拟类DB.java,各个db子类都要继承该类;还有比如workload抽象类,如果我们要自定义workload实现也需要继承该类各种DB的目录: - 比如mongo,redis等,里面包含了对应测试的源码等。 - 当ycsb mvn编译后,会在对应的目录下生成target文件,ycsb会加载对应target文件中的class类
2 use
YCSB is implemented in two stages: the load phase and the transaction phase
2.1 Load Phase
This phase is mainly used to construct the test data, and the YCSB will construct the data required for the test in DB based on the parameter setting, such as:
./BIN/YCSB load Mongodb-async-s-P Workloads/workloada > OutputLoad.txt
Mongodb-async
In YCSB, there are options for different db, such as MONGO with MongoDB and Mongodb-async.
The default MongoDB represents synchronization, that is, load and run use synchronization, YCSB will call mongodb/src under the corresponding mongodbclient to implement the corresponding insert/update and other operations. If MONGODB-ASYNC,YCSB is set, the corresponding Asyncmongodbclient.java implementation under MONGODB/SRC is called
Parameter settings:
Options: -P File specify workload//Workload files -cp path Additional Java classpath Entries - Jvm-args args Additional arguments to the JVM- p key=value Override Workload Property//Some settings- s Print STA Tus to stderr//put status up to stderr -target n target ops/sec (default:unthrottled)//sec Total operations -threads N Number of client threads (DEFAULT:1)//client thread
Parameter interpretation:
-P Workload File
In the YCSB directory there are various workload, reference: Https://github.com/brianfrankcooper/YCSB/wiki/Core-Workloads, we take the example of Workloada
Basic configuration:
recordcount=1000 # Total number of operating strips operationcount=1000 # Total number of operations workload=com.yahoo.ycsb.workloads.coreworkload= Readallfields=true # When reading, do you need to read all fields readproportion=0.5 # Read scale updateproportion=0.5 # Update ratio Scanproportion=0insertproportion=0requestdistribution=zipfian
Workloada load comparison, read and update class ratio is 1:1, some of the settings parameters such as above, if we set MONGO again, also need to add the corresponding MONGO configuration workload, as follows:
MONGODB.URL=MONGODB://192.168.137.10:34001/YCSB? # MongoDB corresponding URI etc MONGODB.DATABASE=YCSB # corresponding to Dbmongodb.writeconcern=normal # write level
-P option
-P is used to set some corresponding parameters, if the parameters in the workload, you can also put on the command line as-p set
-S
-S is indicated, in the run, to print some status to stderr, general status information, used to indicate some intermediate states in the run (such as how many requests are currently processed, how many requests, etc.)
-target N
Represents the total number of operations in 1s (each thread adds up), if the performance is not satisfied, such as the maximum performance of only 100, you set 1000, then YCSB will try to approach this number. The default is no limit.
-thread Number of threads
Set the number of concurrent test threads for the YCSB client, the default is 1, single-threaded, so when testing again, be sure to set this option
2.2 Transcation Stage
At the end of 2.1load data, YCSB can be tested, which is the transaction phase. In the transaction phase, the DB pressure is measured based on the scale setting in the workload and the thread parameter settings. Specific parameters above
3 Some Custom Actions
Because this time in the use of YCSB test MongoDB is mainly to test the performance of mongodb3.0, but also need to compare with 2.6.9. The 3.0 performance write performance is greatly improved because the lock force is upgraded from DB Lock to collection lock. The default insert and update operations are for the same collection operation (usertable), which does not reflect this advantage.
So we need to modify the corresponding insert, update, and read interfaces to write multiple db at once. Modify the following:
修改mongodb底下的MongoDbClient和AsyncMongoDbClient中关于insert、update、read函数实现如下:原来的实现:
Public final int Update (final string table, final string key, final hashmap<string, byteiterator> values) { try { Final mongocollection collection = database.getcollection (table); Final Documentbuilder query = Builderfactory.start (). Add ("_id", key); Final Documentbuilder update = Builderfactory.start (); Final Documentbuilder Fieldstoset = Update.push ("$set"); For (final map.entry<string, byteiterator> Entry:values.entrySet ()) { Fieldstoset.add (Entry.getkey (), Entry.getvalue (). ToArray ()); } Final long res = collection.update (query, update, FALSE, False, Writeconcern); return res = = 1? 0:1; } catch (Final Exception e) { System.err.println (e.tostring ()); return 1;} }
修改后:
Public final int Update (final string table, final string key, final hashmap<string, byteiterator> values) {
Make changes to the original update function and do a few more operations at each update
int ret = updateonetable (table, key, values);
if (ret! = 0) {
return ret;
}
for (int i = 0; i < Table_num; ++i) {
String tableName = table + string.valueof (i);
ret = updateonetable (tableName, key, values);
if (ret! = 0) {
return ret;
}
}
return 0;
}
Public final int updateonetable (final string table, final string key, final hashmap<string, byteiterator> values) {
try {
Final Mongocollection collection = database.getcollection (table);
Final Documentbuilder query = Builderfactory.start (). Add ("_id", key);
Final Documentbuilder update = Builderfactory.start ();
Final Documentbuilder Fieldstoset = Update.push ("$set");
For (final map.entry<string, byteiterator> Entry:values.entrySet ()) {
Fieldstoset.add (Entry.getkey (), Entry.getvalue (). ToArray ());
}
Final Long res = collection.update (query, update, FALSE, False, Writeconcern);
return res = = 1? 0:1;
} catch (Final Exception e) {
System.err.println (E.tostring ()); return 1;
}
}
Where table_num can be set according to actual requirements (in practice, we set this value to 4, which means write 5 table at a time)
Test MongoDB with YCSB