Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. var Promise = require('bluebird');
  2. var util = require('util');
  3. var heapdump = require('heapdump');
  4.  
  5. function log(message) {
  6. console.log(new Date().toISOString() + ' - ' + message);
  7. }
  8.  
  9. function gc() {
  10. log('-- Forced GC');
  11. global.gc();
  12. }
  13. var countOps = 0;
  14. var countOpsSuccess = 0;
  15. var countOpsErrors = 0;
  16.  
  17. var initialMemoryUsage = process.memoryUsage();
  18. log(util.inspect(initialMemoryUsage));
  19. function printMemoryUsage() {
  20. var diff = process.memoryUsage();
  21. var result = {};
  22. result.diff_rss = diff.rss - initialMemoryUsage.rss;
  23. result.diff_heapTotal = diff.heapTotal - initialMemoryUsage.heapTotal;
  24. result.diff_heapUsed = diff.heapUsed - initialMemoryUsage.heapUsed;
  25. log(`${countOps} tried, ${countOpsSuccess} done, ${countOpsErrors} errors; ${parseInt(result.diff_heapUsed / (1024 * 1024 ))} MiB`);
  26. }
  27.  
  28. var INSERT_TIMERS = 100;
  29. var DELETE_TIMERS = 20;
  30. var UPDATE_TIMERS = 150;
  31. var TIME_END = parseFloat(process.argv[2] || 15) * 60 * 1000;
  32.  
  33.  
  34. function startTest() {
  35. log('-- Start test');
  36. var r = require('rethinkdbdash')({
  37. max: 1000,
  38. buffer: 50,
  39. host: 'localhost',
  40. port: 28015,
  41. // timeoutGb: 1000 * 10
  42. });
  43.  
  44. setup(r).then(function () {
  45. var feeds = [];
  46. openFeeds(r, feeds);
  47. var timers = [];
  48. fireUpdates(r, timers);
  49. setTimeout(function () {
  50. tearDown(r, feeds, timers)
  51. }, TIME_END);
  52. timers.push(setInterval(() => printMemoryUsage(), 5000));
  53. timers.push(setInterval(() => gc(), 60000));
  54. return null;
  55. }).catch(function (err) {
  56. log('Could not setup the test. ' + err);
  57. });
  58. }
  59.  
  60. function setup(r) {
  61. log('-- Setup test');
  62. return r.dbCreate('test').run().then(function () {
  63. log('-- db created');
  64. }).catch(function () {
  65. log('-- db not created');
  66. }).finally(function () {
  67. return r.db('test').tableDrop('test').run()
  68. }).then(function () {
  69. log('-- table dropped');
  70. }).catch(function () {
  71. log('-- table not dropped');
  72. }).finally(function () {
  73. return r.db('test').tableCreate('test').run()
  74. }).then(function () {
  75. log('-- table created');
  76. }).catch(function () {
  77. log('-- table not created');
  78. }).finally(function () {
  79. return Promise.resolve(true);
  80. });
  81. }
  82.  
  83. function openFeeds(r, feeds) {
  84. printMemoryUsage();
  85. gc()
  86. printMemoryUsage();
  87. log('-- Open feeds');
  88. var count = 0;
  89. r.db('test').table('test').changes().run().then(function (feed) {
  90. feeds.push(feed);
  91. feed.eachAsync(function () {
  92. count++;
  93. }).catch(function (e) {
  94. log('Caught error?');
  95. log(e);
  96. });
  97. });
  98. }
  99.  
  100. function fireUpdates(r, timers) {
  101. log('-- Fire updates');
  102. for (var i = 0; i < INSERT_TIMERS; i++) {
  103. timers.push(setInterval(function () {
  104. countOps++;
  105. r.db('test').table('test').insert({
  106. a: "a".repeat(10),
  107. b: "b".repeat(10),
  108. c: "c".repeat(10),
  109. d: "d".repeat(10)
  110. }).run().then(function () {
  111. countOpsSuccess++;
  112. }).catch(function (e) {
  113. countOpsErrors++;
  114. });
  115. }, 1000));
  116. }
  117. for (var i = 0; i < DELETE_TIMERS; i++) {
  118. timers.push(setInterval(function () {
  119. r.db('test').table('test').limit(1).delete().run();
  120. }, 1000));
  121. }
  122. for (var i = 0; i < UPDATE_TIMERS; i++) {
  123. timers.push(setInterval(function () {
  124. r.db('test').table('test').limit(1).update({
  125. value: Math.random()
  126. }).run();
  127. }, 1000));
  128. }
  129. }
  130.  
  131. function tearDown(r, feeds, timers) {
  132. log(`-- countOps: ${countOps}`);
  133. log(`-- countOpsSuccess: ${countOpsSuccess}`);
  134. log(`-- countOpsErrors: ${countOpsErrors}`);
  135. log('-- Tear down');
  136. printMemoryUsage();
  137. gc();
  138. printMemoryUsage();
  139. for (var i = 0; i < timers.length; i++) {
  140. clearInterval(timers[i]);
  141. }
  142. Promise.map(feeds, function (feed) {
  143. return feed.close().catch(function (e) {
  144. log('Caught error?');
  145. log(e);
  146. });
  147. }).then(function () {
  148. log('-- Feed closed');
  149. feeds = [];
  150. timers = [];
  151. printMemoryUsage();
  152. gc()
  153. printMemoryUsage();
  154. // return r.getPoolMaster().drain();
  155. }).then(function () {
  156. setTimeout(function () {
  157. log('-- Pool master drained');
  158. printMemoryUsage();
  159. gc()
  160. printMemoryUsage();
  161. r = null;
  162. printMemoryUsage();
  163. log('-- countOps:', countOps);
  164. log('-- countOpsSuccess:', countOpsSuccess);
  165. log('-- countOpsErrors:', countOpsErrors);
  166. gc()
  167. printMemoryUsage();
  168. }, 5000);
  169. log('-- Pool master drained');
  170. printMemoryUsage();
  171. gc()
  172. printMemoryUsage();
  173. // r = null;
  174. printMemoryUsage();
  175. log('-- countOps:', countOps);
  176. log('-- countOpsSuccess:', countOpsSuccess);
  177. log('-- countOpsErrors:', countOpsErrors);
  178. gc()
  179. printMemoryUsage();
  180. // heapdump.writeSnapshot('./' + Date.now() + '.heapsnapshot');
  181. });
  182. }
  183.  
  184.  
  185. startTest();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement