-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
181 lines (160 loc) · 5.47 KB
/
app.js
File metadata and controls
181 lines (160 loc) · 5.47 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//TODO: tests
//abstract database class (for other database implementations)
var cassandraUtility = require('./cassandraUtility.js'),
insertTracker = require('./insertTracker.js'),
config = require('./config.js'),
logger = require('./logger.js')(config.debugLevel),
dataGenerator = require('./dataGenerator.js'),
Query = require('./myQuery.js'),
query = null,
cluster = require('cluster'),
numCPUs = require('os').cpus().length,
start = new Date(),
failureCount = 0,
myId = 0,
statisticsTimer = null,
workers = 0,
completed = 0,
MESSAGE = {
INCREMENTAL: 'incremental',
SUMMARY: 'summary',
UPDATE: 'update'
},
summaryStats = null,
statsLogged = false, //helper variable
workerStatsSummary = "";
/**
* gathers statistics on increment basis
**/
function gatherIncrementalStats (isMaster) {
insertTracker.getIncrementStats(function (stats) {
if (isMaster) {
logger.info("AverageTimeForInterval: " + stats.average + ", CountForInterval: " + stats.count);
} else {
process.send({
type: MESSAGE.INCREMENTAL,
worker: process.env,
details: "CurrentWorkers: " + workers + ", TotalCompleted: " + completed + ", AverageTimeForInterval: " + stats.average + ", CountForInterval: " + stats.count
});
}
}); //resets stats and gets current stats
}
/**
* teardown...
**/
function cleanup () {
var end = new Date();
if (statisticsTimer) { clearInterval(statisticsTimer); }
//close pool
cassandraUtility.doPoolClose();
//send summary statistics to master
process.send({
type: MESSAGE.SUMMARY,
worker: process.env.NODE_UNIQUE_ID,
details: "\ntest ran for: " + (end.getTime() - start.getTime() +
"\nNumber of Records: " + insertTracker.getTimes() +
"\naverage time: " + insertTracker.getAverageTime()),
failures: failureCount,
successes: insertTracker.getTimes()
});
process.exit();
}
/**
* this method is always running, until the duration of the test is completed
**/
function doForDuration () {
var shuttingDown = false;
//run this for the alloted duration
var currentTime = new Date();
if (start.getTime() + config.duration <= currentTime.getTime()) {
if (workers <= 0) {
cleanup();
return;
} else {
shuttingDown = true;
}
}
if (workers < config.maxWorkers && !shuttingDown) {
//this for loop takes you right to the max number of concurrent workers
for (i=0;i<(config.maxWorkers-workers);i++) {
workers++;
completed++;
cassandraUtility.query(query.getQuery(),
params = query.getParams(completed), //each core has its own myId and the ts plus the count of completed
function (error, timeTaken) {
workers--;
if (error) {
failureCount++;
} else {
//log for my instance
insertTracker.addTime(timeTaken);
//and for all instances
process.send({
'type': MESSAGE.UPDATE,
'timeTaken': timeTaken
});
}
}
);
}
}
//process.nextTick was giving me 100% cpu
//process.nextTick(function () { doForDuration(); });
setTimeout(function () { doForDuration(); }, 1);
}
/**
* bootstrap method
**/
function main (processId) {
//create the connection pool
cassandraUtility.doPoolConnect(function () { logger.debug("connected"); });
//create this worker's query object
query = new Query(processId);
//start running
doForDuration();
//start gathering statistics
statisticsTimer = setInterval(function () { gatherIncrementalStats(); }, config.statisticInterval);
}
//go!
if (cluster.isMaster) {
//get stats incrementally
statisticsTimer = setInterval(function () { gatherIncrementalStats(true); }, config.statisticInterval);
logger.info("This test will run for (ms): " + config.duration);
logger.info("spinning up " + numCPUs + " instances");
// Fork instances.
for (var i = 0; i < numCPUs; i++) {
cluster.fork({ instanceId: i }); //pass a unique id to all workers because the env it should have is missing (windows)
}
cluster.on('exit', function(worker, code, signal) {
var countWorkers = 0;
for (var id in cluster.workers) {
countWorkers++
}
logger.debug('Thread ' + worker.process.pid + ' terminated');
logger.debug(countWorkers + " workers left");
if (countWorkers === 0 && !statsLogged) {
statsLogged = true;
clearInterval(statisticsTimer);
console.log("------------------------------------------");
logger.info("Average Time Across Cluster : " + insertTracker.getAverageTime());
logger.info("Total Inserts Across Cluster: " + insertTracker.getTimes().length);
logger.info("Test Ran For (Milliseconds) : " + ((new Date()).getTime() - start.getTime()));
logger.info("worker stats: " + workerStatsSummary);
}
});
//accept messages from workers.
Object.keys(cluster.workers).forEach(function(id) {
cluster.workers[id].on('message', function (msg) {
if (msg && msg.type === MESSAGE.INCREMENTAL) {
logger.debug("Thread: " + msg.worker + " : " + msg.details);
} else if (msg && msg.type === MESSAGE.SUMMARY) {
//a node just sent it's summary...
workerStatsSummary += "\n\tworker finishing, failures: " + msg.failures + ", successes: " + msg.successes.length;
} else if (msg && msg.type === MESSAGE.UPDATE) {
insertTracker.addTime(msg.timeTaken);
}
});
});
} else {
main(process.env['instanceId']);
}