File: analyze.js

package info (click to toggle)
node-mysql 2.18.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,740 kB
  • sloc: makefile: 4
file content (98 lines) | stat: -rw-r--r-- 1,877 bytes parent folder | download | duplicates (3)
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
var script = process.cwd() + '/' + process.argv[2];
var spawn  = require('child_process').spawn;

var numbers       = [];
var boringResults = 0;
var scriptRuns    = 0;

function runScript() {
  scriptRuns++;

  var child = spawn(process.execPath, [script]);

  var buffer = '';
  child.stdout.on('data', function(chunk) {
    buffer += chunk;

    var offset;
    while ((offset = buffer.indexOf('\n')) > -1) {
      var number = parseInt(buffer.substr(0, offset), 10);
      buffer = buffer.substr(offset + 1);

      var maxBefore = max();
      var minBefore = min();

      numbers.push(number);

      if (maxBefore === max() && minBefore === min()) {
        boringResults++;
      }

      if (boringResults > 10) {
        boringResults = 0;
        child.kill();
        runScript();
      }
    }
  });
}

function report() {
  console.log(
    'max: %s | median: %s | sdev: %s | last: %s | min: %s | runs: %s | results: %s',
    max(),
    median(),
    sdev(),
    numbers[numbers.length - 1],
    min(),
    scriptRuns,
    numbers.length
  );
}

function min() {
  if (!numbers.length) return undefined;

  return numbers.reduce(function(min, number) {
    return (number < min)
      ? number
      : min;
  });
}

function max() {
  if (!numbers.length) return undefined;

  return numbers.reduce(function(max, number) {
    return (number > max)
      ? number
      : max;
  });
}

function median() {
  return numbers[Math.floor(numbers.length / 2)];
}

function sdev() {
  if (!numbers.length) return undefined;

  return Math.round(Math.sqrt(variance()));
}

function variance() {
  var t       = 0;
  var squares = 0;
  var len     = numbers.length;

  for (var i = 0; i < len; i++) {
    var obs = numbers[i];
    t += obs;
    squares += Math.pow(obs, 2);
  }
  return (squares / len) - Math.pow(t / len, 2);
}

setInterval(report, 1000);

runScript();