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