-
Notifications
You must be signed in to change notification settings - Fork 0
/
shm-test.js
executable file
·130 lines (93 loc) · 2.69 KB
/
shm-test.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var shm = require('shm-typed-array')
var fs = require("fs")
var {spawn} = require('child_process')
var shmkey = 0;
// ------ ------ ------ ------ ------ ------ ------
//
var buf = false;
try {
shmkey = parseInt(process.argv[2]);
console.log(shmkey);
buf = shm.create(4096,'Buffer',shmkey)
} catch (e) {
buf = shm.create(4096)
}
//
console.log(typeof buf)
console.log('[Master] Typeof buf:', buf.constructor.name)
console.log(buf.key)
buf.write('buffer: this is a test')
console.log(buf.toString())
var logger = spawn(__dirname + '/clogger',[buf.key])
var firstrun = false
logger.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
if ( !firstrun ) {
firstrun = true
logger.emit('first-run')
}
});
logger.on('first-run',() => {
buf.writeUInt32LE(1)
buf.writeUInt32LE(1,4)
buf.writeUInt32LE(1,8)
var hrstart = process.hrtime()
for ( var i = 0; i < 11000000; i++ ) {
var date = Date.now()
var d1 = Math.floor(date/1000)
var d2 = date % 1000
buf.writeUInt32LE(d1)
buf.writeUInt32LE(d2,4)
buf.writeUInt32LE(i%8192,8)
buf.write('a',12,1,'ascii')
}
buf.writeUInt32BE(0)
buf.writeInt32BE(0,4)
buf.writeInt32BE(0,8)
const NS_PER_SEC = 1e9;
var hrend = process.hrtime(hrstart)
console.log(`etime: ${hrend[0] * NS_PER_SEC + hrend[1]}`);
console.log('secs: ' + (hrend[0] * NS_PER_SEC + hrend[1])/1000000000.0);
const used = process.memoryUsage();
for (let key in used) {
console.log(`${key} ${Math.round(used[key] / 1024 / 1024 * 100) / 100} MB`);
}
})
/*
if ( process.argv.length <= 2 ) {
console.log("not enough parameters");
process.exit(0);
}
console.log(process.argv);
// ------ ------ ------ ------ ------ ------ ------
var buf = shm.create(4096,'Buffer',shmkey)
console.log(typeof buf)
console.log('[Master] Typeof buf:', buf.constructor.name)
console.log(buf.key)
buf.write('buffer: this is a test')
console.log(buf.toString())
buf.writeUInt32LE(1)
buf.writeUInt32LE(1,4)
buf.writeUInt32LE(1,8)
var hrstart = process.hrtime()
for ( var i = 0; i < 11000000; i++ ) {
var date = Date.now()
var d1 = Math.floor(date/1000)
var d2 = date % 1000
buf.writeUInt32LE(d1)
buf.writeUInt32LE(d2,4)
buf.writeUInt32LE(i%8192,8)
buf.write('a',12,1,'ascii')
}
buf.writeUInt32BE(0)
buf.writeInt32BE(0,4)
buf.writeInt32BE(0,8)
const NS_PER_SEC = 1e9;
var hrend = process.hrtime(hrstart)
console.log(`etime: ${hrend[0] * NS_PER_SEC + hrend[1]}`);
console.log('secs: ' + (hrend[0] * NS_PER_SEC + hrend[1])/1000000000.0);
const used = process.memoryUsage();
for (let key in used) {
console.log(`${key} ${Math.round(used[key] / 1024 / 1024 * 100) / 100} MB`);
}
*/