-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.js
More file actions
277 lines (231 loc) · 10.4 KB
/
Copy pathmain.js
File metadata and controls
277 lines (231 loc) · 10.4 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const ConnectionManager = require('./connection_manager');
const ProgressBar = require('./progress_bar');
const FileManager = require('./file_manager');
const Results = require('./results');
const readline = require('readline');
const WebSocketClient = require('websocket').client;
class Benchmarker {
/**
* Initializes all the data that will be needed throughout the program
*
* We initialize data that will be shared between mutliple files as objects, becuase
* objects will be passed as reference, where a primitve variable would be passed by value
*/
constructor() {
/**
* The folder where the benchmark results will be saved. This will be set once the user is prompted for
* the current language being tested
* @type {string}
*/
this.benchmark_folder = "";
/**
* The base directory where all benchmark results are stored
* @type {string}
*/
this.benchmark_results_directory = process.env.BENCHMARK_FOLDER || "./benchmarks";
/**
* The language which is being benchmarked. This can be pulled from an environment variable for easy
* automation with Docker, or can be entered manually by the user if an environment variable is not set
* @type {string | undefined}
*/
this.benchmark_language = process.env.BENCHMARK_LANGUAGE || undefined;
/**
* An object storing data on the connections currently being made each round
* {
* counter: {number} the number of clients currently created each round,
* total: {number} the total number of clients expected to me created each round,
* message: {string} the message to output before starting the connection process
* }
* @type {Object}
*/
this.connection_progress_obj = {
counter: 0,
total: 0,
message: "Connecting..."
};
/**
* An object storing data on all the requests currently being made each round
* {
* counter: {number} the number of requests currently completed each round,
* total: {number} the total number of requests expected to me completed each round,
* message: {string} the message to output before starting the benchmarking process
* }
* @type {Object}
*/
this.benchmark_progress_obj = {
counter: 0,
total: 0,
message: "Benchmarking..."
};
/**
* An object storing websocket client connections and connection data
* {
* connection_time: {number} the total time it took for all the clients to connect each round
* times: {Array}, time data produces by each client for each request to the websocket server
clients: {Array} list of all connected clients
* }
* @type {Object}
*/
this.connection_obj = {
connection_time: 0,
times: [],
clients: []
};
/**
* An object storing data that each client will need to connect to the benchmark server, and send requests
* {
* websocket_address {string} IP address of the websocket server to connect to
* websocket_port: {number} Port number of the websocket to connect to
* connection_interval: {number} The number of websocket connections to add each round
* request_interval: {number} The number of requests to sound out per connected client per round
* request_timeout: {number} The number of minutes to wait before abandoning a request
* }
* @type {Object}
*/
this.benchmark_obj = {
websocket_address: process.env.WEBSOCKET_ADDRESS || "127.0.0.1",
websocket_port: process.env.WEBSOCKET_PORT || 8080,
connection_interval: process.env.ADD_CONNECTIONS || 100,
request_interval: process.env.REQUESTS || 50,
};
/**
* Instance of the FileManager. This will be set once the user is prompted for
* the current language being tested
* @type {module.FileManager}
*/
this.fm = null;
/**
* Instance of the Results class. This will be set once the user is prompted for
* the current language being tested, as it requires the instance of the FileManager
* @type {module.Results}
*/
this.result = null;
/**
* Instance of the ConnectionManager class
* @type {module.ConnectionManager}
*/
this.cm = new ConnectionManager(this.benchmark_obj, this.connection_obj, this.connection_progress_obj, this.benchmark_progress_obj);
/**
* The number of rounds to perform per test
* @type {number}
*/
this.ROUNDS = process.env.ROUNDS || 25;
}
/**
* Prompts the user at the beginning of the application for the current language being benchmarked
* @returns {void}
*/
prompt() {
// allows this to be used inside nested functions
let self = this;
// create a readline interface for console input/output
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// continue running the programming asynchronously
let manage_file = async function() {
// assign the benchmark folder by conjoining the benchmark results root directory with the language name
self.benchmark_folder = self.benchmark_results_directory + '/' + self.benchmark_language + "/";
// create the FileManager and Results instances
self.fm = new FileManager(self.benchmark_folder);
self.result = new Results(self.fm, self.benchmark_obj.request_interval);
// close the readline interface
rl.close();
// create the file where the benchmark results will be saved to
self.fm.createFile();
// benchmark the rest of the program
await self.run_program();
};
if(this.benchmark_language === undefined){
// prompt the user for the language being benchmarked and wait for a response
rl.question('Language: ', async (benchmark_language) => {
self.benchmark_language = benchmark_language;
manage_file();
});
}else{
manage_file();
}
}
/**
* Loops through the benchmarking process for the given number of rounds,
* then closed the websocket connections
* @return {Promise<void>}
*/
async run_program() {
// for the number of given rounds, perform the benchmarking process, waiting for each round to finish before
// continuing
for (let i = 0; i < this.ROUNDS; i++) {
console.log("\nTest: " + (i + 1) + "/" + this.ROUNDS);
await this.benchmark(i);
}
// once all round have been completed, close the websocket connections
await this.cm.close();
}
/**
* Performs the benchmarking process
* @param round {number} The current iteration count of the round being performed
* @return {Promise} resolves once the round of the benchmarking process is complete
*/
async benchmark(round) {
return new Promise(async (resolve, reject) => {
try {
// determine the total number of expected connections
// REQUEST_INTERVAL * ROUND_NUMBER
this.connection_progress_obj.total = (round + 1) * this.benchmark_obj.connection_interval;
// start the connection progress bar
let connection_bar = new ProgressBar(this.connection_progress_obj);
connection_bar.start();
// begin the connection process, and wait for it to finish
await this.cm.createConnections(round);
// finalize the progress bar, and stop it from updating any further
this.connection_progress_obj.bar.update(this.connection_progress_obj.counter);
connection_bar.stop();
// output to the conole the time elapse for the new connections to connect
console.log("\nConnection Time: " + this.connection_obj.connection_time);
// start the benchmarking progress bar
this.benchmark_progress_obj.total = this.benchmark_obj.request_interval * this.connection_obj.clients.length;
let benchmark_bar = new ProgressBar(this.benchmark_progress_obj);
benchmark_bar.clear();
benchmark_bar.start();
// start the benchmarking process, and wait for it to finish
await this.cm.sendRequests(round);
// stop the progress bar from updating any further
benchmark_bar.stop();
// calculate the results for the current round of benchmarking
await this.result.calculate(this.connection_obj);
// resolve when done
resolve();
} catch (error) {
reject(error);
}
});
}
/**
* Checks if there is a websocket server accepting connections on the given IP and Port. If no server found,
* the application terminates
*
* @return {Promise} Resolves when a connection is made to the websocket server, otherwise terminates
*/
async serverCheck(){
// connect to the websocket server
let url = "ws://"+this.benchmark_obj.websocket_address+":"+this.benchmark_obj.websocket_port;
let client = new WebSocketClient();
client.connect(url);
return new Promise(async (resolve, reject) => {
// terminate the program is the connection is unsuccessful
client.on('connectFailed', function (error) {
console.log("Server Not Found");
process.exit();
});
// resolve on a successful connection
client.on('connect', function (connection) {
connection.close();
resolve();
});
});
}
}
let benchmarker = new Benchmarker();
let check_server = async function(){await benchmarker.serverCheck()};
check_server().then( () => { benchmarker.prompt(); });