Skip to content

Commit 10f3dea

Browse files
committed
feat(any-by): add instrumentation to the async any-by utility
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent ebb46af commit 10f3dea

3 files changed

Lines changed: 113 additions & 2 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
// MAIN //
24+
25+
/**
26+
* Creates a high-resolution timer to measure async operations.
27+
*
28+
* @returns {Function} function to stop the timer and return the duration in milliseconds
29+
*/
30+
function createTimer() {
31+
var start = process.hrtime();
32+
33+
return stop;
34+
35+
/**
36+
* Stops the timer and returns the elapsed time.
37+
*
38+
* @private
39+
* @returns {number} elapsed time in milliseconds
40+
*/
41+
function stop() {
42+
var diff = process.hrtime( start );
43+
44+
// Convert to milliseconds:
45+
return ( diff[ 0 ] * 1e3 ) + ( diff[ 1 ] / 1e6 );
46+
}
47+
}
48+
49+
50+
// EXPORTS //
51+
52+
module.exports = createTimer;

lib/node_modules/@stdlib/utils/async/any-by/lib/factory.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var format = require( '@stdlib/string/format' );
2626
var PINF = require( '@stdlib/constants/float64/pinf' );
2727
var validate = require( './validate.js' );
2828
var limit = require( './limit.js' );
29+
var createTimer = require( './../internal/instrument.js' );
2930

3031

3132
// MAIN //
@@ -129,6 +130,8 @@ function factory( options, predicate ) {
129130
* @returns {void}
130131
*/
131132
function anyByAsync( collection, done ) {
133+
var timer = createTimer();
134+
132135
if ( !isCollection( collection ) ) {
133136
throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );
134137
}
@@ -146,10 +149,11 @@ function factory( options, predicate ) {
146149
* @returns {void}
147150
*/
148151
function clbk( error, bool ) {
152+
var duration = timer();
149153
if ( error ) {
150-
return done( error, false );
154+
return done( error, false, duration );
151155
}
152-
done( null, bool );
156+
done( null, bool, duration );
153157
}
154158
}
155159
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var tape = require( 'tape' );
24+
var createTimer = require( './../internal/instrument.js' );
25+
26+
27+
// TESTS //
28+
29+
tape( 'main export is a function', function test( t ) {
30+
t.ok( true, __filename );
31+
t.strictEqual( typeof createTimer, 'function', 'main export is a function' );
32+
t.end();
33+
});
34+
35+
tape( 'the function returns a function', function test( t ) {
36+
var stop = createTimer();
37+
38+
t.strictEqual( typeof stop, 'function', 'returns a function' );
39+
t.end();
40+
});
41+
42+
tape( 'the returned function returns a number (elapsed milliseconds)', function test( t ) {
43+
var duration;
44+
var stop = createTimer();
45+
46+
setTimeout( onTimeout, 10 );
47+
48+
function onTimeout() {
49+
duration = stop();
50+
51+
t.strictEqual( typeof duration, 'number', 'returns a number' );
52+
t.ok( duration >= 0.0, 'returns a non-negative number' );
53+
t.end();
54+
}
55+
});

0 commit comments

Comments
 (0)