Giter Site home page Giter Site logo

rajulbabel / promise-profiler Goto Github PK

View Code? Open in Web Editor NEW
2.0 0.0 1.0 610 KB

This repo will profile your code by giving the execution time of all promises in a json without disturbing the production level code.

Home Page: https://rajulbabel.github.io/promise-profiler/

License: MIT License

JavaScript 100.00%
nodejs profiler bluebird-promise promise javascript promise-profiler bluebird

promise-profiler's Introduction

Promise Profiler

This repo will profile your code by giving the execution time of all promises in a json without disturbing the production level code.

Documentation

  • The documentation can be found here.

Installation

  • Install bluebird-promise-profiler as dev dependency

     npm i --save-dev bluebird-promise-profiler
    
  • Please upgrade to the latest version for bug free experience.

  • Version >= 1.3.0 of this package works for all node versions.

Usage

  • We will implement routes and model for two function multiply and square (the working examples can be seen in ./examples of this repository).

  • File: ./routes/route.js

     'use strict';
     
     const Promise = require('bluebird');
     const Model = require('../models/Model');
     
     /**
      * Route for calling square function in ../models/Model.js
      * @param num
      * @returns {Promise}
      */
     const squareRoute = function squareRoute (num) {
     
     	return Model.square(num)
     		// the function name 'squarePromise' will be one of the key in output of the profiler whose value would be around 1000 milliseconds
     		.then(function squarePromise (result) {
     
     			return {result};
     
     		});
     };
     
     const squareAndMultiplyRoute = function squareAndMultiply (num1, num2) {
     
     	return Promise.join(Model.multiply(num1, num2), Model.square(num1))
     		// the function name 'spreadFunction' will be one of the key in output of the profiler whose value would be around 2000 milliseconds
     		.spread(function spreadFunction (multiplyResult, squareResult) {
     			return {multiplyResult, squareResult};
     		});
     
     };
     
     module.exports = {
     	squareRoute: squareRoute,
     	squareAndMultiplyRoute: squareAndMultiplyRoute
     };
     
  • File: ./models/Model.js

     'use strict';
     
     const Promise = require('bluebird');
     
     /**
      * Makes a delay of one second and returns back a promise which multiplies the two numbers.
      * @param num1
      * @param num2
      * @returns {bluebird} Returns back a promise which multiplies the two numbers.
      */
     const multiply = function multiply (num1, num2) {
     
     	return new Promise (function (resolve, reject) {
     
     		setTimeout(function promise1Timeout () {
     			resolve(num1 * num2);
     		}, 1000);
     
     	});
     
     };
     
     /**
      * Makes delay of one second and then calls multiply which further makes a delay of one second, so total delay is two seconds.
      * @param num1
      * @returns {bluebird} Returns back a promise which multiplies the given number.
      */
     const square = function square (num1) {
     
     	return new Promise (function (resolve, reject) {
     
     		setTimeout(function promise1Timeout () {
     
     			multiply(num1, num1)
     				// the function name 'multiplyPromise' will be one of the key in output of the profiler whose value would be around 1000 milliseconds
     				.then(function multiplyPromise (result) {
     					resolve(result);
     				});
     
     		}, 1000);
     
     	});
     
     };
     
     module.exports = {
     	square: square,
     	multiply: multiply
     };
     
  • The above are the files for routes and model respectively for squaring and multiplying numbers.

  • The multiply function in models causes a delay of one second and returns the output, therefore total time taken by square function is one seconds

  • The square function also makes a delay of one second and then calls the multiply function, therefore total time taken by square function is two seconds.

  • So now we need to profile this, without touching the code, now we make a temporary file called profiler.js

  • profiler.js

     'use strict';
     // to run this file type 'node profiler' in the terminal
    
     const route = require('./routes/route');
     
     const promiseProfiler = require('bluebird-promise-profiler');
     promiseProfiler.startProfiling();
     
     //call square route
     route.squareRoute(5).then((res) => {
     
     	// { result: 25 }
     	console.log(res);
     
     	// { multiplyPromise: 1004.3564850000002, squarePromise: 2009.1301549999998 }
     	console.log(promiseProfiler.getProfilerResult());
     
     	// reset profiler result to analyze other route
     	promiseProfiler.resetProfiler();
     
     	// call squareAndMultiply route
     	route.squareAndMultiplyRoute(4, 5).then((result) => {
     
     		// { multiplyResult: 20, squareResult: 16 }
     		console.log(result);
     
     		// note there is a multiplyPromise included in this response as square function internally calls multiply function
     		// { multiplyPromise: 1004.7086040000004, spreadFunction: 2008.4984540000005 }
     		console.log(promiseProfiler.getProfilerResult());
     
     		// stop the profiler once the profiling is done
     		promiseProfiler.stopProfiling();
     	});
     
     });
     
  • Profiling would be done according to the function names given in .then() and .spread() functions.

  • Anonymous functions given inside .then() or .spread() will not be profiled. So please do give names into those functions.

Examples

  • Steps to run examples in this repo:

     npm run-script examples
    

Tests

  • Test without code coverage

     npm test
    
  • Test with code coverage

     npm run-script test-with-coverage
    

promise-profiler's People

Contributors

rajulbabel avatar

Stargazers

 avatar  avatar

Forkers

jbriales

promise-profiler's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.