Skip to content

Commit 7f2e898

Browse files
committed
bench: add benchmarks
--- 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: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: passed - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 17295e1 commit 7f2e898

File tree

5 files changed

+538
-0
lines changed

5 files changed

+538
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var pkg = require( './../package.json' ).name;
29+
var dasumpw = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var opts = {
35+
'skip': !hasWebAssemblySupport()
36+
};
37+
var options = {
38+
'dtype': 'float64'
39+
};
40+
41+
42+
// FUNCTIONS //
43+
44+
/**
45+
* Creates a benchmark function.
46+
*
47+
* @private
48+
* @param {PositiveInteger} len - array length
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( len ) {
52+
var x = uniform( len, -10.0, 10.0, options );
53+
return benchmark;
54+
55+
/**
56+
* Benchmark function.
57+
*
58+
* @private
59+
* @param {Benchmark} b - benchmark instance
60+
*/
61+
function benchmark( b ) {
62+
var sum;
63+
var i;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
sum = dasumpw.main( x.length, x, 1 );
68+
if ( isnan( sum ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( sum ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( pkg+':len='+len, opts, f );
103+
}
104+
}
105+
106+
main();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
25+
var Memory = require( '@stdlib/wasm/memory' );
26+
var pkg = require( './../package.json' ).name;
27+
var dasumpw = require( './../lib' );
28+
29+
30+
// VARIABLES //
31+
32+
var opts = {
33+
'skip': !hasWebAssemblySupport()
34+
};
35+
36+
37+
// MAIN //
38+
39+
bench( pkg+':Module:constructor', opts, function benchmark( b ) {
40+
var values;
41+
var o;
42+
var v;
43+
var i;
44+
45+
o = {
46+
'initial': 0
47+
};
48+
values = [
49+
new Memory( o ),
50+
new Memory( o )
51+
];
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
v = new dasumpw.Module( values[ i%values.length ] );
56+
if ( typeof v !== 'object' ) {
57+
b.fail( 'should return an object' );
58+
}
59+
}
60+
b.toc();
61+
if ( typeof v !== 'object' ) {
62+
b.fail( 'should return an object' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var hasWebAssemblySupport = require( '@stdlib/assert/has-wasm-support' );
25+
var Memory = require( '@stdlib/wasm/memory' );
26+
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
27+
var uniform = require( '@stdlib/random/array/uniform' );
28+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
29+
var pow = require( '@stdlib/math/base/special/pow' );
30+
var pkg = require( './../package.json' ).name;
31+
var dasumpw = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var opts = {
37+
'skip': !hasWebAssemblySupport()
38+
};
39+
var options = {
40+
'dtype': 'float64'
41+
};
42+
43+
44+
// FUNCTIONS //
45+
46+
/**
47+
* Creates a benchmark function.
48+
*
49+
* @private
50+
* @param {PositiveInteger} len - array length
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( len ) {
54+
return benchmark;
55+
56+
/**
57+
* Benchmark function.
58+
*
59+
* @private
60+
* @param {Benchmark} b - benchmark instance
61+
*/
62+
function benchmark( b ) {
63+
var xptr;
64+
var mod;
65+
var mem;
66+
var sum;
67+
var nb;
68+
var i;
69+
70+
// Create a new BLAS routine interface:
71+
mem = new Memory({
72+
'initial': 0
73+
});
74+
mod = new dasumpw.Module( mem );
75+
76+
// Initialize the module:
77+
mod.initializeSync(); // eslint-disable-line node/no-sync
78+
79+
// Reallocate the underlying memory to allow storing a vector:
80+
nb = bytesPerElement( options.dtype );
81+
mod.realloc( len*nb );
82+
83+
// Define a pointer (i.e., byte offset) to the first vector element:
84+
xptr = 0;
85+
86+
// Write random values to module memory:
87+
mod.write( xptr, uniform( len, -10.0, 10.0, options ) );
88+
89+
b.tic();
90+
for ( i = 0; i < b.iterations; i++ ) {
91+
sum = mod.main( len, xptr, 1 );
92+
if ( isnan( sum ) ) {
93+
b.fail( 'should not return NaN' );
94+
}
95+
}
96+
b.toc();
97+
if ( isnan( sum ) ) {
98+
b.fail( 'should not return NaN' );
99+
}
100+
b.pass( 'benchmark finished' );
101+
b.end();
102+
}
103+
}
104+
105+
106+
// MAIN //
107+
108+
/**
109+
* Main execution sequence.
110+
*
111+
* @private
112+
*/
113+
function main() {
114+
var len;
115+
var min;
116+
var max;
117+
var f;
118+
var i;
119+
120+
min = 1; // 10^min
121+
max = 6; // 10^max
122+
123+
for ( i = min; i <= max; i++ ) {
124+
len = pow( 10, i );
125+
f = createBenchmark( len );
126+
bench( pkg+'::module,pointers:len='+len, opts, f );
127+
}
128+
}
129+
130+
main();

0 commit comments

Comments
 (0)