mongoDB官方网站下载:
1、mongodb-linux-i686-2.2.1.tgz
2、java驱动 mongo-2.2.jar
测试服务器为Dell E5410 的Debian linux 2.6,配置为:
1、4核,2.33GHz
2、内存3G
3、SATA硬盘2T
web服务器:tomcat5.5
打压工具:Apache Bench
监控工具: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/sbin/ab -n 1000 -c 400 http://192.168.175.130:8080/labWeb/insertMongodb.do?insertNum=1000
监控:bin/mongostat
可以看到在400个并发情况下,插入100万记录,QPS在28000左右。