UNPKG

2.21 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2012 Dmitri Melikyan
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to permit
9 * persons to whom the Software is furnished to do so, subject to the
10 * following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24var nt = require('./nodetime');
25
26function Time(scope, command, isMacro) {
27 var nt = global.nodetime;
28
29 this.scope = scope;
30 this.command = command;
31 this.isMacro = isMacro;
32
33 this.id = nt.nextId++;
34
35 this._begin = undefined;
36 this._cputime = undefined;
37
38 this.begin = undefined;
39 this.end = undefined;
40 this.ms = undefined;
41 this.cputime = undefined;
42};
43exports.Time = Time;
44
45
46Time.prototype.start = function() {
47 var nt = global.nodetime;
48
49 this.begin = nt.millis();
50 this._cputime = nt.cputime();
51 this._begin = nt.hrtime();
52
53 var self = this;
54 process.nextTick(function() {
55 try {
56 nt.emit("call", "start", self);
57 }
58 catch(err) {
59 nt.error(err);
60 }
61 });
62};
63
64
65Time.prototype.done = function() {
66 var nt = global.nodetime;
67
68 if(this.end) return false;
69
70 this.ms = (nt.hrtime() - this._begin) / 1000;
71 if(this._cputime !== undefined) this.cputime = (nt.cputime() - this._cputime) / 1000;
72 this.end = nt.millis();
73
74 var self = this;
75 process.nextTick(function() {
76 nt.emit("call", "done", self);
77 });
78
79 return true;
80};
81
82