/src/main/java/com/johnlpage/pocdriver/POCDriver.java

https://github.com/johnlpage/POCDriver · Java · 105 lines · 88 code · 13 blank · 4 comment · 12 complexity · 30fac36b9663a5d7c13d2caabf4d4fdd MD5 · raw file

  1. package com.johnlpage.pocdriver;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. import com.google.gson.JsonElement;
  5. import com.google.gson.JsonParser;
  6. import org.apache.commons.cli.ParseException;
  7. import org.bson.BsonBinaryWriter;
  8. import org.bson.codecs.DocumentCodec;
  9. import org.bson.codecs.EncoderContext;
  10. import org.bson.io.BasicOutputBuffer;
  11. import java.util.logging.LogManager;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. public class POCDriver {
  15. public static void main(String[] args) {
  16. POCTestOptions testOpts;
  17. LogManager.getLogManager().reset();
  18. Logger logger = LoggerFactory.getLogger(POCDriver.class);
  19. logger.info("MongoDB Proof Of Concept - Load Generator version 0.1.2");
  20. try {
  21. testOpts = new POCTestOptions(args);
  22. // Quit after displaying help message
  23. if (testOpts.helpOnly) {
  24. return;
  25. }
  26. if (testOpts.arrayupdates > 0 && (testOpts.arraytop < 1 || testOpts.arraynext < 1)) {
  27. logger.error("You must specify an array size to update arrays");
  28. return;
  29. }
  30. if (testOpts.printOnly) {
  31. printTestDocument(testOpts);
  32. return;
  33. }
  34. } catch (ParseException e) {
  35. System.err.println(e.getMessage());
  36. return;
  37. }
  38. POCTestResults testResults = new POCTestResults(testOpts);
  39. LoadRunner runner = new LoadRunner(testOpts);
  40. runner.RunLoad(testOpts, testResults);
  41. }
  42. private static void printTestDocument(final POCTestOptions testOpts) {
  43. //Sets up sample data don't remove
  44. TestRecord tr;
  45. int[] arr = new int[2];
  46. arr[0] = testOpts.arraytop;
  47. arr[1] = testOpts.arraynext;
  48. tr = new TestRecord(testOpts.numFields, testOpts.depth, testOpts.textFieldLen,
  49. 1, 12345678, testOpts.NUMBER_SIZE, arr, testOpts.blobSize, testOpts.locationCodes);
  50. //System.out.println(tr);
  51. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  52. JsonParser jp = new JsonParser();
  53. JsonElement je = jp.parse(tr.internalDoc.toJson());
  54. String json = gson.toJson(je);
  55. StringBuilder newJson = new StringBuilder();
  56. int arrays = 0;
  57. // Collapse inner newlines
  58. boolean inquotes = false;
  59. for (int c = 0; c < json.length(); c++) {
  60. char inChar = json.charAt(c);
  61. if (inChar == '[') {
  62. arrays++;
  63. }
  64. if (inChar == ']') {
  65. arrays--;
  66. }
  67. if (inChar == '"') {
  68. inquotes = !inquotes;
  69. }
  70. if (arrays > 1 && inChar == '\n') {
  71. continue;
  72. }
  73. if (arrays > 1 && !inquotes && inChar == ' ') {
  74. continue;
  75. }
  76. newJson.append(json.charAt(c));
  77. }
  78. //This is actual output not logging
  79. System.out.println(newJson.toString());
  80. //Thanks to Ross Lawley for this bit of black magic
  81. BasicOutputBuffer buffer = new BasicOutputBuffer();
  82. BsonBinaryWriter binaryWriter = new BsonBinaryWriter(buffer);
  83. new DocumentCodec().encode(binaryWriter, tr.internalDoc, EncoderContext.builder().build());
  84. int length = binaryWriter.getBsonOutput().getSize();
  85. System.out.println(String.format("Documents are %.2f KB each as BSON", (float) length / 1024));
  86. }
  87. }