Skip to content

Commit 4b59ac3

Browse files
committed
Add Bernoulli streams
1 parent 672f8e0 commit 4b59ac3

25 files changed

+5465
-0
lines changed

lib/node_modules/@stdlib/random/streams/bernoulli/README.md

+538
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 Readable = require( 'readable-stream' ).Readable;
24+
var bench = require( '@stdlib/bench' );
25+
var pkg = require( './../package.json' ).name;
26+
var randomStream = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var s;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
s = randomStream( 0.3 );
38+
if ( typeof s !== 'object' ) {
39+
b.fail( 'should return an object' );
40+
}
41+
}
42+
b.toc();
43+
if ( !( s instanceof Readable ) ) {
44+
b.fail( 'should return a readable stream' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( pkg+'::seed', function benchmark( b ) {
51+
var opts;
52+
var s;
53+
var i;
54+
55+
opts = {
56+
'seed': 12345
57+
};
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
s = randomStream( 0.3, opts );
62+
if ( typeof s !== 'object' ) {
63+
b.fail( 'should return an object' );
64+
}
65+
}
66+
b.toc();
67+
if ( !( s instanceof Readable ) ) {
68+
b.fail( 'should return a readable stream' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
});
73+
74+
bench( pkg+':objectMode', function benchmark( b ) {
75+
var s;
76+
var i;
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
s = randomStream.objectMode( 0.3 );
81+
if ( typeof s !== 'object' ) {
82+
b.fail( 'should return an object' );
83+
}
84+
}
85+
b.toc();
86+
if ( !( s instanceof Readable ) ) {
87+
b.fail( 'should return a readable stream' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
});
92+
93+
bench( pkg+'::factory', function benchmark( b ) {
94+
var createStream;
95+
var s;
96+
var i;
97+
98+
createStream = randomStream.factory( 0.3 );
99+
100+
b.tic();
101+
for ( i = 0; i < b.iterations; i++ ) {
102+
s = createStream();
103+
if ( typeof s !== 'object' ) {
104+
b.fail( 'should return an object' );
105+
}
106+
}
107+
b.toc();
108+
if ( !( s instanceof Readable ) ) {
109+
b.fail( 'should return a readable stream' );
110+
}
111+
b.pass( 'benchmark finished' );
112+
b.end();
113+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
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

Comments
 (0)