1 | | /*! |
2 | | * |
3 | | * Copyright (c) 2013 Sebastian Golasch |
4 | | * |
5 | | * Permission is hereby granted, free of charge, to any person obtaining a |
6 | | * copy of this software and associated documentation files (the "Software"), |
7 | | * to deal in the Software without restriction, including without limitation |
8 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
9 | | * and/or sell copies of the Software, and to permit persons to whom the |
10 | | * Software is furnished to do so, subject to the 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 MERCHANTABILITY, |
17 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21 | | * DEALINGS IN THE SOFTWARE. |
22 | | * |
23 | | */ |
24 | | |
25 | 1 | 'use strict'; |
26 | | |
27 | | /** |
28 | | * Initializes the timers default values |
29 | | * |
30 | | * @constructor |
31 | | * @class Timer |
32 | | */ |
33 | | |
34 | 1 | var Timer = function () { |
35 | 7 | this.timerData = [0, 0]; |
36 | 7 | this.interval = [0, 0]; |
37 | | }; |
38 | | |
39 | | /** |
40 | | * Timing module to measure test run times |
41 | | * |
42 | | * @module DalekJS |
43 | | * @class Timer |
44 | | * @namespace Dalek |
45 | | * @part Timer |
46 | | * @api |
47 | | */ |
48 | | |
49 | 1 | Timer.prototype = { |
50 | | |
51 | | /** |
52 | | * Starts the timer |
53 | | * |
54 | | * @method start |
55 | | * @chainable |
56 | | */ |
57 | | |
58 | | start: function () { |
59 | 4 | this.timerData = process.hrtime(); |
60 | 4 | return this; |
61 | | }, |
62 | | |
63 | | /** |
64 | | * Stops the timer |
65 | | * |
66 | | * @method stop |
67 | | * @chainable |
68 | | */ |
69 | | |
70 | | stop: function () { |
71 | 3 | this.interval = process.hrtime(this.timerData); |
72 | 3 | return this; |
73 | | }, |
74 | | |
75 | | /** |
76 | | * Returns the elapsed time in ms |
77 | | * |
78 | | * @method getElapsedTime |
79 | | * @return {float} |
80 | | */ |
81 | | |
82 | | getElapsedTime: function () { |
83 | 4 | return (this.interval[0]*1e3 + this.interval[1]/1e6) / 1000; |
84 | | }, |
85 | | |
86 | | /** |
87 | | * Returns an object with test run time informations |
88 | | * containing hours, minutes & seconds |
89 | | * |
90 | | * @method getElapsedTimeFormatted |
91 | | * @return {Object} |
92 | | */ |
93 | | |
94 | | getElapsedTimeFormatted: function () { |
95 | 3 | var hours, minutes, seconds; |
96 | 3 | var elapsedTimeInSeconds = this.getElapsedTime(); |
97 | | |
98 | | // assign elapsed time (in seconds) to the seconds output |
99 | 3 | seconds = elapsedTimeInSeconds; |
100 | | |
101 | | // check if the elapsed time in seconds is more than a minute |
102 | | // and convert the raw seconds to minutes & seconds |
103 | 3 | if (elapsedTimeInSeconds > 60) { |
104 | 2 | minutes = Math.floor(elapsedTimeInSeconds / 60); |
105 | 2 | seconds = elapsedTimeInSeconds - minutes * 60; |
106 | | } |
107 | | |
108 | | // check if the elapsed time in minutes is more than an hour |
109 | | // and convert the raw seconds to hours, minutes & seconds |
110 | 3 | if (minutes > 60) { |
111 | 2 | hours = Math.floor(elapsedTimeInSeconds / 3600); |
112 | 2 | minutes = elapsedTimeInSeconds - hours * 60; |
113 | 2 | seconds = elapsedTimeInSeconds - minutes * 3600; |
114 | | } |
115 | | |
116 | 3 | return {hours: hours, minutes: minutes, seconds: seconds}; |
117 | | } |
118 | | }; |
119 | | |
120 | | // export the timer module |
121 | 1 | module.exports = Timer; |
122 | | |