"Turn" JMeter Learning (19) JMeter test MongoDB

Source: Internet
Author: User
Tags uuid



JMeter Test MongoDB Performance in two ways, one is to use JMeter directly to test MongoDB, there is a way to write Java code to test MongoDB performance.






The first of these methods



1. Write Java code that reads as follows:


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 insert multiple data at once to test the performance of 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", "Guangdong");
            doc.put ("city", "Guangzhou");
            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. Build the jar package and place it under the Jmeter_home/lib/ext folder



3. Start JMeter, new thread Group, Java request, view result tree, aggregated report, graphical results






4. Perform the test



Check that MongoDB has added success data.






The second method of



1. Start JMeter, create a new thread group, set thread Group properties



2. Right-click Add-mongodb Source Config



Set Properties Server Address list:127.0.0.1



MongoDB Source:jmeterdbsource, as set:






3. Right-click to add-sampler-mongodb Script



Set Properties MongoDB Source: Refer to the good name set in MongoDB source Config, Jmeterdbsource



Database name: Iflashbuy-log



Script: Scripts






4. Right-click-listener-view result tree









Both of these methods can be used to test MongoDB performance.



Summary: Learning JMeter for a while, more and more realize that JMeter support for Java is too strong.






Report:



MongoDB billion-level data volume performance test http://www.cnblogs.com/lovecindywang/archive/2011/03/02/1969324.html



"Turn" JMeter Learning (19) JMeter test MongoDB


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.