测试mongodb的jmeter脚本代码

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
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.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;
import com.mongodb.WriteResult;

/**
 * 
 * @author ke.chenk 2011-12-21 上午9:48:58
 * @mail	lingqi1818@gmail.com
 */

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("10.20.141.22", 28018), options);
            // m = new Mongo(new ServerAddress("10.20.159.19",28018), options);
            db = m.getDB("test");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    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("test");
        if ("w".equalsIgnoreCase(context.getParameter("rw"))) {
            long key = 1;
            BasicDBObject doc = new BasicDBObject();
            doc.put("count", 100);
            doc.put("offer_price", 150);
            doc.put("industry_name", "捕鱼,原材料,加工品,十个字以内");
            doc.put("category_id", 10);
            doc.put("offer_title", "黑猫白猫抓到老鼠就是好猫");
            doc.put("sum_payment", 100);
            doc.put("sum_product_payment", 1);
            doc.put("gmt_completed", new Date());
            doc.put("is_completed", true);
            doc.put("status", "new");
            doc.put("gmt_create", new Date());
            doc.put("gmt_modified", new Date());
            doc.put("_id", read_key.getAndIncrement());
            doc.put("offer_id", key);
            doc.put("buyer_member_id", "lingqi" + key);
            doc.put("seller_member_id", "yeling" + key);
            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();

        }
        if ("r".equalsIgnoreCase(context.getParameter("rw"))) {
            BasicDBObject query = new BasicDBObject();
            query.put("_id", read_key.getAndIncrement());
            DBCursor cur = coll.find(query);
            System.out.println(cur.count());
        }
        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 < 50; i++) {
            new Thread() {

                public void run() {
                    TestMongodb t = new TestMongodb();
                    for (int j = 0; j < 10000; j++) {
                        t.runTest(c);
                    }
                }
            }.start();
        }
        // DBCollection coll = db.getCollection("test");
        // System.out.println(db.getLastError());
        // BasicDBObject in = new BasicDBObject();
        // BasicDBList values = new BasicDBList();
        // values.add(1);
        // values.add(2);
        // values.add(3);
        // in.put("$in", values);
        // BasicDBObject query = new BasicDBObject();
        // query.put("_id", in);
        // System.out.println(query);
        // //System.out.println(coll.find(query).hasNext());
        // System.out.println(coll.count());
        // System.out.println(coll.findOne());

        /*
         * String map = "function() { emit(this.offer_price, {count:1});}"; String reduce = "function(key, values) {";
         * reduce = reduce + "var total = 0;"; reduce = reduce +
         * "for(var i=0;i<values.length;i++){total += values[i].count;}"; reduce = reduce + "return {count:total};}";
         * String result = "resultCollection"; MapReduceOutput mapReduceOutput = coll.mapReduce(map, reduce.toString(),
         * result, null); DBCollection resultColl = mapReduceOutput.getOutputCollection(); DBCursor cursor =
         * resultColl.find(); while (cursor.hasNext()) { System.out.println(cursor.next()); }
         */
        // DBCursor cursor = coll.find();
        // long start = System.currentTimeMillis();
        // while (cursor.hasNext()) {
        // System.out.println(cursor.next());
        // }
        // System.out.println(System.currentTimeMillis()-start);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值