GithubHelp home page GithubHelp logo

nho-sh / fu-counter Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 15 KB

A simple function usage counter for performance profiling backend and frontend javascript.

License: MIT License

JavaScript 84.07% Shell 15.93%

fu-counter's Introduction

fu-counter - Function usage counter

█▀▀ █  █   █▀▀ █▀▀█ █  █ █▀▀▄ ▀▀█▀▀ █▀▀ █▀▀█
█▀  █  █ - █   █  █ █  █ █  █   █   █▀  █▄▄▀
▀    ▀▀▀   ▀▀▀ ▀▀▀▀  ▀▀▀ ▀  ▀   ▀   ▀▀▀ ▀ ▀▀
           Function usage counter

Function usage counter is a small, basic call counter. It allows you to quickly throw some usage counters on functions, give em names, and later, print out the counts. With these counts, you can find parts of the code that execute a lot (and find places for performance tuning?).

  • 100% Test coverage
  • No dependencies
  • .mjs and .cjs support
  • Browser compatible

Reasoning

Q: "Hey, why don't you profile using the debugger? Much better!".
A: Sure, but if your application runs an algorithm for a few minutes, the debugger does not hold out, and crashes.

Usage

Counts are gathered in a (named) group. You can have multiple groups, or just one, whatever you like:

const group1Counts = FuCounter.create({
  name: 'group1'
});

// Now you can:
//  - pass it around
//  - stick it on a context
//  - assign to global
//  - ...

Next, start wrapping a function, which slighly degraded performance, due to the counting itself.

// Before:
module.exports = {
  doWork: () => {
    /// ... i work hard ...
  }
}

// After:
module.exports = {
  doWork: group1Counts('doWorkCount', () => {
    /// ... i work hard ...
  })
}

Finally, get some insights:

  • A stats function returns the count for each counter
  • A print function does a console.log with a nice human readable format
group1Counts.stats()
// Returns
/*
{
  doWorkCount: 123
}
*/

or

group1Counts.print()
// Prints to console.log
/*
group1 (fu counter):
  123 doWorkCount
   .. ...
    1 didOneWork
*/

Watching a running process

If you want to track the counts in a running process, you can use a watch: <milliseconds> option when creating the group. The print function will automatically be called using a setInterval.

const group = FuCounter.create({
  // ...
  watch: 1000 // group.print() every second
})

// You can stop watching as well:
group.stopWach();

Dealing with process crashes (node)

If your process has a tendency to crash, you can also get a final print before the process dies:

const group = FuCounter.create({
  // ...
  printOnExit: true // group.print() is called during process.on('exit', )
})

Browser support

If you want to get counts in a browser, simply load the file ./fu-counter.js into your browser, and window.FuCounter.create(...) will be available. (Do not load the index files in your browser, not supported)

fu-counter's People

Contributors

nho-sh avatar

Watchers

 avatar

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.