insert_bench.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. var MongoClient = require('./').MongoClient,
  2. assert = require('assert');
  3. // var memwatch = require('memwatch-next');
  4. // memwatch.on('leak', function(info) {
  5. // console.log("======== leak")
  6. // });
  7. //
  8. // memwatch.on('stats', function(stats) {
  9. // console.log("======== stats")
  10. // console.dir(stats)
  11. // });
  12. // // Take first snapshot
  13. // var hd = new memwatch.HeapDiff();
  14. MongoClient.connect('mongodb://localhost:27017/bench', function(err, db) {
  15. var docs = [];
  16. var total = 1000;
  17. var count = total;
  18. var measurements = [];
  19. // Insert a bunch of documents
  20. for(var i = 0; i < 100; i++) {
  21. docs.push(JSON.parse(data));
  22. }
  23. var col = db.collection('inserts');
  24. function execute(col, callback) {
  25. var start = new Date().getTime();
  26. col.find({}).limit(100).toArray(function(e, docs) {
  27. measurements.push(new Date().getTime() - start);
  28. assert.equal(null, e);
  29. callback();
  30. });
  31. }
  32. console.log("== insert documents")
  33. col.insert(docs, function(e, r) {
  34. docs = [];
  35. assert.equal(null, e);
  36. console.log("== start bench")
  37. for(var i = 0; i < total; i++) {
  38. execute(col, function(e) {
  39. count = count - 1;
  40. if(count == 0) {
  41. // Calculate total execution time for operations
  42. var totalTime = measurements.reduce(function(prev, curr) {
  43. return prev + curr;
  44. }, 0);
  45. console.log("===========================================");
  46. console.log("total time: " + totalTime)
  47. // var diff = hd.end();
  48. // console.log("===========================================");
  49. // console.log(JSON.stringify(diff, null, 2))
  50. db.close();
  51. process.exit(0)
  52. }
  53. });
  54. }
  55. });
  56. });
  57. var data = JSON.stringify({
  58. "data": [
  59. {
  60. "_id": 1,
  61. "x": 11
  62. },
  63. {
  64. "_id": 2,
  65. "x": 22
  66. },
  67. {
  68. "_id": 3,
  69. "x": 33
  70. }
  71. ],
  72. "collection_name": "test",
  73. "database_name": "command-monitoring-tests",
  74. "tests": [
  75. {
  76. "description": "A successful mixed bulk write",
  77. "operation": {
  78. "name": "bulkWrite",
  79. "arguments": {
  80. "requests": [
  81. {
  82. "insertOne": {
  83. "document": {
  84. "_id": 4,
  85. "x": 44
  86. }
  87. }
  88. },
  89. {
  90. "updateOne": {
  91. "filter": {
  92. "_id": 3
  93. },
  94. "update": {
  95. "set": {
  96. "x": 333
  97. }
  98. }
  99. }
  100. }
  101. ]
  102. }
  103. },
  104. "expectations": [
  105. {
  106. "command_started_event": {
  107. "command": {
  108. "insert": "test",
  109. "documents": [
  110. {
  111. "_id": 4,
  112. "x": 44
  113. }
  114. ],
  115. "ordered": true
  116. },
  117. "command_name": "insert",
  118. "database_name": "command-monitoring-tests"
  119. }
  120. },
  121. {
  122. "command_succeeded_event": {
  123. "reply": {
  124. "ok": 1.0,
  125. "n": 1
  126. },
  127. "command_name": "insert"
  128. }
  129. },
  130. {
  131. "command_started_event": {
  132. "command": {
  133. "update": "test",
  134. "updates": [
  135. {
  136. "q": {
  137. "_id": 3
  138. },
  139. "u": {
  140. "set": {
  141. "x": 333
  142. }
  143. },
  144. "upsert": false,
  145. "multi": false
  146. }
  147. ],
  148. "ordered": true
  149. },
  150. "command_name": "update",
  151. "database_name": "command-monitoring-tests"
  152. }
  153. },
  154. {
  155. "command_succeeded_event": {
  156. "reply": {
  157. "ok": 1.0,
  158. "n": 1
  159. },
  160. "command_name": "update"
  161. }
  162. }
  163. ]
  164. },
  165. {
  166. "description": "A successful unordered bulk write with an unacknowledged write concern",
  167. "operation": {
  168. "name": "bulkWrite",
  169. "arguments": {
  170. "requests": [
  171. {
  172. "insertOne": {
  173. "document": {
  174. "_id": 4,
  175. "x": 44
  176. }
  177. }
  178. }
  179. ],
  180. "ordered": false,
  181. "writeConcern": {
  182. "w": 0
  183. }
  184. }
  185. },
  186. "expectations": [
  187. {
  188. "command_started_event": {
  189. "command": {
  190. "insert": "test",
  191. "documents": [
  192. {
  193. "_id": 4,
  194. "x": 44
  195. }
  196. ],
  197. "ordered": false,
  198. "writeConcern": {
  199. "w": 0
  200. }
  201. },
  202. "command_name": "insert",
  203. "database_name": "command-monitoring-tests"
  204. }
  205. },
  206. {
  207. "command_succeeded_event": {
  208. "reply": {
  209. "ok": 1.0
  210. },
  211. "command_name": "insert"
  212. }
  213. }
  214. ]
  215. }
  216. ]
  217. });