diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/MongoDBPlugin.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/MongoDBPlugin.java index aee15254..b7f28613 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/MongoDBPlugin.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/MongoDBPlugin.java @@ -1,6 +1,6 @@ package org.bench4q.agent.plugin.basic.MongoDB; -import java.net.UnknownHostException; +import java.util.Arrays; import org.bench4q.agent.plugin.Constructor; import org.bench4q.agent.plugin.Parameter; @@ -8,6 +8,8 @@ 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 org.bench4q.agent.utils.types.Table; +import org.bench4q.agent.utils.types.Table.Row; import com.mongodb.BasicDBObject; import com.mongodb.DB; @@ -15,72 +17,82 @@ 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 { + private final String hostName; + private final int port; + private final String dbName; + private final String tableUnderTest = "users"; @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, + public MongoDBPlugin( @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的插入性能 + this.hostName = hostName; + this.port = port; + this.dbName = dbName; + } - int recordsum = 0; + @SuppressWarnings("deprecation") + @Behavior(value = "Insert", type = BehaviorType.USER_BEHAVIOR) + public MongoDBReturn insert( + @Parameter(value = "id", type = SupportTypes.Field) String id, + @Parameter(value = "value", type = SupportTypes.Field) String value) { + // int recordsum = 0; + Mongo mongo = null; try { - @SuppressWarnings("deprecation") - Mongo mongo = new Mongo(hostName, port); + 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)); - + DBCollection table = db.getCollection(this.tableUnderTest); + DBObject valueToInsert = new BasicDBObject(); + valueToInsert.put("_id", id); + valueToInsert.put("value", value); + table.save(valueToInsert); + return new MongoDBReturn(true); + } catch (Exception e) { + e.printStackTrace(); + return new MongoDBReturn(false); + } finally { if (mongo != null) { mongo.close(); } - mongo = null; - db = null; - users = null; - - } catch (UnknownHostException e) { - e.printStackTrace(); } - return new MongoDBReturn(recordsum > 0); + + } + + @SuppressWarnings("deprecation") + @Behavior(value = "Query", type = BehaviorType.USER_BEHAVIOR) + public MongoDBReturn query( + @Parameter(value = "properties", type = SupportTypes.Table) String properties) { + Mongo mongo = null; + try { + mongo = new Mongo(hostName, port); + DB db = mongo.getDB(dbName); + DBCollection table = db.getCollection(this.tableUnderTest); + Table propertiesTable = Table.buildTable(properties, + Arrays.asList("propertyName", "propertyValue")); + BasicDBObject query = new BasicDBObject(); + for (Row row : propertiesTable.getRows()) { + String propertyName = row.getCell("propertyName"); + String propertyValue = row.getCell("propertyValue"); + query.append(propertyName, propertyValue); + } + + DBCursor queryResult = table.find(query); + for (DBObject item : queryResult.toArray()) { + System.out.println(item); + } + return new MongoDBReturn(true); + } catch (Exception e) { + e.printStackTrace(); + return new MongoDBReturn(false); + } finally { + if (mongo != null) { + mongo.close(); + } + } + } - /* - * @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 - * attributes){ - * - * - * return null; } - */ } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/ui.xml b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/ui.xml index d2546492..4189d005 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/ui.xml +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/MongoDB/ui.xml @@ -2,16 +2,7 @@ - - - - - - - - - @@ -20,8 +11,24 @@ - + - - + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/utils/types/Table.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/utils/types/Table.java index cc1da489..a0b67123 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/utils/types/Table.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/utils/types/Table.java @@ -162,8 +162,10 @@ public class Table extends Type { List tempEntries = ParameterParser.getRealTokens( Table.ESCAPE_ROW_SEPARATOR, value, false); List> allValues = new ArrayList>(); - for (int i = 0; i < tempEntries.size(); i++) { - String tempEntry = tempEntries.get(i); + for (String tempEntry : tempEntries) { + if (tempEntry.trim().isEmpty()) { + continue; + } List values = ParameterParser.getRealTokens( Table.ESCAPE_COLUMN_SEPARATOR, tempEntry, true); arrangeTableWithRowSeparatorTail(values); @@ -179,16 +181,14 @@ public class Table extends Type { for (int i = 0; i < allValues.size(); i++) { List resultValues = new ArrayList(); List tempValues = allValues.get(i); - for (int j = 0; j < tempValues.size(); j++) { - String v = tempValues.get(j); - List temp = ParameterParser - .getRealTokens("=", v, false); - String res; - if (temp.size() <= 1) { + for (String v : tempValues) { + // List temp = ParameterParser + // .getRealTokens("=", v, false); + int index = v.indexOf('='); + if (index < 0) { continue; } - res = temp.get(1); - resultValues.add(res); + resultValues.add(v.substring(index + 1)); } result.add(resultValues); } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java index 615ad9e2..b8ec2f78 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java @@ -30,11 +30,26 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:application-context.xml" }) public class Test_MongoDBPlugin extends TestBase { + private UUID testId = UUID.randomUUID(); + @Test public void test_Insert() { - MongoDBPlugin mongoDBPlugin = new MongoDBPlugin(); - MongoDBReturn result = mongoDBPlugin.insert("key1", "value1", - "localhost", 27017, "temp"); + MongoDBPlugin mongoDBPlugin = new MongoDBPlugin("localhost", 27017, + "temp"); + MongoDBReturn result = mongoDBPlugin.insert(this.testId.toString(), + "value1"); + result = mongoDBPlugin.query("propertyName=_id|propertyValue=" + + this.testId.toString() + "|;"); + assertTrue(result.getSuccessCount() > 0); + } + + @Test + public void test_query() { + MongoDBPlugin mongoDBPlugin = new MongoDBPlugin("localhost", 27017, + "temp"); + MongoDBReturn result = mongoDBPlugin + .query("propertyName=_id|propertyValue=" + + this.testId.toString() + "|;"); assertTrue(result.getSuccessCount() > 0); } @@ -45,7 +60,10 @@ public class Test_MongoDBPlugin extends TestBase { String pluginId = "mongo1"; mongoDB.setId(pluginId); mongoDB.setName("MongoDBPlugin"); - mongoDB.setParameters(new LinkedList()); + mongoDB.setParameters(Arrays.asList( + ParameterModel.createParameter("hostName", "localhost"), + ParameterModel.createParameter("port", "27017"), + ParameterModel.createParameter("dbName", "temp"))); scenarioModel.getUsePlugins().add(mongoDB); scenarioModel.setPages(new LinkedList()); @@ -58,12 +76,7 @@ public class Test_MongoDBPlugin extends TestBase { 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")))); + "value2")))); String scriptContent = MarshalHelper.tryMarshal(scenarioModel); System.out.println(scriptContent); File targetFile = new File("Scripts"