mongodbのjmeterスクリプトコードのテスト

6951 ワード

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	[email protected]
 */

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);

    }

}