├── .gitignore ├── LICENSE ├── NOTICE ├── README.md ├── css └── styles.css ├── doc ├── Example.md ├── chrome_os_graph.png ├── mac_graph.png ├── win_graph.png └── win_mac_chrome_graph.png ├── index.html └── js ├── Benchmark.js ├── Test.js └── WebAudioBenchApplication.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Web Audio Bench 2 | Copyright (c) 2019 Spotify AB 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Web Audio Bench 2 | 3 | [![lifecycle](https://img.shields.io/badge/lifecycle-alpha-blue.svg)](https://img.shields.io/badge/lifecycle-alpha-blue.svg) 4 | [![License](https://img.shields.io/github/license/spotify/apollo.svg)](LICENSE.txt) 5 | 6 | Web Audio Bench is a test suite for comparing web audio performance across devices and browsers. It is written in Javascript. 7 | 8 | ## Example 9 | 10 | The example below compares different browsers on Mac. Lower values are better. It shows: 11 | 12 | * The performance is similar across browsers for AudioBufferSourceNode and OscillatorNode. 13 | * There are big variations in GainNode and BiquadFilterNode automation performance. 14 | * The most expensive node types are CompressorNode and the nodes involving FFT; WaveShaperNode, ConvolverNode and PannerNode, with variations across browsers. 15 | * Safari has the fastest baseline and most nodes are a bit faster. This could indicate faster framework, such as a more efficient audio graph traversal / audio data propagation. 16 | * The MixedBenchmark metric is a compound score using a handful of the most commonly used nodes. It can be used as an overall score for a given platform and browser. 17 | 18 | ![Example Graph](doc/mac_graph.png) 19 | 20 | ## About 21 | 22 | The primary goal of Web Audio Bench is to provide a benchmark of overall CPU performance for Web Audio as well as individual performance figures for different AudioNodes. The results can be used to e.g. 23 | 24 | * Compare the performance of different browsers on the same hardware 25 | * Compare the performance on different hardware for the same browser 26 | * Compare different browser versions 27 | 28 | It can be used to identify regressions as well as optimisation opportunities. It can also be used to customize web audio usage for best performance per platform. 29 | 30 | The second goal is to allow Web Audio developers to write custom tests to compare the relative performance of different graph layouts. For this, you'll need to fork this project and add your own 31 | custom test implementations. 32 | 33 | * [More examples](doc/Example.md) 34 | * [Source code for tests](js/Test.js) 35 | 36 | # Running 37 | 38 | To run the benchmark, go to [https://spotify.github.io/web-audio-bench/](https://spotify.github.io/web-audio-bench/) or clone this repo and open index.html in your browser. 39 | 40 | Alternatively, use an http server, e.g.: 41 | 42 | npm install http-server -g 43 | http-server -p 7777 -c-1 44 | 45 | Then, open http://localhost:7777 46 | 47 | ### Safari note 48 | 49 | 2019-09-11: A memory leak in Safari makes the browser tab crash after 50 test runs or so. To avoid it, reduce number of test runs. If this doesn't help, reduce the test duration too. 50 | 51 | Also note that Safari time resolution is limited to 1ms, so the default test duration is set to 5x longer than on other browsers. 52 | 53 | # Developing 54 | 55 | All tests inherit the `Test` base class. For a simple example, look at the `CompressorTest`. You can add constructor arguments to customize the kind of test to be run. In the `CompressorTest` 56 | example, we can provide different `knee`-values and see how this affects performance. 57 | 58 | After having written your test, add it to the list of tests in `WebAudioBenchApplication.getTestList()`. 59 | 60 | In order to get fast test runs while not measuring too short time periods, all tests have been tuned to take approximately 50 ms on a MacBook Pro 2017 (3,1 GHz Intel Core i7) when running in Chrome 78. When adding your 61 | own tests, try to make them run roughly as long. You can tune the test duration by setting `numNodes` in the `Test` constructor, which will indicate how many 62 | nodes you intend to test on in parallel. The total duration of your test will then be divided by this number. 63 | 64 | ## Contributing 65 | 66 | Contributions such as bug fixes, improvements and new tests are welcome. Please submit a pull request or issue. 67 | 68 | ## Development Status 69 | 70 | This product is considered alpha. 71 | 72 | ## Code of Conduct 73 | 74 | This project adheres to the [Open Code of Conduct][code-of-conduct]. By participating, you are expected to honor this code. 75 | 76 | [code-of-conduct]: https://github.com/spotify/code-of-conduct/blob/master/code-of-conduct.md 77 | -------------------------------------------------------------------------------- /css/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: 'Crete Round', serif; 3 | padding: 20px; 4 | background-color: #fff; 5 | color: #000; 6 | font-size: 14px; 7 | line-height: 1.5em; 8 | } 9 | 10 | h1 { 11 | margin-top: 20px; 12 | font-size: 100px; 13 | line-height: 1.15em; 14 | margin-bottom: 20px; 15 | color: #333; 16 | } 17 | 18 | .fork-me-ribbon { 19 | position: absolute; 20 | top: 0; 21 | right: 0; 22 | } 23 | 24 | .mid-section { 25 | display: flex; 26 | flex-direction: row; 27 | margin: 30px 0 40px; 28 | } 29 | 30 | .mid-section .control-column { 31 | width: 470px; 32 | } 33 | 34 | .input-basic { 35 | box-sizing: border-box; 36 | padding: 5px 10px; 37 | border-radius: 3px; 38 | box-shadow: none; 39 | font-size: 13px; 40 | border: 1px solid #ccc; 41 | } 42 | 43 | .select-basic { 44 | font-family: 'Crete Round', serif; 45 | box-sizing: border-box; 46 | border-radius: 3px; 47 | box-shadow: none; 48 | font-size: 13px; 49 | border: 1px solid #ccc; 50 | } 51 | 52 | .run-settings .control { 53 | display: flex; 54 | margin-bottom: 20px; 55 | } 56 | 57 | .run-settings .control label { 58 | width: 100px; 59 | } 60 | 61 | .run-settings .runs input { 62 | width: 250px; 63 | } 64 | 65 | .run-settings .duration input { 66 | width: 250px; 67 | } 68 | 69 | .run-settings .tests-selection select { 70 | width: 250px; 71 | height: 250px; 72 | } 73 | 74 | .button-basic { 75 | border: 0; 76 | border-radius: 20px; 77 | box-shadow: none; 78 | background-color: #5790c1; 79 | color: #fff; 80 | padding: 9px 20px; 81 | font-size: 17px; 82 | min-width: 200px; 83 | } 84 | 85 | .button-basic:hover { 86 | opacity: 0.9; 87 | } 88 | 89 | .results-table { 90 | font-family: Monospaced, Courier; 91 | font-size: 16px; 92 | line-height: 1.5em; 93 | white-space: pre; 94 | } 95 | 96 | .results-table th { 97 | width: 80px; 98 | font-weight: bold; 99 | text-align: right; 100 | } 101 | 102 | .results-table td { 103 | width: 80px; 104 | text-align: right; 105 | } 106 | 107 | .results-table td:nth-child(1), .results-table th:nth-child(1) { 108 | text-align: left; 109 | width: 300px; 110 | font-weight: bold; 111 | } 112 | 113 | .results-table td:nth-child(2), .results-table th:nth-child(2) { 114 | font-weight: bold; 115 | } 116 | 117 | .results-table td:nth-child(3), .results-table th:nth-child(3) { 118 | width: 250px; 119 | } 120 | -------------------------------------------------------------------------------- /doc/Example.md: -------------------------------------------------------------------------------- 1 | # Web Audio Bench Examples 2 | 3 | ## Mac 4 | 5 | * MacBook Pro 2017 (3,1 GHz Intel Core i7). 6 | * The performance is similar across browsers for AudioBufferSourceNode and OscillatorNode. 7 | * There are big variations in GainNode and BiquadFilterNode automation performance. 8 | * The most expensive node types are CompressorNode and the nodes involving FFT; WaveShaperNode, ConvolverNode and PannerNode, with variations across browsers. 9 | * Safari has the fastest baseline and most nodes are a bit faster. This could indicate faster framework, such as a more efficient audio graph traversal / audio data propagation. 10 | * The MixedBenchmark metric is a compound score using a handful of the most commonly used nodes. It can be used as an overall score for a given platform and browser. 11 | 12 | ![Mac Graph](mac_graph.png) 13 | 14 | | TEST | Mac Chrome 79 | Mac Safari 13 | Mac Firefox 71 | 15 | |--------------------------------|---------------|---------------|----------------| 16 | | Baseline | 313 | 97 | 230 | 17 | | Biquad-default | 195 | 139 | 185 | 18 | | Biquad-440 | 204 | 134 | 184 | 19 | | AudioBufferSource-rate1 | 62 | 45 | 35 | 20 | | AudioBufferSource-rate0.9 | 229 | 204 | 193 | 21 | | Oscillator | 448 | 370 | 372 | 22 | | Gain-default | 42 | 21 | 21 | 23 | | Gain-1.0 | 66 | 20 | 22 | 24 | | Gain-0.9 | 66 | 24 | 23 | 25 | | Gain-0.9-k-rate | 59 | 22 | 21 | 26 | | GainCancel-1.0 | 66 | 21 | 22 | 27 | | GainCancel-0.9 | 66 | 23 | 22 | 28 | | GainCancel-0.9-k-rate | 60 | 22 | 21 | 29 | | GainAutomation-exp-a-rate | 108 | 111 | 830 | 30 | | GainAutomation-linear-a-rate | 63 | 113 | 362 | 31 | | GainAutomation-target-a-rate | 90 | 196 | 615 | 32 | | GainAutomation-curve-a-rate | 114 | 114 | 1029 | 33 | | GainAutomation-exp-k-rate | 99 | 117 | 834 | 34 | | GainAutomation-linear-k-rate | 76 | 122 | 363 | 35 | | GainAutomation-target-k-rate | 90 | 206 | 612 | 36 | | GainAutomation-curve-k-rate | 99 | 109 | 1029 | 37 | | BiquadAutomation-exp-a-rate | 1387 | 308 | 201 | 38 | | BiquadAutomation-linear-a-rate | 1347 | 283 | 193 | 39 | | BiquadAutomation-target-a-rate | 1382 | 340 | 201 | 40 | | BiquadAutomation-curve-a-rate | 1403 | 327 | 199 | 41 | | BiquadAutomation-exp-k-rate | 309 | 287 | 200 | 42 | | BiquadAutomation-linear-k-rate | 276 | 290 | 196 | 43 | | BiquadAutomation-target-k-rate | 296 | 292 | 199 | 44 | | BiquadAutomation-curve-k-rate | 313 | 286 | 198 | 45 | | Delay-default | 753 | 795 | 902 | 46 | | Delay-0.1 | 767 | 790 | 908 | 47 | | DelayAutomation-a-rate | 818 | 875 | 1128 | 48 | | DelayAutomation-k-rate | 768 | 930 | 1133 | 49 | | ChannelSplitter | 38 | 21 | 36 | 50 | | ChannelMerger | 44 | 19 | 182 | 51 | | Analyser | 33 | 17 | 420 | 52 | | WaveShaper-1x | 209 | 234 | 177 | 53 | | WaveShaper-2x | 902 | 962 | 865 | 54 | | WaveShaper-4x | 2203 | 2283 | 1590 | 55 | | Compressor-knee-0 | 4013 | 5737 | 5710 | 56 | | Compressor-knee-40 | 3383 | 4903 | 5047 | 57 | | Convolver-128f-3ms | 379 | 150 | 360 | 58 | | Convolver-1024f-23ms | 933 | 923 | 1340 | 59 | | Convolver-2048f-46ms | 1097 | 1093 | 1667 | 60 | | Convolver-32768f-743ms | 2088 | 2090 | 3478 | 61 | | Panner-equalpower | 285 | 221 | 122 | 62 | | Panner-HRTF | 2047 | 1920 | 2887 | 63 | | StereoPanner-default | 80 | - | 176 | 64 | | StereoPanner-0 | 92 | - | 174 | 65 | | StereoPanner-0.2 | 93 | - | 194 | 66 | | StereoPanner-1 | 94 | - | 192 | 67 | | MixedBenchmark | 2835 | 2010 | 2791 | 68 | 69 | ## Windows vs Mac Chrome 70 | 71 | * Mac: MacBook Pro 2017 (3,1 GHz Intel Core i7). 72 | * Windows: Intel Core i5-7300U 2.6Hgz 2.71 GHz. Test results were adjusted for clock speed difference (2.6 / 3.1). 73 | * Windows has 100% slower FFT performance. 74 | * Windows has 50% slower Biquad performance. 75 | 76 | ![Windows vs Mac Chrome Graph](win_mac_chrome_graph.png) 77 | 78 | | Test | Chrome 77 Win adjusted | Chrome 78 Mac | 79 | |--------------------------------|------------------------|---------------| 80 | | Baseline | 313 | 317 | 81 | | Biquad-default | 321 | 190 | 82 | | Biquad-440 | 335 | 211 | 83 | | AudioBufferSource-rate1 | 78 | 61 | 84 | | AudioBufferSource-rate0.9 | 231 | 229 | 85 | | Oscillator | 414 | 437 | 86 | | Gain-default | 45 | 42 | 87 | | Gain-1.0 | 72 | 65 | 88 | | Gain-0.9 | 70 | 69 | 89 | | Gain-0.9-k-rate | 66 | 59 | 90 | | GainCancel-1.0 | 74 | 66 | 91 | | GainCancel-0.9 | 71 | 69 | 92 | | GainCancel-0.9-k-rate | 122 | 60 | 93 | | GainAutomation-exp-a-rate | 122 | 107 | 94 | | GainAutomation-linear-a-rate | 84 | 60 | 95 | | GainAutomation-target-a-rate | 100 | 89 | 96 | | GainAutomation-curve-a-rate | 127 | 109 | 97 | | GainAutomation-exp-k-rate | 122 | 98 | 98 | | GainAutomation-linear-k-rate | 94 | 73 | 99 | | GainAutomation-target-k-rate | 108 | 89 | 100 | | GainAutomation-curve-k-rate | 126 | 98 | 101 | | BiquadAutomation-exp-a-rate | 1494 | 1372 | 102 | | BiquadAutomation-linear-a-rate | 1344 | 1380 | 103 | | BiquadAutomation-target-a-rate | 1375 | 1354 | 104 | | BiquadAutomation-curve-a-rate | 1406 | 1361 | 105 | | BiquadAutomation-exp-k-rate | 502 | 410 | 106 | | BiquadAutomation-linear-k-rate | 453 | 432 | 107 | | BiquadAutomation-target-k-rate | 460 | 414 | 108 | | BiquadAutomation-curve-k-rate | 493 | 401 | 109 | | Delay-default | 720 | 738 | 110 | | Delay-0.1 | 752 | 763 | 111 | | DelayAutomation-a-rate | 899 | 820 | 112 | | DelayAutomation-k-rate | 776 | 750 | 113 | | ChannelSplitter | 51 | 37 | 114 | | ChannelMerger | 60 | 43 | 115 | | Analyser | 51 | 32 | 116 | | WaveShaper-1x | 481 | 208 | 117 | | WaveShaper-2x | 2073 | 897 | 118 | | WaveShaper-4x | 4470 | 2193 | 119 | | Compressor-knee-0 | 4476 | 3993 | 120 | | Compressor-knee-40 | 3928 | 3393 | 121 | | Convolver-128f-3ms | 790 | 392 | 122 | | Convolver-1024f-23ms | 1781 | 943 | 123 | | Convolver-2048f-46ms | 2119 | 1137 | 124 | | Convolver-32768f-743ms | 3897 | 2089 | 125 | | Panner-equalpower | 325 | 285 | 126 | | Panner-HRTF | 2376 | 2027 | 127 | | StereoPanner-default | 105 | 79 | 128 | | StereoPanner-0 | 124 | 91 | 129 | | StereoPanner-0.2 | 124 | 94 | 130 | | StereoPanner-1 | 124 | 91 | 131 | | MixedBenchmark | 3752 | 2542 | 132 | 133 | ## Windows 134 | 135 | * Windows: Intel Core i5-7300U 2.6Hgz 2.71 GHz. 136 | * A lot of differences due to completely different implementations. 137 | 138 | ![Windows vs Mac Chrome Graph](win_graph.png) 139 | 140 | 141 | | Test | Chrome 77 Win | Firefox 69 Win | 142 | |--------------------------------|---------------|----------------| 143 | | Baseline | 373 | 273 | 144 | | Biquad-default | 383 | 237 | 145 | | Biquad-440 | 400 | 227 | 146 | | AudioBufferSource-rate1 | 93 | 54 | 147 | | AudioBufferSource-rate0.9 | 276 | 307 | 148 | | Oscillator | 494 | 660 | 149 | | Gain-default | 54 | 35 | 150 | | Gain-1.0 | 86 | 29 | 151 | | Gain-0.9 | 84 | 29 | 152 | | Gain-0.9-k-rate | 79 | 29 | 153 | | GainCancel-1.0 | 88 | 32 | 154 | | GainCancel-0.9 | 85 | 31 | 155 | | GainCancel-0.9-k-rate | 145 | 30 | 156 | | GainAutomation-exp-a-rate | 146 | 1183 | 157 | | GainAutomation-linear-a-rate | 100 | 321 | 158 | | GainAutomation-target-a-rate | 119 | 675 | 159 | | GainAutomation-curve-a-rate | 151 | 725 | 160 | | GainAutomation-exp-k-rate | 146 | 1177 | 161 | | GainAutomation-linear-k-rate | 112 | 316 | 162 | | GainAutomation-target-k-rate | 129 | 680 | 163 | | GainAutomation-curve-k-rate | 150 | 698 | 164 | | BiquadAutomation-exp-a-rate | 1781 | 247 | 165 | | BiquadAutomation-linear-a-rate | 1602 | 249 | 166 | | BiquadAutomation-target-a-rate | 1639 | 243 | 167 | | BiquadAutomation-curve-a-rate | 1676 | 253 | 168 | | BiquadAutomation-exp-k-rate | 598 | 246 | 169 | | BiquadAutomation-linear-k-rate | 540 | 234 | 170 | | BiquadAutomation-target-k-rate | 549 | 239 | 171 | | BiquadAutomation-curve-k-rate | 588 | 239 | 172 | | Delay-default | 858 | 1170 | 173 | | Delay-0.1 | 897 | 1173 | 174 | | DelayAutomation-a-rate | 1072 | 1382 | 175 | | DelayAutomation-k-rate | 925 | 1370 | 176 | | ChannelSplitter | 61 | 62 | 177 | | ChannelMerger | 72 | 243 | 178 | | Analyser | 61 | 398 | 179 | | WaveShaper-1x | 573 | 203 | 180 | | WaveShaper-2x | 2472 | 1232 | 181 | | WaveShaper-4x | 5330 | 2197 | 182 | | Compressor-knee-0 | 5337 | 6317 | 183 | | Compressor-knee-40 | 4683 | 5720 | 184 | | Convolver-128f-3ms | 942 | 464 | 185 | | Convolver-1024f-23ms | 2123 | 1593 | 186 | | Convolver-2048f-46ms | 2527 | 2027 | 187 | | Convolver-32768f-743ms | 4647 | 4290 | 188 | | Panner-equalpower | 387 | 161 | 189 | | Panner-HRTF | 2833 | 4020 | 190 | | StereoPanner-default | 125 | 221 | 191 | | StereoPanner-0 | 148 | 222 | 192 | | StereoPanner-0.2 | 148 | 245 | 193 | | StereoPanner-1 | 148 | 246 | 194 | | MixedBenchmark | 4474 | 3533 | 195 | 196 | ## Chrome OS 197 | 198 | * Dell Chromebook 11 P22T (3120) Intel Celeron N2840 2.16 GHz (candy) 199 | * Lenovo N23 Yoga Chromebook ARM Cortex A53 (ARMv8) (hana) 200 | * This compares Intel vs ARM performance, however on two machines of unequal strength. 201 | * The Lenovo is faster on everything, except FFT, where it is 20% slower. 202 | 203 | ![ChromeOS Graph](chrome_os_graph.png) 204 | 205 | | TEST | Dell Chromebook 11 Chrome 77 | Lenovo N23 Yoga Chrome 77 | 206 | |--------------------------------|------------------------------|---------------------------| 207 | | Baseline | 1070 | 1000 | 208 | | Biquad-default | 1022 | 723 | 209 | | Biquad-440 | 1086 | 751 | 210 | | AudioBufferSource-rate1 | 357 | 337 | 211 | | AudioBufferSource-rate0.9 | 1113 | 926 | 212 | | Oscillator | 1777 | 1208 | 213 | | Gain-default | 182 | 158 | 214 | | Gain-1.0 | 288 | 241 | 215 | | Gain-0.9 | 307 | 246 | 216 | | Gain-0.9-k-rate | 270 | 210 | 217 | | GainCancel-1.0 | 295 | 242 | 218 | | GainCancel-0.9 | 308 | 245 | 219 | | GainCancel-0.9-k-rate | 272 | 205 | 220 | | GainAutomation-exp-a-rate | 499 | 439 | 221 | | GainAutomation-linear-a-rate | 330 | 560 | 222 | | GainAutomation-target-a-rate | 433 | 488 | 223 | | GainAutomation-curve-a-rate | 806 | 736 | 224 | | GainAutomation-exp-k-rate | 559 | 422 | 225 | | GainAutomation-linear-k-rate | 375 | 280 | 226 | | GainAutomation-target-k-rate | 464 | 347 | 227 | | GainAutomation-curve-k-rate | 476 | 397 | 228 | | BiquadAutomation-exp-a-rate | 11604 | 6557 | 229 | | BiquadAutomation-linear-a-rate | 11367 | 6656 | 230 | | BiquadAutomation-target-a-rate | 11511 | 6557 | 231 | | BiquadAutomation-curve-a-rate | 11948 | 6838 | 232 | | BiquadAutomation-exp-k-rate | 2162 | 1402 | 233 | | BiquadAutomation-linear-k-rate | 1901 | 1203 | 234 | | BiquadAutomation-target-k-rate | 2058 | 1356 | 235 | | BiquadAutomation-curve-k-rate | 2061 | 1388 | 236 | | Delay-default | 1963 | 843 | 237 | | Delay-0.1 | 2000 | 898 | 238 | | DelayAutomation-a-rate | 2978 | 1255 | 239 | | DelayAutomation-k-rate | 2037 | 878 | 240 | | ChannelSplitter | 191 | 157 | 241 | | ChannelMerger | 262 | 205 | 242 | | Analyser | 189 | 196 | 243 | | WaveShaper-1x | 972 | 587 | 244 | | WaveShaper-2x | 6782 | 10713 | 245 | | WaveShaper-4x | 16840 | 18940 | 246 | | Compressor-knee-0 | 18133 | 14200 | 247 | | Compressor-knee-40 | 16003 | 12500 | 248 | | Convolver-128f-3ms | 3483 | 5366 | 249 | | Convolver-1024f-23ms | 8857 | 10480 | 250 | | Convolver-2048f-46ms | 10867 | 12433 | 251 | | Convolver-32768f-743ms | 21920 | 26142 | 252 | | Panner-equalpower | 1431 | 1181 | 253 | | Panner-HRTF | 10997 | 7117 | 254 | | StereoPanner-default | 505 | 359 | 255 | | StereoPanner-0 | 584 | 402 | 256 | | StereoPanner-0.2 | 573 | 413 | 257 | | StereoPanner-1 | 792 | 624 | 258 | | MixedBenchmark | 17374 | 13201 | 259 | -------------------------------------------------------------------------------- /doc/chrome_os_graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spotify/web-audio-bench/77a6f80e43044481a9adbf40288b56a7473d38ed/doc/chrome_os_graph.png -------------------------------------------------------------------------------- /doc/mac_graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spotify/web-audio-bench/77a6f80e43044481a9adbf40288b56a7473d38ed/doc/mac_graph.png -------------------------------------------------------------------------------- /doc/win_graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spotify/web-audio-bench/77a6f80e43044481a9adbf40288b56a7473d38ed/doc/win_graph.png -------------------------------------------------------------------------------- /doc/win_mac_chrome_graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spotify/web-audio-bench/77a6f80e43044481a9adbf40288b56a7473d38ed/doc/win_mac_chrome_graph.png -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Web Audio Bench 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | Fork me on GitHub 17 |
18 | 19 |

Web Audio Bench

20 | 21 |

This page contains a series of web audio benchmark tests. The tests are run in an OfflineAudioContext, where an AudioBufferSourceNode is used as baseline to feed data into the test. 22 | To get more accurate results, each test is run multiple times and the min duration is chosen (here's why). The results are given in microseconds per second of processed audio data and node, so lower is 23 | faster. A value of 1000000 means realtime processing speed.

24 | 25 |

For accurate results:

26 | 27 | 34 | 35 |
36 |
37 |
38 |
39 | 40 | 42 |
43 |
44 | 45 | 46 |
47 |
48 | 49 | 50 |
51 |
52 | 53 | 54 |
55 |
56 |
57 |
58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 |
TESTμsMINQ1MEDIANQ3MAXMEANSTDDEV
71 |
72 |
73 | 74 | 75 | 76 | 77 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /js/Benchmark.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-Present, Spotify AB. 3 | * 4 | * Licensed to the Apache Software Foundation (ASF) under one 5 | * or more contributor license agreements. See the NOTICE file 6 | * distributed with this work for additional information 7 | * regarding copyright ownership. The ASF licenses this file 8 | * to you under the Apache License, Version 2.0 (the 9 | * "License"); you may not use this file except in compliance 10 | * with the License. You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, 15 | * software distributed under the License is distributed on an 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | * KIND, either express or implied. See the License for the 18 | * specific language governing permissions and limitations 19 | * under the License. 20 | */ 21 | 22 | class Benchmark { 23 | /** 24 | * @param name The benchmark name. 25 | * @param testResults a map from test name to score from the individual tests. 26 | */ 27 | constructor(name, testResults) { 28 | this.name = name; 29 | this.testResults = testResults; 30 | } 31 | 32 | /** 33 | * Calculates the score for a test given a multiplier. 34 | * @param name The test name. 35 | * @param multiplier The multiplier. 36 | * @returns {number} 37 | */ 38 | calcScore(name, multiplier) { 39 | if (this.testResults[name] === undefined) { 40 | throw new Error('Cannot run benchmark, test not run: ' + name); 41 | } 42 | return this.testResults[name] * multiplier; 43 | } 44 | 45 | /** 46 | * Calculates the benchmark score based on individual test results. 47 | * @returns {number} The benchmark score. Lower is better. 48 | */ 49 | calculate() { 50 | return 0; 51 | } 52 | } 53 | 54 | class MixedBenchmark extends Benchmark{ 55 | constructor(testResults) { 56 | super('MixedBenchmark', testResults); 57 | } 58 | 59 | calculate() { 60 | // We include the most commonly used node types. 61 | // The number of nodes per type is chosen in attempt to give each node type equal 62 | // influence on the total score, given the node type's "expected" computational cost. 63 | // This was chosen such that each group costs around 2000ms on Chrome 78 for a MacBook 2017 3.1GHz. 64 | 65 | let score = 0; 66 | let groups = 0; 67 | 68 | // GainNode group 69 | // 20 gain nodes 70 | score += this.calcScore('Gain-default', 20); 71 | // 10 gain nodes with non-default value 72 | score += this.calcScore('Gain-0.9', 10); 73 | // 8 gain nodes with automation 74 | score += this.calcScore('GainAutomation-exp-a-rate', 2); 75 | score += this.calcScore('GainAutomation-linear-a-rate', 2); 76 | score += this.calcScore('GainAutomation-target-a-rate', 2); 77 | score += this.calcScore('GainAutomation-curve-a-rate', 2); 78 | groups ++; 79 | 80 | // BiquadFilterNode group 81 | // 5 biquads 82 | score += this.calcScore('Biquad-default', 5); 83 | // 4 biquads with automation 84 | score += this.calcScore('BiquadAutomation-exp-a-rate', 1); 85 | score += this.calcScore('BiquadAutomation-linear-a-rate', 1); 86 | score += this.calcScore('BiquadAutomation-target-a-rate', 1); 87 | score += this.calcScore('BiquadAutomation-curve-a-rate', 1); 88 | groups ++; 89 | 90 | // DelayNode group 91 | // 3 delays 92 | score += this.calcScore('Delay-0.1', 3); 93 | groups ++; 94 | 95 | // WaveShaperNode group 96 | // 10 wave shapers 97 | score += this.calcScore('WaveShaper-1x', 10); 98 | groups ++; 99 | 100 | // OscillatorNode group 101 | // 5 oscillators 102 | score += this.calcScore('Oscillator', 5); 103 | groups ++; 104 | 105 | // AudioBufferSourceNode group 106 | // 20 absn rate 1 107 | score += this.calcScore('AudioBufferSource-rate1', 20); 108 | // 5 absn rate 0.9 109 | score += this.calcScore('AudioBufferSource-rate0.9', 5); 110 | groups ++; 111 | 112 | // ConvolverNode group 113 | // 1 convolver with 32768 frames 114 | score += this.calcScore('Convolver-32768f-743ms', 1); 115 | groups ++; 116 | 117 | return score / groups; 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /js/Test.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-Present, Spotify AB. 3 | * 4 | * Licensed to the Apache Software Foundation (ASF) under one 5 | * or more contributor license agreements. See the NOTICE file 6 | * distributed with this work for additional information 7 | * regarding copyright ownership. The ASF licenses this file 8 | * to you under the Apache License, Version 2.0 (the 9 | * "License"); you may not use this file except in compliance 10 | * with the License. You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, 15 | * software distributed under the License is distributed on an 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | * KIND, either express or implied. See the License for the 18 | * specific language governing permissions and limitations 19 | * under the License. 20 | */ 21 | 22 | /** 23 | * Base class for tests. 24 | * 25 | * Tests are run in an OfflineAudioContext and the duration is measured. 26 | */ 27 | class Test { 28 | /** 29 | * @param {string} name The name of the test. 30 | * @param {number} numNodes Number of nodes used in the test. The test duration will be divided by this number to give the duration per node. Choose a number which makes your test take > 500 ms to run. 31 | * @param {number} [durationFactor=1] For slow tests, you can set a value less than 1 to shorten the render duration. 32 | */ 33 | constructor(name, numNodes, durationFactor) { 34 | this.name = name; 35 | this.numNodes = numNodes; 36 | this.durationFactor = durationFactor || 1.0; 37 | } 38 | 39 | /** 40 | * Builds the audio graph. Override in subclasses. 41 | * 42 | * @param ctx {OfflineAudioContext} The OfflineAudioContext. 43 | * @param last {AudioNode} The OscillatorNode, which is the last node in the graph, so far. 44 | * @returns {AudioNode} The resulting last AudioNode of the graph, which will be connected to ctx.destination. 45 | */ 46 | buildGraph(ctx, last) { 47 | // default is pass through 48 | return last; 49 | } 50 | 51 | /** 52 | * Runs the test. 53 | * 54 | * @param duration {Number} number of seconds to render audio. 55 | * @returns {Promise} the duration of the test in seconds. 56 | */ 57 | run(duration) { 58 | return new Promise((resolve, reject) => { 59 | const sampleRate = 44100; 60 | const ctx = new (window.OfflineAudioContext || window.webkitOfflineAudioContext)(1, sampleRate * duration, sampleRate); 61 | 62 | try { 63 | // we use an AudioBufferSourceNode as baseline. 64 | // We set playbackRate to something other than 1 so the node needs to do a bit of work. 65 | // This gives more stable results than having playbackRate of 1. 66 | const source = ctx.createBufferSource(); 67 | const buffer = ctx.createBuffer(1, sampleRate, sampleRate); 68 | const chan = buffer.getChannelData(0); 69 | for(let i = 0; i < chan.length; i++) { 70 | chan[i] = Math.sin(Math.PI * 2 * i * 440 / 44100); 71 | } 72 | source.buffer = buffer; 73 | source.loop = true; 74 | source.loopStart = 0; 75 | source.loopEnd = 1; 76 | source.playbackRate.value = 0.9; 77 | source.start(0); 78 | 79 | let last = this.buildGraph(ctx, source); 80 | 81 | last.connect(ctx.destination); 82 | } catch(e) { 83 | reject(e); 84 | return; 85 | } 86 | 87 | let startTime; 88 | // cooling period 89 | setTimeout(() => { 90 | startTime = window.performance.now(); 91 | ctx.startRendering(); 92 | }, 10); 93 | 94 | ctx.oncomplete = (event) => { 95 | const endTime = event.timeStamp; 96 | const duration = (endTime - startTime) / 1000; 97 | resolve(duration); 98 | }; 99 | }); 100 | } 101 | } 102 | 103 | class BiquadFilterTest extends Test { 104 | constructor(value) { 105 | super('Biquad-' + value, 7); 106 | this.value = value; 107 | } 108 | 109 | buildGraph(ctx, last) { 110 | for (let i = 0; i < this.numNodes; i++) { 111 | const node = ctx.createBiquadFilter(); 112 | if (this.value === 'default') { 113 | // nothing 114 | } else { 115 | node.frequency.value = this.value; 116 | } 117 | last.connect(node); 118 | last = node; 119 | } 120 | return last; 121 | } 122 | } 123 | 124 | class BiquadFilterAutomationTest extends Test { 125 | constructor(rampType, automationRate) { 126 | super('BiquadAutomation-' + rampType + '-' + automationRate, 3); 127 | this.rampType = rampType; 128 | this.automationRate = automationRate; 129 | } 130 | 131 | buildGraph(ctx, last) { 132 | const curve = new Float32Array([1.1, 1.2, 1.3, 1.6, 2.0]); 133 | for (let i = 0; i < this.numNodes; i++) { 134 | const node = ctx.createBiquadFilter(); 135 | node.Q.setValueAtTime(1, 0); 136 | if (this.rampType === 'exp') { 137 | node.Q.exponentialRampToValueAtTime(800, 1000); 138 | node.Q.exponentialRampToValueAtTime(1, 1001); 139 | node.Q.exponentialRampToValueAtTime(800, 1002); 140 | } else if (this.rampType === 'linear') { 141 | node.Q.linearRampToValueAtTime(800, 1000); 142 | node.Q.linearRampToValueAtTime(1, 1001); 143 | node.Q.linearRampToValueAtTime(800, 1002); 144 | } else if (this.rampType === 'target') { 145 | node.Q.setTargetAtTime(800, 0.1, 1000); 146 | node.Q.setTargetAtTime(1, 1000, 1000); 147 | node.Q.setTargetAtTime(800, 1001, 1000); 148 | } else if (this.rampType === 'curve') { 149 | node.Q.setValueCurveAtTime(curve, 0.1, 1000); 150 | node.Q.setValueCurveAtTime(curve, 1001, 1); 151 | node.Q.setValueCurveAtTime(curve, 1003, 1); 152 | } else { 153 | throw new Error('Bad rampType: ' + this.rampType); 154 | } 155 | node.frequency.automationRate = this.automationRate; 156 | node.gain.automationRate = this.automationRate; 157 | node.Q.automationRate = this.automationRate; 158 | node.detune.automationRate = this.automationRate; 159 | last.connect(node); 160 | last = node; 161 | } 162 | return last; 163 | } 164 | } 165 | 166 | class ConvolverTest extends Test { 167 | constructor(length, nameSuffix, numNodes, durationFactor) { 168 | super('Convolver-' + nameSuffix, numNodes, durationFactor); 169 | this.length = length; 170 | } 171 | 172 | buildGraph(ctx, last) { 173 | const buffer = ctx.createBuffer(1, this.length, ctx.sampleRate); 174 | 175 | // make sure it is not all zeros or Firefox will optimise away fft 176 | const chan = buffer.getChannelData(0); 177 | for(let i = 0; i < chan.length; i++) { 178 | chan[i] = Math.sin(Math.PI * 2 * i * 440 / 44100); 179 | } 180 | 181 | for(let i = 0; i < this.numNodes; i++) { 182 | const node = ctx.createConvolver(); 183 | node.buffer = buffer; 184 | last.connect(node); 185 | last = node; 186 | } 187 | return last; 188 | } 189 | } 190 | 191 | class WaveShaperTest extends Test { 192 | constructor(oversample, nameSuffix, numNodes) { 193 | super('WaveShaper-' + nameSuffix, numNodes); 194 | this.oversample = oversample; 195 | } 196 | 197 | buildGraph(ctx, last) { 198 | // basic curve data will do 199 | const curve = new Float32Array([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]); 200 | 201 | for(let i = 0; i < this.numNodes; i++) { 202 | const node = ctx.createWaveShaper(); 203 | node.oversample = this.oversample; 204 | node.curve = curve; 205 | last.connect(node); 206 | last = node; 207 | } 208 | 209 | return last; 210 | } 211 | } 212 | 213 | class GainTest extends Test { 214 | constructor(gain, automationRate, nameSuffix) { 215 | super('Gain-' + nameSuffix, 30); 216 | this.gain = gain; 217 | this.automationRate = automationRate; 218 | } 219 | 220 | buildGraph(ctx, last) { 221 | for (let i = 0; i < this.numNodes; i++) { 222 | const node = ctx.createGain(); 223 | if (this.gain !== 'default') { 224 | node.gain.value = this.gain; 225 | } 226 | if (this.automationRate !== '') { 227 | node.gain.automationRate = this.automationRate; 228 | } 229 | last.connect(node); 230 | last = node; 231 | } 232 | return last; 233 | } 234 | } 235 | 236 | class GainCancelTest extends Test { 237 | constructor(gain, automationRate, nameSuffix) { 238 | super('GainCancel-' + nameSuffix, 30); 239 | this.gain = gain; 240 | this.automationRate = automationRate; 241 | } 242 | 243 | buildGraph(ctx, last) { 244 | for (let i = 0; i < this.numNodes; i++) { 245 | const node = ctx.createGain(); 246 | if (this.gain !== 'default') { 247 | node.gain.value = this.gain; 248 | node.gain.cancelScheduledValues(0.001); 249 | } 250 | if (this.automationRate !== '') { 251 | node.gain.automationRate = this.automationRate; 252 | } 253 | last.connect(node); 254 | last = node; 255 | } 256 | return last; 257 | } 258 | } 259 | 260 | class GainAutomationTest extends Test { 261 | constructor(rampType, automationRate) { 262 | super('GainAutomation-' + rampType + '-' + automationRate, 12); 263 | this.rampType = rampType; 264 | this.automationRate = automationRate; 265 | } 266 | 267 | buildGraph(ctx, last) { 268 | const curve = new Float32Array([1.1, 1.2, 1.3, 1.6, 2.0]); 269 | for (let i = 0; i < this.numNodes; i++) { 270 | const node = ctx.createGain(); 271 | node.gain.automationRate = this.automationRate; 272 | node.gain.setValueAtTime(1.01, 0); 273 | if (this.rampType === 'exp') { 274 | node.gain.exponentialRampToValueAtTime(800, 1000); 275 | node.gain.exponentialRampToValueAtTime(1, 1001); 276 | node.gain.exponentialRampToValueAtTime(800, 1002); 277 | } else if (this.rampType === 'linear') { 278 | node.gain.linearRampToValueAtTime(800, 1000); 279 | node.gain.linearRampToValueAtTime(1, 1001); 280 | node.gain.linearRampToValueAtTime(800, 1002); 281 | } else if (this.rampType === 'target') { 282 | node.gain.setTargetAtTime(800, 0.1, 1000); 283 | node.gain.setTargetAtTime(1, 1000, 1000); 284 | node.gain.setTargetAtTime(800, 1001, 1000); 285 | } else if (this.rampType === 'curve') { 286 | node.gain.setValueCurveAtTime(curve, 0.1, 1000); 287 | node.gain.setValueCurveAtTime(curve, 1001, 1); 288 | node.gain.setValueCurveAtTime(curve, 1003, 1); 289 | } else { 290 | throw new Error('Bad rampType: ' + this.rampType); 291 | } 292 | node.gain.automationRate = this.automationRate; 293 | last.connect(node); 294 | last = node; 295 | } 296 | return last; 297 | } 298 | } 299 | 300 | class GainAutomationConnTest extends Test { 301 | constructor(automationRate) { 302 | super('GainAutomationConn-' + automationRate, 12); 303 | this.automationRate = automationRate; 304 | } 305 | 306 | buildGraph(ctx, last) { 307 | const source = last; 308 | for (let i = 0; i < this.numNodes; i++) { 309 | const node = ctx.createGain(); 310 | node.gain.automationRate = this.automationRate; 311 | source.connect(node.gain); 312 | last.connect(node); 313 | last = node; 314 | } 315 | return last; 316 | } 317 | } 318 | 319 | class CompressorTest extends Test { 320 | constructor(knee) { 321 | super('Compressor-knee-' + knee, 1); 322 | this.knee = knee; 323 | } 324 | 325 | buildGraph(ctx, last) { 326 | const node = ctx.createDynamicsCompressor(); 327 | node.knee.value = this.knee; 328 | last.connect(node); 329 | last = node; 330 | return last; 331 | } 332 | } 333 | 334 | class OscillatorTest extends Test { 335 | constructor() { 336 | super('Oscillator', 3); 337 | } 338 | 339 | buildGraph(ctx, last) { 340 | for (let i = 0; i < this.numNodes; i++) { 341 | const node = ctx.createOscillator(); 342 | node.start(0); 343 | node.connect(ctx.destination); 344 | } 345 | return last; 346 | } 347 | } 348 | 349 | class OscillatorAutomationTest extends Test { 350 | constructor(rampType, automationRate) { 351 | super('Oscillator.frequency-' + rampType + '-' + automationRate, 3); 352 | this.rampType = rampType; 353 | this.automationRate = automationRate; 354 | } 355 | 356 | buildGraph(ctx, last) { 357 | for (let i = 0; i < this.numNodes; i++) { 358 | const node = ctx.createOscillator(); 359 | node.frequency.automationRate = this.automationRate; 360 | node.frequency.setValueAtTime(100, 0); 361 | if (this.rampType == "linear") { 362 | node.frequency.linearRampToValueAtTime(4000, 1000); 363 | } else { 364 | throw new Error('Bad rampType: ' + this.rampType); 365 | } 366 | node.start(0); 367 | node.connect(ctx.destination); 368 | } 369 | return last; 370 | } 371 | } 372 | 373 | class AudioBufferSourceTest extends Test { 374 | constructor(rate, looping, numNodes) { 375 | // Preserve old name if we're looping (since that's what was happening before. 376 | // If we're not looping, add a suffix. 377 | super('AudioBufferSource-rate' + rate + (looping ? '' : '-noloop'), numNodes); 378 | this.rate = rate; 379 | this.looping = looping; 380 | } 381 | 382 | buildGraph(ctx, last) { 383 | const bufferLength = this.looping ? ctx.sampleRate : ctx.length; 384 | 385 | // Buffer for the AudioBufferSourceNode. This can be shared 386 | // between all the nodes. 387 | const buffer = ctx.createBuffer(1, bufferLength, ctx.sampleRate); 388 | 389 | // make sure it is not all zeros or Firefox will optimise away fft 390 | const chan = buffer.getChannelData(0); 391 | for(let i = 0; i < chan.length; i++) { 392 | chan[i] = Math.sin(Math.PI * 2 * i * 440 / 44100); 393 | } 394 | 395 | for (let i = 0; i < this.numNodes; i++) { 396 | const node = ctx.createBufferSource(); 397 | node.buffer = buffer; 398 | if (this.looping) { 399 | node.loopStart = 0; 400 | node.loopEnd = 1; 401 | node.loop = true; 402 | } 403 | if (this.rate !== 1) { 404 | node.playbackRate.value = this.rate; 405 | } 406 | node.start(0); 407 | node.connect(ctx.destination); 408 | } 409 | return last; 410 | } 411 | } 412 | 413 | class DelayTest extends Test { 414 | constructor(delay) { 415 | super('Delay-' + delay, 2); 416 | this.delay = delay; 417 | } 418 | 419 | buildGraph(ctx, last) { 420 | for (let i = 0; i < this.numNodes; i++) { 421 | const node = ctx.createDelay(0.2); 422 | if (this.delay === 'default') { 423 | // use defaults 424 | } else { 425 | node.delayTime.value = this.delay; 426 | } 427 | last.connect(node); 428 | last = node; 429 | } 430 | return last; 431 | } 432 | } 433 | 434 | class DelayAutomationTest extends Test { 435 | constructor(automationRate) { 436 | super('DelayAutomation-' + automationRate, 2); 437 | this.automationRate = automationRate; 438 | } 439 | 440 | buildGraph(ctx, last) { 441 | for (let i = 0; i < this.numNodes; i++) { 442 | const node = ctx.createDelay(0.2); 443 | node.delayTime.setValueAtTime(0, 0); 444 | node.delayTime.setValueAtTime(0.1, 1000); 445 | node.delayTime.automationRate = this.automationRate; 446 | last.connect(node); 447 | last = node; 448 | } 449 | return last; 450 | } 451 | } 452 | 453 | class ChannelSplitterTest extends Test { 454 | constructor() { 455 | super('ChannelSplitter', 30); 456 | } 457 | 458 | buildGraph(ctx, last) { 459 | for (let i = 0; i < this.numNodes; i++) { 460 | const node = ctx.createChannelSplitter(2); 461 | last.connect(node); 462 | last = node; 463 | } 464 | return last; 465 | } 466 | } 467 | 468 | class ChannelMergerTest extends Test { 469 | constructor() { 470 | super('ChannelMerger', 30); 471 | } 472 | 473 | buildGraph(ctx, last) { 474 | for (let i = 0; i < this.numNodes; i++) { 475 | const node = ctx.createChannelMerger(2); 476 | last.connect(node); 477 | last = node; 478 | } 479 | return last; 480 | } 481 | } 482 | 483 | class AnalyserTest extends Test { 484 | constructor() { 485 | super('Analyser', 30); 486 | } 487 | 488 | buildGraph(ctx, last) { 489 | for (let i = 0; i < this.numNodes; i++) { 490 | const node = ctx.createAnalyser(); 491 | last.connect(node); 492 | last = node; 493 | } 494 | return last; 495 | } 496 | } 497 | 498 | class PannerTest extends Test { 499 | constructor(panningModel, numNodes) { 500 | super('Panner-' + panningModel, numNodes); 501 | this.panningModel = panningModel; 502 | } 503 | 504 | buildGraph(ctx, last) { 505 | for (let i = 0; i < this.numNodes; i++) { 506 | const node = ctx.createPanner(); 507 | // setPosition is deprecated but supported by all browsers, .positionX isn't 508 | node.setPosition(1, 1, 1); 509 | node.panningModel = this.panningModel; 510 | last.connect(node); 511 | last = node; 512 | } 513 | return last; 514 | } 515 | } 516 | 517 | class StereoPannerTest extends Test { 518 | constructor(pan) { 519 | super('StereoPanner-' + pan, 20); 520 | this.pan = pan; 521 | } 522 | 523 | buildGraph(ctx, last) { 524 | for (let i = 0; i < this.numNodes; i++) { 525 | const node = ctx.createStereoPanner(); 526 | // always treat input as mono 527 | node.channelCount = 1; 528 | node.channelCountMode = 'explicit'; 529 | if (this.pan === 'default') { 530 | // default 531 | } else { 532 | node.pan.value = this.pan; 533 | } 534 | last.connect(node); 535 | last = node; 536 | } 537 | return last; 538 | } 539 | } 540 | -------------------------------------------------------------------------------- /js/WebAudioBenchApplication.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-Present, Spotify AB. 3 | * 4 | * Licensed to the Apache Software Foundation (ASF) under one 5 | * or more contributor license agreements. See the NOTICE file 6 | * distributed with this work for additional information 7 | * regarding copyright ownership. The ASF licenses this file 8 | * to you under the Apache License, Version 2.0 (the 9 | * "License"); you may not use this file except in compliance 10 | * with the License. You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, 15 | * software distributed under the License is distributed on an 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 | * KIND, either express or implied. See the License for the 18 | * specific language governing permissions and limitations 19 | * under the License. 20 | */ 21 | 22 | class WebAudioBenchApplication { 23 | constructor() { 24 | this.testsSelection = document.querySelector('.tests-selection select'); 25 | const tests = this.getTestList(); 26 | 27 | const urlParams = new URLSearchParams(window.location.search); 28 | 29 | // Expression for matching test names that we want to 30 | // select for testing. Use something like 31 | // "localhost/?pattern=Oscillator" if you want to select all 32 | // Oscillator tests. 33 | let testPattern; 34 | if (urlParams.has('pattern')) { 35 | testPattern = urlParams.get('pattern'); 36 | } 37 | 38 | tests.forEach((test) => { 39 | const option = document.createElement('option'); 40 | option.value = test.name; 41 | option.innerText = test.name; 42 | if (testPattern) { 43 | option.selected = option.value.indexOf(testPattern) !== -1; 44 | } else { 45 | option.selected = true; 46 | } 47 | this.testsSelection.appendChild(option); 48 | }); 49 | 50 | const runsInputElement = document.querySelector('.run-settings .runs input'); 51 | const durationInputElement = document.querySelector('.run-settings .duration input'); 52 | const ua = navigator.userAgent.toLowerCase(); 53 | if (ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1) { 54 | // Safari. 55 | // 1ms resolution forces us to run longer tests, giving lower precision. 56 | // See https://github.com/w3c/hr-time/issues/56 57 | runsInputElement.value = 50; 58 | durationInputElement.value = 200; 59 | } else { 60 | // Other browsers. 61 | runsInputElement.value = 500; 62 | durationInputElement.value = 20; 63 | } 64 | 65 | // Update the runs and duration from the URL, if given 66 | if (urlParams.has('runs')) { 67 | runsInputElement.value = urlParams.get('runs'); 68 | } 69 | if (urlParams.has('sec')) { 70 | durationInputElement.value = urlParams.get('sec'); 71 | } 72 | // Verbosity level of console logs. Higher means more logs. 0 73 | // means the least. 74 | if (urlParams.has('verbosity')) { 75 | this.verbosity = urlParams.get('verbosity'); 76 | } else { 77 | this.verbosity = 99; 78 | } 79 | 80 | this.resultsTable = document.querySelector('.results-table'); 81 | this.runButton = document.querySelector('.run-button'); 82 | this.runButton.addEventListener('mousedown', () => { 83 | const testRuns = parseInt(runsInputElement.value); 84 | if (Number.isNaN(testRuns) || testRuns < 1) { 85 | alert("The number of test runs is invalid."); 86 | return; 87 | } 88 | 89 | const defaultRenderDuration = parseInt(durationInputElement.value); 90 | if (Number.isNaN(defaultRenderDuration) || defaultRenderDuration < 1) { 91 | alert("The duration is invalid."); 92 | return; 93 | } 94 | 95 | const testNames = []; 96 | for(let i = 0; i < this.testsSelection.options.length; i++) { 97 | const option = this.testsSelection.options[i]; 98 | if (option.selected) { 99 | testNames.push(option.value); 100 | } 101 | } 102 | 103 | if(testNames.length === 0) { 104 | alert("Please select one or more tests."); 105 | return; 106 | } 107 | 108 | this.runButton.disabled = true; 109 | 110 | this.resultsTable.querySelectorAll('tr:not(.header)').forEach(elem => elem.remove()); 111 | this.testResults = {}; 112 | 113 | 114 | const origText = this.runButton.innerText; 115 | this.runButton.innerText = '...'; 116 | this.runTests(testNames, testRuns, defaultRenderDuration).finally(() => { 117 | this.runButton.innerText = origText; 118 | this.runButton.disabled = false; 119 | }); 120 | }); 121 | } 122 | 123 | getTestList() { 124 | return [ 125 | new BiquadFilterTest('default'), 126 | new BiquadFilterTest(440), 127 | new AudioBufferSourceTest(1.0, true, 20), 128 | new AudioBufferSourceTest(0.9, true, 8), 129 | new AudioBufferSourceTest(1.0, false, 20), 130 | new AudioBufferSourceTest(0.9, false, 8), 131 | new OscillatorTest(), 132 | new OscillatorAutomationTest('linear', 'a-rate'), 133 | new OscillatorAutomationTest('linear', 'k-rate'), 134 | new GainTest('default', '', 'default'), 135 | new GainTest(1.0, '', '1.0'), 136 | new GainTest(0.9, '', '0.9'), 137 | new GainTest(0.9, 'k-rate', '0.9-k-rate'), 138 | new GainCancelTest(1.0, '', '1.0'), 139 | new GainCancelTest(0.9, '', '0.9'), 140 | new GainCancelTest(0.9, 'k-rate', '0.9-k-rate'), 141 | new GainAutomationTest('exp', 'a-rate'), 142 | new GainAutomationTest('linear', 'a-rate'), 143 | new GainAutomationTest('target', 'a-rate'), 144 | new GainAutomationTest('curve', 'a-rate'), 145 | new GainAutomationTest('exp', 'k-rate'), 146 | new GainAutomationTest('linear', 'k-rate'), 147 | new GainAutomationTest('target', 'k-rate'), 148 | new GainAutomationTest('curve', 'k-rate'), 149 | new GainAutomationConnTest('a-rate'), 150 | new GainAutomationConnTest('k-rate'), 151 | new BiquadFilterAutomationTest('exp', 'a-rate'), 152 | new BiquadFilterAutomationTest('linear', 'a-rate'), 153 | new BiquadFilterAutomationTest('target', 'a-rate'), 154 | new BiquadFilterAutomationTest('curve', 'a-rate'), 155 | new BiquadFilterAutomationTest('exp', 'k-rate'), 156 | new BiquadFilterAutomationTest('linear', 'k-rate'), 157 | new BiquadFilterAutomationTest('target', 'k-rate'), 158 | new BiquadFilterAutomationTest('curve', 'k-rate'), 159 | new DelayTest('default'), 160 | new DelayTest(0.1), 161 | new DelayAutomationTest('a-rate'), 162 | new DelayAutomationTest('k-rate'), 163 | new ChannelSplitterTest(), 164 | new ChannelMergerTest(), 165 | new AnalyserTest(), 166 | new WaveShaperTest('none', '1x', 6), 167 | new WaveShaperTest('2x', '2x', 2), 168 | new WaveShaperTest('4x', '4x', 1), 169 | new CompressorTest(0), 170 | new CompressorTest(40), 171 | new ConvolverTest(128, '128f-3ms', 3, 1), 172 | new ConvolverTest(1024, '1024f-23ms', 1, 1), 173 | new ConvolverTest(2048, '2048f-46ms', 1, 1), 174 | new ConvolverTest(32768, '32768f-743ms', 1, 0.6), 175 | new PannerTest('equalpower', 5), 176 | new PannerTest('HRTF', 1), 177 | new StereoPannerTest('default'), 178 | new StereoPannerTest(0), 179 | new StereoPannerTest(0.2), 180 | new StereoPannerTest(1.0), 181 | ]; 182 | } 183 | 184 | runTests(testNames, testRuns, defaultRenderDuration) { 185 | const tests = this.getTestList(); 186 | const baselineTest = new Test('Baseline', 1, 4); 187 | let baseline = 0; 188 | let chain = this.runTest(baselineTest, testRuns, defaultRenderDuration).then((durations) => { 189 | baseline = Math.min(...durations); 190 | this.storeResult(baselineTest.name, durations); 191 | }); 192 | 193 | tests.filter((test) => testNames.indexOf(test.name) !== -1).forEach((test) => { 194 | chain = chain.then(() => { 195 | return this.runTest(test, testRuns, defaultRenderDuration).then((durations) => { 196 | durations = durations.map((d) => (d - baseline) / test.numNodes); 197 | this.storeResult(test.name, durations); 198 | }, (error) => { 199 | this.storeError(test.name, error); 200 | }); 201 | }); 202 | }); 203 | 204 | chain = chain.then(() => { 205 | const benchmark = new MixedBenchmark(this.testResults); 206 | const score = benchmark.calculate(); 207 | this.outputResult(benchmark.name, score, score, score, score, score, false); 208 | }); 209 | return chain; 210 | } 211 | 212 | runTest(test, numRuns, defaultRenderSeconds) { 213 | const renderSeconds = defaultRenderSeconds * test.durationFactor; 214 | const durations = []; 215 | let chain = undefined; 216 | console.log('Running ' + test.name); 217 | for(let i = 0; i < numRuns; i++) { 218 | if (chain === undefined) { 219 | chain = test.run(renderSeconds); 220 | } else { 221 | chain = chain.then((_) => test.run(renderSeconds)); 222 | } 223 | chain = chain.then((duration) => { 224 | if (this.verbosity >= 99) { 225 | console.log('took ' + Math.round(duration * 1000) + ' ms'); 226 | } 227 | duration /= renderSeconds; 228 | durations.push(duration); 229 | return durations; 230 | }); 231 | } 232 | return chain; 233 | } 234 | 235 | storeError(name, error) { 236 | console.warn(name + ' returned error: ', error); 237 | 238 | this.outputResult(name, 0, 0, 0, 0, 0, false); 239 | } 240 | 241 | storeResult(name, durations) { 242 | let mean = 0; 243 | let stddev = 0; 244 | 245 | for (let k = 0; k < durations.length; ++k) { 246 | mean += durations[k]; 247 | } 248 | mean = mean / durations.length; 249 | 250 | for (let k = 0; k < durations.length; ++k) { 251 | let diff = durations[k] - mean; 252 | stddev += diff * diff; 253 | } 254 | stddev = Math.sqrt(stddev / (durations.length - 1)); 255 | 256 | durations = durations.map((d) => Math.round(d * 1000 * 1000)); 257 | durations.sort((a, b) => a - b); 258 | 259 | const min = durations[0]; 260 | const q1 = durations[Math.floor(durations.length * 0.25)]; 261 | const median = durations[Math.floor(durations.length / 2)]; 262 | const q3 = durations[Math.floor(durations.length * 0.75)]; 263 | const max = durations[durations.length - 1]; 264 | 265 | console.log('Test ' + name); 266 | console.log(`Stats: (${min},${min},${q1},${median},${q3},${max},${1e6 * mean},${1e6 * stddev})`); 267 | console.log('min ' + min + ' (' + durations + ')'); 268 | console.log('mean = ', mean); 269 | console.log('stddev = ', stddev); 270 | 271 | this.testResults[name] = min; 272 | 273 | const showDetails = durations.length >= 5; 274 | this.outputResult(name, min, q1, median, q3, max, mean, stddev, showDetails); 275 | } 276 | 277 | outputResult(name, min, q1, median, q3, max, mean, stddev, showDetails) { 278 | const cells = [name, "" + Math.round(min)]; 279 | if (showDetails) { 280 | [min, q1, median, q3, max].map(v => "" + Math.round(v)).forEach(v => cells.push(v)); 281 | [mean, stddev].map(v => "" + Math.round(v*1000*1000*100)/100).forEach(v => cells.push(v)); 282 | } 283 | this.outputRow(cells); 284 | } 285 | 286 | outputRow(cells) { 287 | const tr = document.createElement('tr'); 288 | 289 | cells.forEach((v => { 290 | const td = document.createElement('td'); 291 | td.innerText = v; 292 | tr.appendChild(td); 293 | })); 294 | 295 | this.resultsTable.appendChild(tr); 296 | } 297 | } 298 | --------------------------------------------------------------------------------