JMeter测试MongoDB性能有两种方式,一种是利用JMeter直接进行测试MongoDB,还有一种是写Java代码方式测试MongoDB性能。
第一种方法
1.编写Java代码,内容如下:
packagecom.test.mongodb;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.UUID;importjava.util.concurrent.atomic.AtomicLong;importorg.apache.jmeter.config.Arguments;importorg.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;importorg.apache.jmeter.protocol.java.sampler.JavaSamplerContext;importorg.apache.jmeter.samplers.SampleResult;importcom.mongodb.BasicDBObject;importcom.mongodb.DB;importcom.mongodb.DBCollection;importcom.mongodb.Mongo;importcom.mongodb.MongoOptions;importcom.mongodb.ServerAddress;importcom.mongodb.WriteResult;/***
*@author2014-10-14
* @remark 一次性插入多条数据测试MongoDB的性能*/publicclassTestMongodbextendsAbstractJavaSamplerClient {privatestaticMongo m;privatestaticDB db;privatestaticAtomicLong read_key =newAtomicLong(0);privatestaticFileOutputStream fos;static{try{
fos=newFileOutputStream(newFile("jmeter_error.log"));
MongoOptions options=newMongoOptions();
options.autoConnectRetry=true;
options.connectionsPerHost= 1000;
options.maxWaitTime= 5000;
options.socketTimeout= 0;
options.connectTimeout= 15000;
options.threadsAllowedToBlockForConnectionMultiplier= 5000;
m=newMongo(newServerAddress("127.0.0.1", 27017), options);
db= m.getDB("iflashbuy-log");
}catch(Exception e) {thrownewRuntimeException(e);
}
}publicArguments getDefaultParameters() {
Arguments params=newArguments();
params.addArgument("rw", "w");returnparams;
}publicSampleResult runTest(JavaSamplerContext context) {//System.out.println(read_key.getAndIncrement());SampleResult results =newSampleResult();//System.out.println(context.getParameter("rw"));results.sampleStart();
DBCollection coll= db.getCollection("area");if("w".equalsIgnoreCase(context.getParameter("rw"))) {longkey = 1;
BasicDBObject doc=newBasicDBObject();
doc.put("_id", UUID.randomUUID());
doc.put("_class", "com.iflashbuy.bi.source.pojo.AreaDataEntity");
doc.put("province", "广东");
doc.put("city", "广州");
doc.put("pv", 35522924);
doc.put("uv", 52556);
doc.put("orderCount", 963);
doc.put("orderTotal", 1548563);
doc.put("entpriseCode", "00540001");
db.requestStart();
WriteResult rs=coll.insert(doc);try{if(rs.getError() !=null) fos.write(rs.getError().getBytes());
}catch(IOException e) {//TODO Auto-generated catch blocke.printStackTrace();
}//System.out.println("w error->" + rs.getError());db.requestDone();
}
results.setSuccessful(true);
results.sampleEnd();returnresults;//return null;}/*public static void main(String args[]) throws Exception {
Arguments a = new Arguments();
a.addArgument("rw", "w");
final JavaSamplerContext c = new JavaSamplerContext(a);
long start = System.currentTimeMillis();
for (int i = 0; i < 5; i++) {
new Thread() {
public void run() {
TestMongodb t = new TestMongodb();
for (int j = 0; j < 2; j++) {
t.runTest(c);
}
}
}.start();
}
}*/}
2.生成jar包,放在Jmeter_HOME/lib/ext文件夹下
3.启动JMeter,新建线程组,Java请求,察看结果树,聚合报告,图形结果
4.执行测试
检查MongoDB已添加成功数据。
第二种方法
1.启动JMeter,新建线程组,设置线程组属性
2.右键添加-MongoDB Source Config
设置属性Server Address List:127.0.0.1
MongoDB Source:jmeterdbsource,如下图设置:
3.右键添加-Sampler-MongoDB Script
设置属性MongoDB Source:引用MongoDB source config中设置的好的名称,jmeterdbsource
Database Name:数据库名称,iflashbuy-log
Script:脚本
4.右键-监听器-察看结果树
以上两种方式,都可用于测试MongoDB的性能。
小结:学习Jmeter有一阵子了,越来越体会到Jmeter对于Java的支持太强大了.
附:
Mongodb亿级数据量的性能测试 http://www.cnblogs.com/lovecindywang/archive/2011/03/02/1969324.html