日期:2014-05-16  浏览次数:20560 次

MongoDB性能测试详细方案(单机)

?mongoDB官方网站下载:

1、mongodb-linux-i686-2.2.1.tgz

2、java驱动 mongo-2.2.jar

?

测试服务器为vmware上的Redhat 5.4 虚拟机,分配的资源为:

1、双CPU,2.13GHz

2、内存3G

3、SATA硬盘50G

?

打压工具:Webbench1.5

监控工具:mongostat

?

测试思路:

1、因为MongoDB内置了连接池,所以客户端程序相对简单,只需从一个Mongo的单例获取连接即可;

2、每个请求做1000次插入;

?

测试代码MongoDBManager:

public class MongoDBManager {
	static private MongoDBManager instance; // 唯一实例
	//	public static final String DB_NAME = "lab";
	//  public static final String MESSAGE_COLLECTION = "email";   

	static synchronized public  MongoDBManager getInstance(final String ip, int port, int poolSize) throws UnknownHostException {
		if (instance == null) {
			instance = new MongoDBManager(ip,port,poolSize);
		}		
		return instance;
	}

	private MongoDBManager() {
	}
	
	private MongoDBManager(final String ip, int port, int poolSize) throws UnknownHostException {
		init(ip,port,poolSize);
	}

	public DB getDB(String dbname) {
		return mongo.getDB(dbname);
	}

	private Mongo mongo;

	public void init(final String ip, int port, int poolSize)
			throws java.net.UnknownHostException {
		System.setProperty("MONGO.POOLSIZE", String.valueOf(poolSize));
		if (mongo == null) {
			MongoOptions options = new MongoOptions();
			options.autoConnectRetry = true;
			options.connectionsPerHost = poolSize;

			ServerAddress serverAddress = new ServerAddress(ip, port);
			mongo = new Mongo(serverAddress, options);
		}
	}
}

?

?测试代码InsertMongodbServlet:

public class InsertMongodbServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Integer insertNum = Integer.valueOf(req.getParameter("insertNum")
				.toString());
		resp.setContentType("text/html;charset=UTF-8");
		resp.setHeader("Cache-Control", "no-cache");

		DB db = MongoDBManager.getInstance("127.0.0.1", 27017, 500)
				.getDB("lab");

		DBCollection tests = db.getCollection("tests");
		for (int i = 0; i < insertNum; i++) {
			DBObject test = new BasicDBObject();
			test.put("id", 10000);
			test.put("name", "This is for mongodb insert.");			
			tests.insert(test);
		}

		resp.getWriter().write(" Insert mongodb successed!");
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	}
}

?

开始压力测试,开1000个线程,共插入100万条记录:?

/usr/local/bin/webbench -c 1000 -t 10 http://192.168.175.130:8080/labWeb/insertMongodb.do?insertNum=1000

?

监控:bin/mongostat

?

可以看到插入的QPS在7400左右。