speedtest.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const path = require('path');
  2. const async = require('async');
  3. const lf = require('lockfile');
  4. const fs = require('fs');
  5. const n = +process.argv[3] || 300;
  6. const a = Array.apply(null, {length: n}).map(function(_, i) {
  7. return i
  8. })
  9. const file = path.resolve(__dirname, 'speed-test.lock');
  10. try{
  11. fs.unlinkSync(file);
  12. }
  13. catch(e){}
  14. /// NOTE: this should run in about 30ms on a SSD Ubuntu 16.04, that is fast, because we are locking/unlocking 300 locks
  15. /// *HOWEVER* if we change async.eachSeries to async.each, lockfile will barf immediately, and I can't get lockfile
  16. /// to not barf, using any of the options {} available to lockfile#lock.
  17. const parallel = process.argv[2] === 'parallel';
  18. var fn, msg;
  19. if(parallel){
  20. msg = 'parallel';
  21. fn = async.each;
  22. }
  23. else{
  24. msg = 'series';
  25. fn = async.eachSeries;
  26. }
  27. const start = Date.now();
  28. console.log(' => locking/unlocking ' + a.length + ' times, in ' + msg);
  29. fn(a, function (val, cb) {
  30. console.log('try %d', val)
  31. lf.lock(file, { retries: n * 3 }, function (err) {
  32. if (err) {
  33. cb(err);
  34. }
  35. else {
  36. console.log('complete %d', val)
  37. lf.unlock(file, cb);
  38. }
  39. });
  40. }, function complete(err) {
  41. if (err) {
  42. throw err;
  43. }
  44. console.log(' => Time required for lockfile => ', Date.now() - start, 'ms');
  45. process.exit(0);
  46. });