標籤:style blog http color io os ar java for
JMeter測試MongoDB效能有兩種方式,一種是利用JMeter直接進行測試MongoDB,還有一種是寫Java代碼方式測試MongoDB效能。
第一種方法
1.編寫Java代碼,內容如下:
package com.test.mongodb;import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.UUID;import java.util.concurrent.atomic.AtomicLong; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.Mongo; import com.mongodb.MongoOptions; import com.mongodb.ServerAddress; import com.mongodb.WriteResult; /** * * @author 2014-10-14 * @remark 一次性插入多條資料測試MongoDB的效能 */ public class TestMongodb extends AbstractJavaSamplerClient { private static Mongo m; private static DB db; private static AtomicLong read_key = new AtomicLong(0); private static FileOutputStream fos; static { try { fos = new FileOutputStream(new File("jmeter_error.log")); MongoOptions options = new MongoOptions(); options.autoConnectRetry = true; options.connectionsPerHost = 1000; options.maxWaitTime = 5000; options.socketTimeout = 0; options.connectTimeout = 15000; options.threadsAllowedToBlockForConnectionMultiplier = 5000; m = new Mongo(new ServerAddress("127.0.0.1", 27017), options); db = m.getDB("iflashbuy-log"); } catch (Exception e) { throw new RuntimeException(e); } } public Arguments getDefaultParameters() { Arguments params = new Arguments(); params.addArgument("rw", "w"); return params; } public SampleResult runTest(JavaSamplerContext context) { // System.out.println(read_key.getAndIncrement()); SampleResult results = new SampleResult(); //System.out.println(context.getParameter("rw")); results.sampleStart(); DBCollection coll = db.getCollection("area"); if ("w".equalsIgnoreCase(context.getParameter("rw"))) { long key = 1; BasicDBObject doc = new BasicDBObject(); 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 block e.printStackTrace(); } //System.out.println("w error->" + rs.getError()); db.requestDone(); } results.setSuccessful(true); results.sampleEnd(); return results; // 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
JMeter學習(十九)JMeter測試MongoDB