/containers/blockchain/cliLoadTester/src/main/java/secretApp/testApp/ExecutionApp.java

https://github.com/IBM/android-kubernetes-blockchain · Java · 188 lines · 168 code · 15 blank · 5 comment · 26 complexity · 313ae079b67f24dba0b6647146ce8388 MD5 · raw file

  1. package secretApp.testApp;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7. import com.google.gson.JsonObject;
  8. import com.google.gson.JsonParser;
  9. import com.mongodb.BasicDBObject;
  10. import com.mongodb.DB;
  11. import com.mongodb.DBCollection;
  12. import com.mongodb.DBCursor;
  13. import com.mongodb.DBObject;
  14. import com.mongodb.MongoClient;
  15. public class ExecutionApp {
  16. private static String executionURL = "http://localhost:3000/api/execute";
  17. private static String dbName = "testResults";
  18. private static int count = 0;
  19. private static int steps = 100;
  20. private static int fixOperations = 10;
  21. private static int totalUsers = 1500;
  22. private static int queuedOperations = 2000;
  23. @SuppressWarnings("static-access")
  24. public static void main(String[] args) {
  25. try {
  26. while (true) {
  27. MongoClient mongo = getConnection();
  28. if (mongo == null) {
  29. System.exit(0);
  30. }
  31. Set<DBObject> dbobj = getUserObjects("results", mongo);
  32. closeConnection(mongo);
  33. if (dbobj.size() > queuedOperations) {
  34. System.out.println("Wait for 3 min to finsh execution of queued request");
  35. Thread.currentThread().sleep(180000);
  36. } else {
  37. mongo = getConnection();
  38. if (mongo == null) {
  39. System.exit(0);
  40. }
  41. dbobj = getUserObjects("users", mongo);
  42. closeConnection(mongo);
  43. if (dbobj.size() > 10) {
  44. performQueryOpertion(fixOperations);
  45. System.out.println("Total Operations queue : " + count);
  46. performInvokeOpertion(fixOperations, String.valueOf(steps));
  47. System.out.println("Total Operations queue : " + count);
  48. performQueryOpertion(fixOperations);
  49. System.out.println("Total Operations queue : " + count);
  50. steps += 10;
  51. if (dbobj.size() < totalUsers) {
  52. System.out.println("Enrolling users");
  53. enrollUsers(fixOperations);
  54. }
  55. } else {
  56. System.out.println("Enrolling users");
  57. enrollUsers(fixOperations);
  58. System.out.println("Total Operations queue : " + count);
  59. Thread.currentThread().sleep(10000);
  60. }
  61. }
  62. }
  63. } catch (Exception ioe) {
  64. ioe.printStackTrace();
  65. }
  66. }
  67. private static MongoClient getConnection() {
  68. int attempt = 0;
  69. MongoClient mongo = null;
  70. while (mongo == null && attempt <= 10) {
  71. attempt++;
  72. try {
  73. mongo = new MongoClient("localhost", 27017);
  74. break;
  75. } catch (Exception e) {
  76. mongo = null;
  77. }
  78. }
  79. return mongo;
  80. }
  81. private static void closeConnection(MongoClient mongo) {
  82. mongo.close();
  83. }
  84. private static void executeRequest(String data, MongoClient mongo) {
  85. String body;
  86. try {
  87. body = Task.post(executionURL, data);
  88. JsonObject jsonObj = new JsonParser().parse(body).getAsJsonObject();
  89. if (jsonObj.get("status").toString().equals("\"success\"")) {
  90. DB database = mongo.getDB(dbName);
  91. DBCollection collection = Task.getDBCollection(database, "results");
  92. List<DBObject> list = new ArrayList<>();
  93. BasicDBObject dataObject = new BasicDBObject();
  94. dataObject.append("resultId", jsonObj.get("resultId").getAsString());
  95. dataObject.append("query", data);
  96. list.add(dataObject);
  97. collection.insert(list);
  98. }
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. private static Set<DBObject> getUserObjects(String collectionName, MongoClient mongo) {
  104. Set<DBObject> users = new HashSet<>();
  105. try {
  106. DB database = mongo.getDB(dbName);
  107. DBCollection collection = Task.getDBCollection(database, collectionName);
  108. DBCursor cursor = collection.find();
  109. while (cursor.hasNext()) {
  110. users.add(cursor.next());
  111. }
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. }
  115. return users;
  116. }
  117. private static void performQueryOpertion(int number) {
  118. MongoClient mongo = getConnection();
  119. if (mongo == null) {
  120. System.exit(0);
  121. }
  122. Set<DBObject> users = getUserObjects("users", mongo);
  123. int temp = 0;
  124. for (DBObject dbObject : users) {
  125. count++;
  126. temp++;
  127. String userId = dbObject.get("user").toString();
  128. String query = "type=query&queue=user_queue&params={\"userId\":\"" + userId
  129. + "\" , \"fcn\":\"getState\" ,\"args\":[\"" + userId + "\"]}";
  130. executeRequest(query, mongo);
  131. if (temp >= number) {
  132. break;
  133. }
  134. }
  135. closeConnection(mongo);
  136. }
  137. private static void performInvokeOpertion(int number, String steps) {
  138. MongoClient mongo = getConnection();
  139. if (mongo == null) {
  140. System.exit(0);
  141. }
  142. Set<DBObject> users = getUserObjects("users", mongo);
  143. int temp = 0;
  144. for (DBObject dbObject : users) {
  145. count++;
  146. temp++;
  147. String userId = dbObject.get("user").toString();
  148. String query = "type=invoke&queue=user_queue&params={ \"userId\":\"" + userId
  149. + "\",\"fcn\":\"generateFitcoins\",\"args\":[\"" + userId + "\",\"" + steps + "\"]}";
  150. // executorService.execute(new ExecutionTask(query, executionURL,
  151. // dbName));
  152. executeRequest(query, mongo);
  153. if (temp >= number) {
  154. break;
  155. }
  156. }
  157. closeConnection(mongo);
  158. }
  159. private static void enrollUsers(int number) {
  160. MongoClient mongo = getConnection();
  161. if (mongo == null) {
  162. System.exit(0);
  163. }
  164. for (int i = 0; i < number; i++) {
  165. count++;
  166. // executorService.execute(new
  167. // ExecutionTask("type=enroll&queue=user_queue&params={}",
  168. // executionURL, dbName));
  169. executeRequest("type=enroll&queue=user_queue&params={}", mongo);
  170. }
  171. closeConnection(mongo);
  172. }
  173. }