-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauxfunctions.js
83 lines (67 loc) · 2.14 KB
/
auxfunctions.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
//auxfunctions1718
// *** getLoad function
function getLoad() {
var fs = require('fs')
, data = fs.readFileSync("/proc/loadavg") // version sincrona
, tokens = data.toString().split(' ')
, min1 = parseFloat(tokens[0])+0.01
, min5 = parseFloat(tokens[1])+0.01
, min15 = parseFloat(tokens[2])+0.01
, m = min1*10 + min5*2 + min15;
return m;
}
// *** randNumber function
function randNumber(upper, extra) {
var num = Math.abs(Math.round(Math.random() * upper));
return num + (extra || 0);
}
// *** randTime function
function randTime(n) {
return Math.abs(Math.round(Math.random() * n)) + 1;
}
// *** showMessage function
function showMessage(msg) {
for(var i = 0; i < msg.length; i++){
console.log(' Segment %d: %s', i, msg[i]);
}
}
function convertArrayToString(a){
var res = "{";
for(var i = 0; i < a.length - 1; i++)
if(a[i] == "")
res += '"",';
else
res += a[i] + ",";
return res + a[a.length -1] + "}";
}
// *** ordered list functions for workers management
// list has an "ordered" property (true|false) and a data property (array)
// data elements consists of pairs {id, load}
function orderedList() {
return {ordered: true, data: []};
}
function nonempty() {
return (this.data.length > 0);
}
function lowest() { // ordered reads
if (!this.ordered) {
this.data.sort(function(a, b) {
return parseFloat(b.load) - parseFloat(a.load);
})
}
this.ordered = true;
return this.data.shift();
}
function insert(k) { // unordered writes
this.ordered = false;
this.data.push(k);
}
module.exports.getLoad = getLoad;
module.exports.randNumber = randNumber;
module.exports.randTime = randTime;
module.exports.showMessage = showMessage;
module.exports.orderedList = orderedList;
module.exports.nonempty = nonempty;
module.exports.lowest = lowest;
module.exports.insert = insert;
module.exports.convertArrayToString = convertArrayToString;