time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

async.js (4252B)


      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 // VARIABLES //
     22 
     23 var noop = 'next();';
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Generates a source code body for asynchronous execution.
     30 *
     31 * ## Notes
     32 *
     33 * -   Example output:
     34 *
     35 *     ```javascript
     36 *     "use strict";
     37 *
     38 *     var ctx = this;
     39 *     var state1 = {};
     40 *     var t1, d1, i1;
     41 *
     42 *     function before( state, next ) {
     43 *         // {{before}}
     44 *     }
     45 *
     46 *     function cb1( error ) {
     47 *         if ( error ) {
     48 *             return ctx.done( error );
     49 *         }
     50 *         i1 = 0;
     51 *         t1 = ctx.tic();
     52 *         main( state1, cb2 );
     53 *     }
     54 *
     55 *     function main( state, next ) {
     56 *         // {{code}}
     57 *     }
     58 *
     59 *     function cb2( error ) {
     60 *         if ( error ) {
     61 *             return ctx.done( error );
     62 *         }
     63 *         i1 += 1;
     64 *         if ( i1 < 1e6 ) {
     65 *             return main( state1, cb2 );
     66 *         }
     67 *         d1 = ctx.toc( t1 );
     68 *         after( state1, cb3 );
     69 *     }
     70 *
     71 *     function after( state, next ) {
     72 *         // {{after}}
     73 *     }
     74 *
     75 *     function cb3( error ) {
     76 *         if ( error ) {
     77 *             return ctx.done( error );
     78 *         }
     79 *         ctx.done( null, d1 );
     80 *     }
     81 *
     82 *     before( state1, cb1 );
     83 *
     84 *     return 1;
     85 *     ```
     86 *
     87 *
     88 * @private
     89 * @param {number} id - id
     90 * @param {string} code - code to time
     91 * @param {Options} opts - function options
     92 * @param {string} opts.before - setup code
     93 * @param {string} opts.after - cleanup code
     94 * @param {PositiveInteger} opts.iterations - number of iterations
     95 * @returns {string} source code body
     96 */
     97 function body( id, code, opts ) {
     98 	var before;
     99 	var after;
    100 	var state;
    101 	var main;
    102 	var src;
    103 	var cb1;
    104 	var cb2;
    105 	var cb3;
    106 	var ctx;
    107 	var t;
    108 	var d;
    109 	var i;
    110 
    111 	src = '"use strict";';
    112 
    113 	// Define identifiers:
    114 	before = '__before$'+id+'__';
    115 	after = '__after$'+id+'__';
    116 	state = '__state$'+id+'__';
    117 	main = '__main$'+id+'__';
    118 	cb1 = '__cb1$'+id+'__';
    119 	cb2 = '__cb2$'+id+'__';
    120 	cb3 = '__cb3$'+id+'__';
    121 	ctx = '__ctx$'+id+'__';
    122 	i = '__i$'+id+'__';
    123 	t = '__t$'+id+'__';
    124 	d = '__d$'+id+'__';
    125 
    126 	// Declare variables:
    127 	src += 'var '+ctx+' = this;';
    128 	src += 'var '+state+' = {};';
    129 	src += 'var '+i+','+t+','+d+';';
    130 
    131 	// Insert the setup code:
    132 	src += 'function '+before+'( state, next ) {';
    133 	src += '  '+(opts.before||noop)+';';
    134 	src += '}';
    135 
    136 	// Insert the setup callback:
    137 	src += 'function '+cb1+'( error ) {';
    138 	src += '  if ( error ) {';
    139 	src += '    return '+ctx+'.done( error );';
    140 	src += '  }';
    141 	src += '  '+i+' = 0;';
    142 	src += '  '+t+' = '+ctx+'.tic();'; // start the timer
    143 	src += '  '+main+'( '+state+','+cb2+' );';
    144 	src += '}';
    145 
    146 	// Wrap the code to time in a function:
    147 	src += 'function '+main+'( state, next ) {';
    148 	src += '  '+(code||noop)+';';
    149 	src += '}';
    150 
    151 	// Insert the main callback:
    152 	src += 'function '+cb2+'( error ) {';
    153 	src += '  if ( error ) {';
    154 	src += '    return '+ctx+'.done( error );';
    155 	src += '  }';
    156 	src += '  '+i+' += 1;';
    157 	src += '  if ( '+i+' < '+opts.iterations+' ) {';
    158 	src += '    return '+main+'( '+state+','+cb2+' );';
    159 	src += '  }';
    160 	src += '  '+d+' = '+ctx+'.toc( '+t+' );'; // stop the timer
    161 	src += '  '+after+'( '+state+','+cb3+' );';
    162 	src += '}';
    163 
    164 	// Insert the cleanup code:
    165 	src += 'function '+after+'( state, next ) {';
    166 	src += '  '+(opts.after||noop)+';';
    167 	src += '}';
    168 
    169 	// Insert the cleanup callback:
    170 	src += 'function '+cb3+'( error ) {';
    171 	src += '  if ( error ) {';
    172 	src += '    return '+ctx+'.done( error );';
    173 	src += '  }';
    174 	src += '  '+ctx+'.done( null,'+d+' );'; // return results
    175 	src += '}';
    176 
    177 	// Invoke the setup function to begin the execution sequence:
    178 	src += before+'( '+state+','+cb1+' );';
    179 
    180 	// Return a value:
    181 	src += 'return '+id+';';
    182 
    183 	return src;
    184 }
    185 
    186 
    187 // EXPORTS //
    188 
    189 module.exports = body;