forked from doowb/composer
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
106 lines (91 loc) · 2.43 KB
/
run.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
'use strict';
const define = require('define-property');
const nano = require('nanoseconds');
const time = require('pretty-time');
/**
* `Run` represents a single execution of a `Task`.
*
* @param {Number} `id` identifier of this run.
* @api public
*/
class Run {
constructor(id) {
this.runId = id;
this.date = {};
this.hr = {};
/**
* Calculate the difference between the `start` and `end` hr times in nanoseconds.
*
* @api public
* @name hr.diff
*/
define(this.hr, 'diff', {
enumerable: true,
configurable: true,
get: function() {
return nano(this.end) - nano(this.start);
}
});
/**
* Calculate the offset between the hr `duration` and hr `diff` properties in nanoseconds.
* This may be needed because `duration` is called with `process.hrtime(hr.start)` after `hr.end`
* is calculated using `process.hrtime()`.
*
* @api public
* @name hr.offset
*/
define(this.hr, 'offset', {
enumerable: true,
configurable: true,
get: function() {
return nano(this.duration) - this.diff;
}
});
}
/**
* Start recording the run times. This will save the start date on `run.date.start` and the start hr time on `run.hr.start`
*/
start() {
this.status = 'starting';
this.date.start = new Date();
this.hr.start = process.hrtime();
}
/**
* Stop recording the run times. This will save the end hr time on `run.hr.end`,
* calculate the duration using `process.hrtime(run.hr.start)`,
* and save the end date on `run.date.end`
*
* `end` is calculated before `duration` causing `duration` to be approximately 10,000 nanoseconds off.
* See `offset` for actual `offset`
*/
end() {
this.hr.end = process.hrtime();
this.duration = process.hrtime(this.hr.start);
this.date.end = new Date();
this.status = 'finished';
}
/**
* Formatted duration using [pretty-time][]. This is the duration from
* [hr.duration](#hrduration) formatted into a nicer string. If `hr.duration`
* is undefined, then an empty string is returned.
*
* @api public
* @name duration
*/
set duration(val) {
this.hr.duration = val;
}
get duration() {
if (this.hr.duration) {
return time(this.hr.duration);
}
if (this.hr.start) {
return time(process.hrtime(this.hr.start));
}
return '';
}
}
/**
* Expose `Run`
*/
module.exports = Run;