Skip to content

Commit

Permalink
fix: Round results of divisions instead of truncating them
Browse files Browse the repository at this point in the history
Prevent 0.000ms time durations in case of 0.0005ms intermediate result.
  • Loading branch information
prantlf committed Oct 22, 2017
1 parent 1e47547 commit 2feb8dc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
37 changes: 21 additions & 16 deletions lib/nettime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,13 @@ const https = require('https')
const url = require('url')

function nettime (options) {
if (typeof options === 'string') {
let url = options
options = {
url: url
}
}
const connection = url.parse(options.url)
const protocol = connection.protocol === 'http:' ? http : https
const parameters = getParameters()
const protocol = parameters.protocol === 'http:' ? http : https
const timings = {}
var start, response
const rejectUnauthorized = options.rejectUnauthorized
if (rejectUnauthorized !== undefined) {
connection.rejectUnauthorized = rejectUnauthorized
}
connection.agent = false
return new Promise((resolve, reject) => {
start = getTime(process.hrtime())
protocol.get(connection, localResponse => {
protocol.get(parameters, localResponse => {
response = localResponse
response.once('readable', () => {
timings.firstByte = getDuration()
Expand Down Expand Up @@ -55,9 +44,25 @@ function nettime (options) {
.on('error', reject)
})

function getParameters () {
if (typeof options === 'string') {
let url = options
options = {
url: url
}
}
const parameters = url.parse(options.url)
const rejectUnauthorized = options.rejectUnauthorized
if (rejectUnauthorized !== undefined) {
parameters.rejectUnauthorized = rejectUnauthorized
}
parameters.agent = false
return parameters
}

function getDuration () {
const duration = getTime(process.hrtime()) - start
return [parseInt(duration / 1e9), duration % 1e9]
return [Math.round(duration / 1e9), duration % 1e9]
}

function getTime (timing) {
Expand All @@ -76,7 +81,7 @@ function getDuration (start, end) {
}

function getMilliseconds (timing) {
return timing[0] * 1000 + parseInt(timing[1] / 1000) / 1000
return timing[0] * 1000 + Math.round(timing[1] / 1000) / 1000
}

nettime.getDuration = getDuration
Expand Down
8 changes: 4 additions & 4 deletions lib/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ function printMilliseconds (timings) {
if (timing) {
let duration = getDuration(lastTiming, timing)
console.log(sprintf('%-17s %3d.%03ds %3d.%03ds',
labels[part], timing[0], parseInt(timing[1] / 1e6),
duration[0], parseInt(duration[1] / 1e6)))
labels[part], timing[0], Math.round(timing[1] / 1e6),
duration[0], Math.round(duration[1] / 1e6)))
lastTiming = timing
}
}
Expand All @@ -42,8 +42,8 @@ function printNanoseconds (timings) {
if (timing) {
let duration = getDuration(lastTiming, timing)
console.log(sprintf('%-17s %3ds %7.3fms %3ds %7.3fms',
labels[part], timing[0], parseInt(timing[1] / 1000) / 1000,
duration[0], parseInt(duration[1] / 1000) / 1000))
labels[part], timing[0], Math.round(timing[1] / 1000) / 1000,
duration[0], Math.round(duration[1] / 1000) / 1000))
lastTiming = timing
}
}
Expand Down

0 comments on commit 2feb8dc

Please sign in to comment.