bench-insert-select.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var common = require('../test/common');
  2. var connection = common.createConnection();
  3. var assert = require('assert');
  4. var table = 'insert_test';
  5. var text = "本日は晴天なり";
  6. connection.query([
  7. 'CREATE TEMPORARY TABLE `' + table + '` (',
  8. '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,',
  9. '`title` varchar(255),',
  10. 'PRIMARY KEY (`id`)',
  11. ') ENGINE=InnoDB DEFAULT CHARSET=utf8'
  12. ].join('\n'));
  13. function benchmarkInsert(numLeft, callback) {
  14. connection.query('INSERT INTO ' + table + ' SET title="' + text + '"', function(err, result) {
  15. if (err) throw err;
  16. if (numLeft > 1)
  17. benchmarkInsert(numLeft-1, callback);
  18. else
  19. callback();
  20. });
  21. }
  22. function benchmarkInserts(n, cb) {
  23. var numInsert = 10000;
  24. var start = process.hrtime();
  25. benchmarkInsert(numInsert, function() {
  26. var end = process.hrtime();
  27. var diff = common.hrdiff(start, end);
  28. console.log(numInsert*1e9/diff + ' inserts/sec');
  29. if (n > 1)
  30. benchmarkInserts(n - 1, cb);
  31. else
  32. cb();
  33. });
  34. }
  35. function benchmarkSelect(numLeft, numSelect, callback) {
  36. connection.query('select * from ' + table + ' limit ' + numSelect, function(err, result) {
  37. if (err) throw err;
  38. if (numLeft > 1)
  39. benchmarkSelect(numLeft-1, numSelect, callback);
  40. else
  41. callback();
  42. });
  43. }
  44. function benchmarkSelects(n, size, cb) {
  45. var numSelects = 100;
  46. var start = process.hrtime();
  47. benchmarkSelect(numSelects, size, function() {
  48. var end = process.hrtime();
  49. var diff = common.hrdiff(start, end);
  50. console.log(size + ' rows: ' + numSelects*1e9/diff + ' results/sec, ' + size*numSelects*1e9/diff + ' rows/sec');
  51. if (n > 1)
  52. benchmarkSelects(n - 1, size, cb);
  53. else
  54. cb();
  55. });
  56. }
  57. module.exports = function(done) {
  58. var testStart = process.hrtime();
  59. benchmarkInserts(5, function() {
  60. benchmarkSelects(5, 10000, function() {
  61. benchmarkSelects(10, 1000, function() {
  62. benchmarkSelects(2, 50000, function() {
  63. var testEnd = process.hrtime();
  64. console.log('total time: ', common.hrdiff(testStart, testEnd)/1e9 );
  65. connection.end();
  66. if (done)
  67. done();
  68. });
  69. });
  70. });
  71. });
  72. };
  73. if (require.main === module) {
  74. module.exports();
  75. }