Coverage

100%
22
22
0

/home/ubuntu/src/github.com/dalekjs/dalek-internal-timer/index.js

100%
22
22
0
LineHitsSource
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
251'use strict';
26
27/**
28 * Initializes the timers default values
29 *
30 * @constructor
31 * @class Timer
32 */
33
341var Timer = function () {
357 this.timerData = [0, 0];
367 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
491Timer.prototype = {
50
51 /**
52 * Starts the timer
53 *
54 * @method start
55 * @chainable
56 */
57
58 start: function () {
594 this.timerData = process.hrtime();
604 return this;
61 },
62
63 /**
64 * Stops the timer
65 *
66 * @method stop
67 * @chainable
68 */
69
70 stop: function () {
713 this.interval = process.hrtime(this.timerData);
723 return this;
73 },
74
75 /**
76 * Returns the elapsed time in ms
77 *
78 * @method getElapsedTime
79 * @return {float}
80 */
81
82 getElapsedTime: function () {
834 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 () {
953 var hours, minutes, seconds;
963 var elapsedTimeInSeconds = this.getElapsedTime();
97
98 // assign elapsed time (in seconds) to the seconds output
993 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
1033 if (elapsedTimeInSeconds > 60) {
1042 minutes = Math.floor(elapsedTimeInSeconds / 60);
1052 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
1103 if (minutes > 60) {
1112 hours = Math.floor(elapsedTimeInSeconds / 3600);
1122 minutes = elapsedTimeInSeconds - hours * 60;
1132 seconds = elapsedTimeInSeconds - minutes * 3600;
114 }
115
1163 return {hours: hours, minutes: minutes, seconds: seconds};
117 }
118};
119
120// export the timer module
1211module.exports = Timer;
122