-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.js
executable file
·157 lines (139 loc) · 5.39 KB
/
client.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
/*-----------------------------------------------------------------------------+
| Cluster/Async I/O Benchmark Version 1.0.0 |
+------------------------------------------------+----------------------------+
| Copyright 2014, Synthetic Semantics LLC | http://synsem.com/ |
| Copyright 2015-2016, Jace A Mogill | [email protected] |
| Released under the Revised BSD License | [email protected] |
+------------------------------------------------+----------------------------*/
'use strict';
var fs = require('fs');
var config = require('./config.json');
var http = require('http');
var cluster = require('cluster');
function Timer() {
this.stop = function () {
var hrTime = process.hrtime();
var now = hrTime[0] * 1000000 + hrTime[1] / 1000;
this.lastLap = now - this.timeStart;
this.elapsed += this.lastLap;
this.timeStart = now;
if (this.lastLap < this.min) this.min = this.lastLap;
if (this.lastLap > this.max) this.max = this.lastLap;
this.nCalls++;
};
this.start = function () {
var hrTime = process.hrtime();
this.timeStart = hrTime[0] * 1000000 + hrTime[1] / 1000;
};
this.min = 9999999999;
this.max = 0;
this.lastLap = 0;
this.nCalls = 0;
this.elapsed = 0;
this.start();
}
function fileN(iterationN) {
var nChimes = Math.floor(iterationN / config.readsPerWrite);
return ("00000"+(nChimes % config.nFiles)).slice(-5);
}
function doPost(iterationN, itersRemaining, keepAliveAgent) {
writeTimer.start();
var request = http.request({
agent: keepAliveAgent,
host: config.serverHostname,
port: config.serverPort,
path: '/put?key=' + "file" + fileN(iterationN),
method: 'POST'
}, function (res) {
var responseString = '';
res.on('data', function (data) {
responseString += data;
});
res.on('end', function (result) {
if (responseString != 'success') {
console.log('post was not successful?', result, responseString)
} else {
writeTimer.stop();
// console.log('PUT: iter=', iterationN, ' str=', responseString, ' len=', responseString.length);
rwLoop(iterationN + 1, itersRemaining - 1, keepAliveAgent);
}
});
});
request.on('error', function (err) {
console.log('doPost: http request failed:', err);
process.exit(1);
});
request.write(config.value + "(procN = " + process.env.processN +
" fileN = " + fileN(iterationN) + " iterationN = " + iterationN + ")\n");
request.end();
}
function doGet(iterationN, itersRemaining, keepAliveAgent) {
readTimer.start();
var get = http.get({
agent: keepAliveAgent,
host: config.serverHostname,
port: config.serverPort,
path: '/get?key=' + "file" + fileN(iterationN)
}, function (response) {
var responseString = '';
response.on('data', function (data) {
responseString += data;
});
response.on('end', function () {
readTimer.stop();
// console.log('GET: iter=', iterationN, ' str=', responseString, ' len=', responseString.length);
rwLoop(iterationN + 1, itersRemaining - 1, keepAliveAgent);
});
response.on('error', function (err) {
console.log('http get error:', err)
})
});
get.on('error', function (e) {
console.log("Got error: " + e.message);
});
get.end();
}
function rwLoop(iterN, itersRemaining, keepAliveAgent) {
if (itersRemaining > 0) {
/*
if (iterN % 500 === 0) {
console.log("Process " + process.env.processN + " performed iteration " + iterN);
}
*/
if (iterN % (config.readsPerWrite + 1) == 0) {
doPost(iterN, itersRemaining, keepAliveAgent);
} else {
doGet(iterN, itersRemaining, keepAliveAgent);
}
} else {
var writeMean = writeTimer.elapsed / writeTimer.nCalls;
var readMean = readTimer.elapsed / readTimer.nCalls;
console.log('Process ' + cluster.worker.id + ' PUT, ' + writeTimer.min + ', ' + writeMean + ', ' + writeMean * 1.01 + ', ' + writeTimer.max +
'\nProcess ' + cluster.worker.id + ' GET, ' + readTimer.min + ', ' + readMean + ', ' + readMean * 1.01 + ', ' + readTimer.max);
cluster.worker.disconnect();
}
}
/*
*
*/
if (cluster.isMaster) {
try { fs.mkdirSync(config.dataPath); }
catch(err) { }
console.log('PUT/GET, Min, Mean, 101% of Mean, Max');
for (var i = 0; i < config.nClients; i++)
cluster.fork({'cluster_bench_task':'client', 'processN':i});
/*
cluster.on('exit', function (worker, code, signal) {
// console.log('worker ' + worker.process.pid + ' exited');
});
*/
} else {
var keepAliveAgent = new http.Agent({ keepAlive: true });
var writeTimer = new Timer();
var readTimer = new Timer();
var itersPerProc = Math.floor(config.nOperations / config.nClients);
var startIter = itersPerProc * process.env.processN;
// console.log("Synchornous client ", cluster.worker.process.pid, " (", process.env.processN, ") starting iter=", startIter, " remaining:", itersPerProc);
rwLoop(startIter, itersPerProc, keepAliveAgent);
}