parent
448d74f707
commit
e613738fa8
|
@ -1,6 +1,6 @@
|
||||||
package org.bench4q.agent.plugin.basic.MongoDB;
|
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.Constructor;
|
||||||
import org.bench4q.agent.plugin.Parameter;
|
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.Behavior;
|
||||||
import org.bench4q.agent.plugin.behavior.BehaviorType;
|
import org.bench4q.agent.plugin.behavior.BehaviorType;
|
||||||
import org.bench4q.agent.utils.Type.SupportTypes;
|
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.BasicDBObject;
|
||||||
import com.mongodb.DB;
|
import com.mongodb.DB;
|
||||||
|
@ -15,72 +17,82 @@ import com.mongodb.DBCollection;
|
||||||
import com.mongodb.DBCursor;
|
import com.mongodb.DBCursor;
|
||||||
import com.mongodb.DBObject;
|
import com.mongodb.DBObject;
|
||||||
import com.mongodb.Mongo;
|
import com.mongodb.Mongo;
|
||||||
import com.mongodb.util.JSON;
|
|
||||||
|
|
||||||
@Plugin("MongoDBPlugin")
|
@Plugin("MongoDBPlugin")
|
||||||
public class MongoDBPlugin {
|
public class MongoDBPlugin {
|
||||||
|
private final String hostName;
|
||||||
|
private final int port;
|
||||||
|
private final String dbName;
|
||||||
|
private final String tableUnderTest = "users";
|
||||||
|
|
||||||
@Constructor
|
@Constructor
|
||||||
public MongoDBPlugin() {
|
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 = "hostName", type = SupportTypes.Field) String hostName,
|
||||||
@Parameter(value = "port", type = SupportTypes.Field) int port,
|
@Parameter(value = "port", type = SupportTypes.Field) int port,
|
||||||
@Parameter(value = "dbName", type = SupportTypes.Field) String dbName) {
|
@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 {
|
try {
|
||||||
@SuppressWarnings("deprecation")
|
mongo = new Mongo(hostName, port);
|
||||||
Mongo mongo = new Mongo(hostName, port);
|
|
||||||
DB db = mongo.getDB(dbName);
|
DB db = mongo.getDB(dbName);
|
||||||
DBCollection users;
|
DBCollection table = db.getCollection(this.tableUnderTest);
|
||||||
// 获取users DBCollection;如果默认没有创建,mongodb会自动创建
|
DBObject valueToInsert = new BasicDBObject();
|
||||||
users = db.getCollection("users");
|
valueToInsert.put("_id", id);
|
||||||
|
valueToInsert.put("value", value);
|
||||||
DBObject user = new BasicDBObject();
|
table.save(valueToInsert);
|
||||||
user.put(key, value);
|
return new MongoDBReturn(true);
|
||||||
users.save(user);
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
DBCollection test = db.getCollection("users");
|
return new MongoDBReturn(false);
|
||||||
// 查询所有的数据
|
} finally {
|
||||||
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) {
|
if (mongo != null) {
|
||||||
mongo.close();
|
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<Object>
|
|
||||||
* attributes){
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* return null; }
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,16 +2,7 @@
|
||||||
<!DOCTYPE ui SYSTEM "../../dtd/ui.dtd">
|
<!DOCTYPE ui SYSTEM "../../dtd/ui.dtd">
|
||||||
<ui>
|
<ui>
|
||||||
<plugin name="MongoDBPlugin">
|
<plugin name="MongoDBPlugin">
|
||||||
<params />
|
|
||||||
</plugin>
|
|
||||||
<behavior name="Insert">
|
|
||||||
<params>
|
<params>
|
||||||
<param name="key" label="key">
|
|
||||||
<field size="7" />
|
|
||||||
</param>
|
|
||||||
<param name="value" label="value">
|
|
||||||
<field size="7" />
|
|
||||||
</param>
|
|
||||||
<param name="hostName" label="hostName">
|
<param name="hostName" label="hostName">
|
||||||
<field size="7" />
|
<field size="7" />
|
||||||
</param>
|
</param>
|
||||||
|
@ -20,8 +11,24 @@
|
||||||
</param>
|
</param>
|
||||||
<param name="dbname" label="dbname">
|
<param name="dbname" label="dbname">
|
||||||
<field size="7" />
|
<field size="7" />
|
||||||
</param>
|
</param>
|
||||||
</params>
|
</params>
|
||||||
</behavior>
|
</plugin>
|
||||||
|
<behavior name="Insert">
|
||||||
|
<params>
|
||||||
|
<param name="id" label="id">
|
||||||
|
<field size="7" />
|
||||||
|
</param>
|
||||||
|
<param name="value" label="value">
|
||||||
|
<field size="7" />
|
||||||
|
</param>
|
||||||
|
</params>
|
||||||
|
</behavior>
|
||||||
|
<behavior name="Query">
|
||||||
|
<params>
|
||||||
|
<param name="properties" label="properties">
|
||||||
|
<table cols="propertyName;propertyValue" />
|
||||||
|
</param>
|
||||||
|
</params>
|
||||||
|
</behavior>
|
||||||
</ui>
|
</ui>
|
|
@ -162,8 +162,10 @@ public class Table extends Type {
|
||||||
List<String> tempEntries = ParameterParser.getRealTokens(
|
List<String> tempEntries = ParameterParser.getRealTokens(
|
||||||
Table.ESCAPE_ROW_SEPARATOR, value, false);
|
Table.ESCAPE_ROW_SEPARATOR, value, false);
|
||||||
List<List<String>> allValues = new ArrayList<List<String>>();
|
List<List<String>> allValues = new ArrayList<List<String>>();
|
||||||
for (int i = 0; i < tempEntries.size(); i++) {
|
for (String tempEntry : tempEntries) {
|
||||||
String tempEntry = tempEntries.get(i);
|
if (tempEntry.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
List<String> values = ParameterParser.getRealTokens(
|
List<String> values = ParameterParser.getRealTokens(
|
||||||
Table.ESCAPE_COLUMN_SEPARATOR, tempEntry, true);
|
Table.ESCAPE_COLUMN_SEPARATOR, tempEntry, true);
|
||||||
arrangeTableWithRowSeparatorTail(values);
|
arrangeTableWithRowSeparatorTail(values);
|
||||||
|
@ -179,16 +181,14 @@ public class Table extends Type {
|
||||||
for (int i = 0; i < allValues.size(); i++) {
|
for (int i = 0; i < allValues.size(); i++) {
|
||||||
List<String> resultValues = new ArrayList<String>();
|
List<String> resultValues = new ArrayList<String>();
|
||||||
List<String> tempValues = allValues.get(i);
|
List<String> tempValues = allValues.get(i);
|
||||||
for (int j = 0; j < tempValues.size(); j++) {
|
for (String v : tempValues) {
|
||||||
String v = tempValues.get(j);
|
// List<String> temp = ParameterParser
|
||||||
List<String> temp = ParameterParser
|
// .getRealTokens("=", v, false);
|
||||||
.getRealTokens("=", v, false);
|
int index = v.indexOf('=');
|
||||||
String res;
|
if (index < 0) {
|
||||||
if (temp.size() <= 1) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
res = temp.get(1);
|
resultValues.add(v.substring(index + 1));
|
||||||
resultValues.add(res);
|
|
||||||
}
|
}
|
||||||
result.add(resultValues);
|
result.add(resultValues);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,11 +30,26 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(locations = { "classpath:application-context.xml" })
|
@ContextConfiguration(locations = { "classpath:application-context.xml" })
|
||||||
public class Test_MongoDBPlugin extends TestBase {
|
public class Test_MongoDBPlugin extends TestBase {
|
||||||
|
private UUID testId = UUID.randomUUID();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test_Insert() {
|
public void test_Insert() {
|
||||||
MongoDBPlugin mongoDBPlugin = new MongoDBPlugin();
|
MongoDBPlugin mongoDBPlugin = new MongoDBPlugin("localhost", 27017,
|
||||||
MongoDBReturn result = mongoDBPlugin.insert("key1", "value1",
|
"temp");
|
||||||
"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);
|
assertTrue(result.getSuccessCount() > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +60,10 @@ public class Test_MongoDBPlugin extends TestBase {
|
||||||
String pluginId = "mongo1";
|
String pluginId = "mongo1";
|
||||||
mongoDB.setId(pluginId);
|
mongoDB.setId(pluginId);
|
||||||
mongoDB.setName("MongoDBPlugin");
|
mongoDB.setName("MongoDBPlugin");
|
||||||
mongoDB.setParameters(new LinkedList<ParameterModel>());
|
mongoDB.setParameters(Arrays.asList(
|
||||||
|
ParameterModel.createParameter("hostName", "localhost"),
|
||||||
|
ParameterModel.createParameter("port", "27017"),
|
||||||
|
ParameterModel.createParameter("dbName", "temp")));
|
||||||
scenarioModel.getUsePlugins().add(mongoDB);
|
scenarioModel.getUsePlugins().add(mongoDB);
|
||||||
|
|
||||||
scenarioModel.setPages(new LinkedList<PageModel>());
|
scenarioModel.setPages(new LinkedList<PageModel>());
|
||||||
|
@ -58,12 +76,7 @@ public class Test_MongoDBPlugin extends TestBase {
|
||||||
BehaviorModel.UserBehaviorBuilder(0, "Insert", pluginId, Arrays
|
BehaviorModel.UserBehaviorBuilder(0, "Insert", pluginId, Arrays
|
||||||
.asList(ParameterModel.createParameter("key", "key2"),
|
.asList(ParameterModel.createParameter("key", "key2"),
|
||||||
ParameterModel.createParameter("value",
|
ParameterModel.createParameter("value",
|
||||||
"value2"), ParameterModel
|
"value2"))));
|
||||||
.createParameter("hostName",
|
|
||||||
"localhost"), ParameterModel
|
|
||||||
.createParameter("port", "27017"),
|
|
||||||
ParameterModel
|
|
||||||
.createParameter("dbName", "temp"))));
|
|
||||||
String scriptContent = MarshalHelper.tryMarshal(scenarioModel);
|
String scriptContent = MarshalHelper.tryMarshal(scenarioModel);
|
||||||
System.out.println(scriptContent);
|
System.out.println(scriptContent);
|
||||||
File targetFile = new File("Scripts"
|
File targetFile = new File("Scripts"
|
||||||
|
|
Loading…
Reference in New Issue