-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrumentation_server.js
141 lines (114 loc) · 4.03 KB
/
instrumentation_server.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
/**
* @description Server that gets called by all all alive functions.
* The server will create an unique set of _alive_functions.json
*
* @version 0.1
* @author Kishan Nirghin
* @Date 07-02-2019
*
* @param <instrumentedSourceFolder> the FOLDER of the instrumented source.
*/
'use strict';
const express = require("express"),
path = require("path"),
fs = require("fs"),
bodyParser = require("body-parser");
const { check } = require('express-validator/check');
const app = express()
const port = 8004
const fileName = path.basename(__filename);
if(process.argv.length < 3) {
console.log(`usage: node ${fileName} <instrumented_sourcefolder>`);
process.exit(1);
}
var instrumentedSourceLocation = process.argv[2];
var aliveFunctionsPath = path.join(instrumentedSourceLocation, "_alive_functions.json");
if (!fs.existsSync(aliveFunctionsPath)) {
fs.writeFileSync(aliveFunctionsPath, "[]", 'utf8'); // default
}
var allFunctionsPath = path.join(instrumentedSourceLocation, "_all_functions.json");
var deadFunctionsPath = path.join(instrumentedSourceLocation, "_dead_functions.json");
var statisticsPath = path.join(instrumentedSourceLocation, "_statistics.json");
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: true
}));
// parse application/json
app.use(bodyParser.json())
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.static(instrumentedSourceLocation));
/**
* An alive function will call this method by default.
*/
app.post('/alivefunction', (req, res) => {
var func = req.body;
if (func) {
/* Fancy way to prevent duplicates in the alive functions file */
var result = JSON.parse(fs.readFileSync(aliveFunctionsPath, 'utf8'));
var resultObj = convertToResultObj(result);
var funcId = getFuncId(func);
resultObj[funcId] = func;
result = Object.values(resultObj);
fs.writeFileSync(aliveFunctionsPath, JSON.stringify(result), 'utf8');
updateStatistics();
}
res.send(JSON.stringify(req.body));
});
app.listen(port, () => {
console.log(`Instrumentation_server listening on port ${port}!`);
console.log("============== See function results here ==============");
console.log(`[All] \t ./${allFunctionsPath}`);
console.log(`[Alive] \t ./${aliveFunctionsPath}`);
console.log(`[Dead] \t ./${deadFunctionsPath}`);
console.log(`[Stats] \t ./${statisticsPath}`);
console.log("=======================================================");
});
function convertToResultObj(result){
var resultObj = {};
result.forEach((func) => {
var funcId = getFuncId(func);
resultObj[funcId] = func;
});
return resultObj;
}
function updateStatistics() {
var aliveFunctions = JSON.parse(fs.readFileSync(aliveFunctionsPath, 'utf8'));
var allFunctions = JSON.parse(fs.readFileSync(allFunctionsPath, 'utf8'));
var deadFunctions = [];
allFunctions.forEach((func) => {
if (!isAlive(aliveFunctions, func)) {
deadFunctions.push(func);
}
});
var statistics = {
numberOfAliveFunctions: aliveFunctions.length,
numberOfFunctions: allFunctions.length,
numberOfDeadFunctions: deadFunctions.length
};
console.log(statistics);
fs.writeFileSync(deadFunctionsPath, JSON.stringify(deadFunctions), 'utf8');
fs.writeFileSync(statisticsPath, JSON.stringify(statistics), 'utf8');
}
/**
* Helper method
* Uses the aliveFunctions global variable
*/
function isAlive(aliveFunctions, func) {
var isAlive = false;
aliveFunctions.forEach((aliveFunc) => {
if (getFuncId(func) == getFuncId(aliveFunc)) { return isAlive = true; }
});
if (isAlive) { return func}
return isAlive;
}
/**
* Computes an unique function Id for every function
* @param {*} func
*/
function getFuncId(func) {
return func.file + func.range[0];
}