MongoDB Plugin

This commit is contained in:
rzs840707 2014-06-20 14:51:22 +08:00
parent ee7c390bc6
commit 0b0ff00a09
4 changed files with 205 additions and 0 deletions

View File

@ -0,0 +1,86 @@
package org.bench4q.agent.plugin.basic.MongoDB;
import java.net.UnknownHostException;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.util.JSON;
@Plugin("MongoDBPlugin")
public class MongoDBPlugin {
@Constructor
public MongoDBPlugin() {
}
@Behavior(value = "Insert", type = BehaviorType.USER_BEHAVIOR)
public MongoDBReturn insert(
@Parameter(value = "key", type = SupportTypes.Field) String key,
@Parameter(value = "value", type = SupportTypes.Field) String value,
@Parameter(value = "hostName", type = SupportTypes.Field) String hostName,
@Parameter(value = "port", type = SupportTypes.Field) int port,
@Parameter(value = "dbName", type = SupportTypes.Field) String dbName) {
// TODO测试MongoDB的插入性能
int recordsum = 0;
try {
@SuppressWarnings("deprecation")
Mongo mongo = new Mongo(hostName, port);
DB db = mongo.getDB(dbName);
DBCollection users;
// 获取users DBCollection如果默认没有创建mongodb会自动创建
users = db.getCollection("users");
DBObject user = new BasicDBObject();
user.put(key, value);
users.save(user);
DBCollection test = db.getCollection("users");
// 查询所有的数据
DBCursor cur = test.find();
while (cur.hasNext()) {
System.out.println(cur.next());
}
recordsum = test.find().count();
System.out.println("总数目: " + cur.count());
System.out.println("当前游标的id号: " + cur.getCursorId());
System.out.println(JSON.serialize(cur));
if (mongo != null) {
mongo.close();
}
mongo = null;
db = null;
users = null;
} catch (UnknownHostException e) {
e.printStackTrace();
}
return new MongoDBReturn(recordsum > 0);
}
/*
* @Behavior(value = "Query", type = BehaviorType.USER_BEHAVIOR) public
* MongoDBReturn query(
*
* @Parameter(value = "queryString", type = SupportTypes.Field) String
* queryString,
*
* @Parameter(value = "attributes", type = SupportTypes.Field) List<Object>
* attributes){
*
*
* return null; }
*/
}

View File

@ -0,0 +1,9 @@
package org.bench4q.agent.plugin.basic.MongoDB;
import org.bench4q.agent.plugin.basic.PluginReturn;
public class MongoDBReturn extends PluginReturn {
public MongoDBReturn(boolean success) {
super(success);
}
}

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ui SYSTEM "../../dtd/ui.dtd">
<ui>
<plugin name="MongoDBPlugin">
<params />
</plugin>
<behavior name="Insert">
<params>
<param name="key" label="key">
<field size="7" />
</param>
<param name="value" label="value">
<field size="7" />
</param>
<param name="hostName" label="hostName">
<field size="7" />
</param>
<param name="port" label="port">
<field size="7" />
</param>
<param name="dbname" label="dbname">
<field size="7" />
</param>
</params>
</behavior>
</ui>

View File

@ -0,0 +1,83 @@
package org.bench4q.agent.test.plugin;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.bench4q.agent.plugin.basic.MongoDB.MongoDBPlugin;
import org.bench4q.agent.plugin.basic.MongoDB.MongoDBReturn;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.VUser;
import org.bench4q.agent.test.TestBase;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.helper.TestHelper;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BatchModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.PageModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
public class Test_MongoDBPlugin extends TestBase {
@Test
public void test_Insert() {
MongoDBPlugin mongoDBPlugin = new MongoDBPlugin();
MongoDBReturn result = mongoDBPlugin.insert("key1", "value1",
"localhost", 27017, "temp");
assertTrue(result.getSuccessCount() > 0);
}
private RunScenarioModel buildUpMongoDBScenario() throws IOException {
RunScenarioModel scenarioModel = new RunScenarioModel();
scenarioModel.setUsePlugins(new LinkedList<UsePluginModel>());
UsePluginModel mongoDB = new UsePluginModel();
String pluginId = "mongo1";
mongoDB.setId(pluginId);
mongoDB.setName("MongoDBPlugin");
mongoDB.setParameters(new LinkedList<ParameterModel>());
scenarioModel.getUsePlugins().add(mongoDB);
scenarioModel.setPages(new LinkedList<PageModel>());
PageModel page1 = new PageModel();
BatchModel batch = new BatchModel();
page1.getBatches().add(batch);
scenarioModel.getPages().add(page1);
batch.getBehaviors().add(
BehaviorModel.UserBehaviorBuilder(0, "Insert", pluginId, Arrays
.asList(ParameterModel.createParameter("key", "key2"),
ParameterModel.createParameter("value",
"value2"), ParameterModel
.createParameter("hostName",
"localhost"), ParameterModel
.createParameter("port", "27017"),
ParameterModel
.createParameter("dbName", "temp"))));
String scriptContent = MarshalHelper.tryMarshal(scenarioModel);
System.out.println(scriptContent);
File targetFile = new File("Scripts"
+ System.getProperty("file.separator") + "mongoScript.xml");
TestHelper.createFileIfNotExist(targetFile);
FileUtils.writeStringToFile(targetFile, scriptContent);
return scenarioModel;
}
@Test
public void test_BuildScenario() throws IOException {
RunScenarioModel result = buildUpMongoDBScenario();
VUser vUser = createVUser(Scenario.scenarioBuilderWithCompile(result),
UUID.randomUUID());
vUser.run();
}
}