-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (54 loc) · 2.06 KB
/
index.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
#!/usr/bin/env node
'use strict';
const { Guber } = require('./data/index');
const rulesEngine = require('./lib/index');
const chalk = require('chalk');
// Generating fake data for rules engine. To capture performance we're going use hrtime (node's process high resolution time)
let time = process.hrtime();
const payloadSize = 10000;
const payload = Guber.generateFakePayload(payloadSize);
let diff = process.hrtime(time);
console.log(`Generated ${chalk.green(payload.length)} unique records took ${chalk.green((diff[0]*1000) + (diff[1] / 1000000))} ms`);
// End of generating fake data
//Function captures metrics from rules engine
function Metrics(results) {
Metrics.results.push(results);
}
//Static properties
Metrics.results = [];
//Invoke validate and capture the resulting metrics
const validateRecord = async (facts) => {
Metrics(await rulesEngine.validate(facts));
};
//Custom async forEach function
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index]);
}
}
//Iterates over records then print results (including metrics)
const init = async (payload) => {
console.log();
console.log(`Starting process to run ${chalk.green(payloadSize)} records through the rules engine...`);
let time = process.hrtime();
await asyncForEach(payload, validateRecord);
//console.log(` Results: ${JSON.stringify(Metrics.results, null, 2)}`);
console.log(`Process complete!`);
let diff = process.hrtime(time);
const totalNumberOfRecords = Metrics.results.length;
let passed = 0;
Metrics.results.forEach((result) => {
const { updated, fired, elapsed } = result;
if (updated && updated.length > 0){
passed++;
}
});
console.log('');
console.log(`Processing of ${chalk.green(totalNumberOfRecords)} records took ${chalk.green((diff[0]*1000) + (diff[1] / 1000000))} ms`);
console.log(` NOTE: ${chalk.green(passed)} passed the rules`);
}
//Main entry point
init(payload);
process.on('unhandledRejection', error => {
console.log('unhandledRejection', error.message);
});