1.0.0 • Published 3 years ago

perf-tester v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

perf-tester

An extremely small, zero dependency module to test performance of any of your code.

It uses it's own timer module to measure time taken which you can include separately in your project too.

All functions are promise based.

Usage

import { PerfTest } from 'perf-tester';
// const { PerfTest } = require('perf-tester');

const tester = new PerfTest();
  • measure( callback )

    Function to measure time taken for the callback function to execute. Works similar to console.time but results can be stored in a variable.

    callback - The callback function to test for.

    @returns - Time taken in miliseconds.

    (async () => {
    
        const result = await tester.measure(async () => {
            // some code to test
        });
        
    	// Returns time taken in ms for the code in callback to execute
        console.log(result + "ms");
    
    })();
  • testIteratively( iterations, callback )

    Executes the callback repeatetively for no. of iterations provided while measuring time taken by each callback and calculates average time taken for each.

iterations - Number of iterations to test the callback. (Higher amount will take longer time)

callback - The callback function to test for.

@returns - Object

{

​ avgTimeTaken : number // Average time taken in miliseconds,

​ tests : number // Array storing time taken by each test

}

(async () => {
	
    // test the callback for 5 times
    const result = await tester.testIteratively(5, async () => {
        // some code to test
    });
    
	// Returns average time taken and array of time taken by each test
    console.log(result);

})();
  • batchTest ( iterations, array_of_callbacks )

    Similar to testIteratively but supports array of different callbacks to test for at once.

iterations - Number of iterations to test the callback. (Higher amount will take longer time)

array_of_callbacks - Array containing callbacks for whom testing should be done.

@returns - Array containing results for each test.

(async () => {

    // test the callbacks each time for 6 times
    const results = await tester.batchTest(6, [
        async () => {
            // some code to test
        },
        async () => {
            // some code to test
        }
    ]);

	// Array of results for each callback
    console.log(results);

})();

Any suggestions are warmly welcome.

1.0.1

3 years ago

1.0.0

3 years ago