|
| 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 WritableStream = require( 'readable-stream' ).Writable; // eslint-disable-line stdlib/no-redeclare |
| 24 | +var bench = require( '@stdlib/bench' ); |
| 25 | +var inherit = require( '@stdlib/utils/inherit' ); |
| 26 | +var pkg = require( './../package.json' ).name; |
| 27 | +var randomStream = require( './../lib' ); |
| 28 | + |
| 29 | + |
| 30 | +// MAIN // |
| 31 | + |
| 32 | +bench( pkg+'::throughput,baseline', function benchmark( b ) { |
| 33 | + var i; |
| 34 | + |
| 35 | + i = 0; |
| 36 | + b.tic(); |
| 37 | + |
| 38 | + return next(); |
| 39 | + |
| 40 | + function next() { |
| 41 | + i += 1; |
| 42 | + if ( i <= b.iterations ) { |
| 43 | + return process.nextTick( onTick ); |
| 44 | + } |
| 45 | + b.toc(); |
| 46 | + b.pass( 'benchmark finished' ); |
| 47 | + b.end(); |
| 48 | + } |
| 49 | + |
| 50 | + function onTick() { |
| 51 | + if ( i !== i ) { |
| 52 | + b.fail( 'should not be NaN' ); |
| 53 | + } |
| 54 | + next(); |
| 55 | + } |
| 56 | +}); |
| 57 | + |
| 58 | +bench( pkg+'::throughput:highWaterMark=<default>', function benchmark( b ) { |
| 59 | + var rStream; |
| 60 | + var wStream; |
| 61 | + var opts; |
| 62 | + var i; |
| 63 | + |
| 64 | + function Writable( opts ) { |
| 65 | + WritableStream.call( this, opts ); |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + inherit( Writable, WritableStream ); |
| 70 | + Writable.prototype._write = next; // eslint-disable-line no-underscore-dangle |
| 71 | + |
| 72 | + // Create a source stream: |
| 73 | + opts = {}; |
| 74 | + rStream = randomStream( 0.5, opts ); |
| 75 | + |
| 76 | + // Create a sink stream: |
| 77 | + opts = {}; |
| 78 | + wStream = new Writable( opts ); |
| 79 | + |
| 80 | + i = 0; |
| 81 | + b.tic(); |
| 82 | + |
| 83 | + return pipe(); |
| 84 | + |
| 85 | + function pipe() { |
| 86 | + // Begin data flow... |
| 87 | + rStream.pipe( wStream ); |
| 88 | + } |
| 89 | + |
| 90 | + function next( chunk, encoding, clbk ) { |
| 91 | + i += 1; |
| 92 | + if ( i < b.iterations ) { |
| 93 | + return clbk(); |
| 94 | + } |
| 95 | + b.toc(); |
| 96 | + rStream.destroy(); |
| 97 | + |
| 98 | + b.pass( 'benchmark finished' ); |
| 99 | + b.end(); |
| 100 | + } |
| 101 | +}); |
| 102 | + |
| 103 | +bench( pkg+'::throughput:highWaterMark=0', function benchmark( b ) { |
| 104 | + var rStream; |
| 105 | + var wStream; |
| 106 | + var opts; |
| 107 | + var i; |
| 108 | + |
| 109 | + function Writable( opts ) { |
| 110 | + WritableStream.call( this, opts ); |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + inherit( Writable, WritableStream ); |
| 115 | + Writable.prototype._write = next; // eslint-disable-line no-underscore-dangle |
| 116 | + |
| 117 | + // Create a source stream: |
| 118 | + opts = { |
| 119 | + 'highWaterMark': 0 |
| 120 | + }; |
| 121 | + rStream = randomStream( 0.5, opts ); |
| 122 | + |
| 123 | + // Create a sink stream: |
| 124 | + opts = {}; |
| 125 | + wStream = new Writable( opts ); |
| 126 | + |
| 127 | + i = 0; |
| 128 | + b.tic(); |
| 129 | + |
| 130 | + return pipe(); |
| 131 | + |
| 132 | + function pipe() { |
| 133 | + // Begin data flow... |
| 134 | + rStream.pipe( wStream ); |
| 135 | + } |
| 136 | + |
| 137 | + function next( chunk, encoding, clbk ) { |
| 138 | + i += 1; |
| 139 | + if ( i < b.iterations ) { |
| 140 | + return clbk(); |
| 141 | + } |
| 142 | + b.toc(); |
| 143 | + rStream.destroy(); |
| 144 | + |
| 145 | + b.pass( 'benchmark finished' ); |
| 146 | + b.end(); |
| 147 | + } |
| 148 | +}); |
| 149 | + |
| 150 | +bench( pkg+'::throughput,object_mode:highWaterMark=<default>', function benchmark( b ) { |
| 151 | + var rStream; |
| 152 | + var wStream; |
| 153 | + var opts; |
| 154 | + var i; |
| 155 | + |
| 156 | + function Writable( opts ) { |
| 157 | + WritableStream.call( this, opts ); |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + inherit( Writable, WritableStream ); |
| 162 | + Writable.prototype._write = next; // eslint-disable-line no-underscore-dangle |
| 163 | + |
| 164 | + // Create a source stream: |
| 165 | + opts = {}; |
| 166 | + rStream = randomStream.objectMode( 0.5, opts ); |
| 167 | + |
| 168 | + // Create a sink stream: |
| 169 | + opts = { |
| 170 | + 'objectMode': true |
| 171 | + }; |
| 172 | + wStream = new Writable( opts ); |
| 173 | + |
| 174 | + i = 0; |
| 175 | + b.tic(); |
| 176 | + |
| 177 | + return pipe(); |
| 178 | + |
| 179 | + function pipe() { |
| 180 | + // Begin data flow... |
| 181 | + rStream.pipe( wStream ); |
| 182 | + } |
| 183 | + |
| 184 | + function next( chunk, encoding, clbk ) { |
| 185 | + i += 1; |
| 186 | + if ( i < b.iterations ) { |
| 187 | + return clbk(); |
| 188 | + } |
| 189 | + b.toc(); |
| 190 | + rStream.destroy(); |
| 191 | + |
| 192 | + b.pass( 'benchmark finished' ); |
| 193 | + b.end(); |
| 194 | + } |
| 195 | +}); |
| 196 | + |
| 197 | +bench( pkg+'::throughput,object_mode:highWaterMark=0', function benchmark( b ) { |
| 198 | + var rStream; |
| 199 | + var wStream; |
| 200 | + var opts; |
| 201 | + var i; |
| 202 | + |
| 203 | + function Writable( opts ) { |
| 204 | + WritableStream.call( this, opts ); |
| 205 | + return this; |
| 206 | + } |
| 207 | + |
| 208 | + inherit( Writable, WritableStream ); |
| 209 | + Writable.prototype._write = next; // eslint-disable-line no-underscore-dangle |
| 210 | + |
| 211 | + // Create a source stream: |
| 212 | + opts = { |
| 213 | + 'highWaterMark': 0 |
| 214 | + }; |
| 215 | + rStream = randomStream.objectMode( 0.5, opts ); |
| 216 | + |
| 217 | + // Create a sink stream: |
| 218 | + opts = { |
| 219 | + 'objectMode': true |
| 220 | + }; |
| 221 | + wStream = new Writable( opts ); |
| 222 | + |
| 223 | + i = 0; |
| 224 | + b.tic(); |
| 225 | + |
| 226 | + return pipe(); |
| 227 | + |
| 228 | + function pipe() { |
| 229 | + // Begin data flow... |
| 230 | + rStream.pipe( wStream ); |
| 231 | + } |
| 232 | + |
| 233 | + function next( chunk, encoding, clbk ) { |
| 234 | + i += 1; |
| 235 | + if ( i < b.iterations ) { |
| 236 | + return clbk(); |
| 237 | + } |
| 238 | + b.toc(); |
| 239 | + rStream.destroy(); |
| 240 | + |
| 241 | + b.pass( 'benchmark finished' ); |
| 242 | + b.end(); |
| 243 | + } |
| 244 | +}); |
0 commit comments