forked from fluture-js/Fluture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-mem
executable file
·101 lines (81 loc) · 2.27 KB
/
test-mem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
/* global process setImmediate */
var Future = require('..');
var {log} = require('util');
var start = Date.now();
var batch = 0;
var stamp = Date.now();
function report(){
var memMB = process.memoryUsage().rss / 1048576;
var now = Date.now();
var passed = now - stamp;
batch = batch + 1;
if(passed >= 5000){
log(
'-BATCH:', batch,
'-OPS:', Math.round(batch / ((now - start) / passed) / (passed / 1000)),
'-MEM:', memMB, 'MB'
);
stamp = now;
}
}
var sync = Future.of;
function async(x){
return Future(function(l, r){ setImmediate(r, x) });
}
var cases = Object.create(null);
//Should infinitely run until finally running out of memory.
cases.syncHeadRecursion = function recur(){
report();
return sync('l').chain(recur).race(sync('r'));
};
//Should immediately exit with "l".
cases.syncDeepRecursion = function recur(){
report();
return sync('l').race(sync('r').chain(recur));
};
//Should infinitely run without any problems.
cases.syncTailRecursion = function recur(){
report();
return sync('l').race(sync('r')).chain(recur);
};
//Should immediately exit with "r".
cases.asyncHeadRecursion = function recur(){
report();
return async('l').chain(recur).race(async('r'));
};
//Should immediately exit with "l".
cases.asyncDeepRecursion = function recur(){
report();
return async('l').race(async('r').chain(recur));
};
//Should infinitely run without any problems.
cases.asyncTailRecursion = function recur(){
report();
return async('l').race(async('r')).chain(recur);
};
//Expected to run out of memory.
cases.debugModeSyncTailRecursion = function(){
Future.debugMode(true);
return cases.syncTailRecursion();
};
//Expected to run out of memory.
cases.debugModeAsyncTailRecursion = function(){
Future.debugMode(true);
return cases.asyncTailRecursion();
};
var f = cases[process.argv[2]];
if(typeof f !== 'function'){
console.log('Usage:\n\n test-mem <case>\n\nPossible cases:\n');
Object.keys(cases).forEach(function(k){console.log(` ${k}`)});
process.exit(1);
}
log('PID', process.pid);
var cancel = f().fork(
function(e){console.error(e.stack); process.exit(1)},
function(v){log('resolved', v); process.exit(2)}
);
process.once('SIGINT', () => {
log('SIGINT caught. Cancelling...');
cancel();
});