-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(#368): added utility function for elapsed time tracking #389
Merged
Merged
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0fac2c1
fix(#368): added utility function for elapsed time tracking
trapvpack 920198b
fix(#368): simplify logic and format code for elapsed
trapvpack 32e0759
fix(#368): fix up todo needful time
trapvpack 4b58147
Merge branch 'master' into 368
trapvpack 80ee2ec
fix(#368): formatted, simplified
trapvpack 7a6e0a9
Merge branch 'master' into 368
trapvpack 4efeacb
fix(#368): formatted
trapvpack 0911954
fix(#368): added utility function for elapsed time tracking
trapvpack 62c60f3
fix(#368): simplify logic and format code for elapsed
trapvpack 42858f3
fix(#368): fix up todo needful time
trapvpack 357691e
fix(#368): formatted, simplified
trapvpack 4abfb9c
fix(#368): formatted
trapvpack 5834769
fix(#368): formatted
trapvpack fb49d5f
Merge remote-tracking branch 'origin/368' into 368
trapvpack 816cb0f
fix(#368): formatted
trapvpack b37f5bf
fix(#368): formatted
trapvpack 7ef9618
fix(#368): formatted
trapvpack 4dd5eea
fix(#368): formatted
trapvpack 9d3e2a8
fix(#368): formatted
trapvpack f911833
fix(#368): simplify regex for time measurement in test
trapvpack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2022-2024 Objectionary.com | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* @todo #368:30min Decide if the `elapsed` utility function is in the right place | ||
* Consider relocating `elapsed` utility function and its test file if needed | ||
*/ | ||
|
||
/** | ||
* A utility function to measure the elapsed time of a task and provide | ||
* detailed timing information. | ||
* | ||
* This function wraps a given task (callback function) and provides it with | ||
* a `tracked` object that includes a `print` method. The `print` method can | ||
* be used within the task to log messages along with the elapsed time | ||
* since the task started execution. The elapsed time is formatted in milliseconds, | ||
* seconds, or minutes, based on the duration. | ||
* | ||
* @param {Function} task - A callback function to be measured. The function | ||
* is invoked with a `tracked` object as an argument. | ||
* @return {*} Result of the wrapped callback function. The result of the | ||
* `task` callback will be returned unchanged. | ||
*/ | ||
module.exports.elapsed = function elapsed(task) { | ||
const startTime = Date.now(); | ||
return task({ | ||
print: (message) => { | ||
const duration = Date.now() - startTime; | ||
let extended; | ||
if (duration < 1000) { | ||
extended = `${duration}ms`; | ||
} else if (duration < 60 * 1000) { | ||
extended = `${Math.ceil(duration / 1000)}s`; | ||
} else { | ||
extended = `${Math.ceil(duration / 3600000)}min`; | ||
} | ||
const msg = `${message} in ${extended}`; | ||
console.info(msg); | ||
return msg; | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2022-2024 Objectionary.com | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
const {elapsed} = require('../src/elapsed'); | ||
const assert = require('assert'); | ||
|
||
describe('elapsed', function() { | ||
const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
it('measures time correctly', async () => { | ||
return elapsed(async (tracked) => { | ||
await snooze(300); | ||
return tracked.print('task'); | ||
}).then( | ||
(actual) => assert( | ||
/task in 30\d+ms/.test(actual), | ||
`Expected "${actual}" to match /task in 30\\d+ms/` | ||
) | ||
); | ||
}); | ||
it('measures short time correctly', async () => { | ||
return elapsed(async (tracked) => { | ||
await snooze(10); | ||
return tracked.print('short task'); | ||
}).then( | ||
(actual) => assert( | ||
/short task in 1\d+ms/.test(actual), | ||
`Expected "${actual}" to match /short task in 1\\d+ms/` | ||
) | ||
); | ||
}); | ||
it('measures long time correctly', async () => { | ||
return elapsed(async (tracked) => { | ||
await snooze(1200); | ||
return tracked.print('long task'); | ||
}).then( | ||
(actual) => assert( | ||
/long task in 2s/.test(actual), | ||
`Expected "${actual}" to match /long task in 2s/` | ||
) | ||
); | ||
}); | ||
it('handles errors in task correctly', async () => { | ||
await assert.rejects( | ||
elapsed(async () => { | ||
throw new Error('task error'); | ||
}), | ||
(error) => { | ||
assert.throws(() => { | ||
throw error; | ||
}, /task error/); | ||
return true; | ||
} | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trapvpack you have two comments section here stated with
/**
and ended with*/
. The first comment is on lines 25-28, the second is on 30-44 and there's an empty line 29 between them. We don't need two comments here, it should one single comment started on line 25 and ended on line 43 or 44 with no empty lines. Also please move your todo under the function description