npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sql-performance-counters

v1.1.1

Published

Simple performance metrics for SQL queries.

Downloads

3

Readme

SQL Performance Counters

Version npm

Linux build

Performance metrics of database queries per query.

String literals and number constants are removed from queries using unparametrize-sql-query before adding an entry in the database.

The database is accessible as a Map on the PerformanceCounters.metrics property or as an object on PerformanceCounters.metricsAsObject and it looks something like this:

{ 
	"SELECT ? FROM table": {
		successCount: 1,
		errorCount: 0,
		successMillisecondsTotal: 50,
		errorMillisecondsTotal: 0,
		successMillisecondsAverage: 50,
		errorMillisecondsAverage: 0,
		fetchedRows: 1,
		affectedRows: 0,
		changedRows: 0
	},
	"SELECT ? FROM another_table": {
		successCount: 0,
		errorCount: 1,
		successMillisecondsTotal: 0,
		errorMillisecondsTotal: 3,
		successMillisecondsAverage: 0,
		errorMillisecondsAverage: 3,
		fetchedRows: 0,
		affectedRows: 0,
		changedRows: 0
	}
}

The above object format ca be rendered in a live HTML table in browsers using sql-performance-counters-ui.

Installation:

npm i sql-performance-counters

Usage

See tests.js and PerformanceCounters.js for usage.

You should use PerformanceCounters.js as a global singleton. For example, create a file named PerformanceCountersSingleton.js with these contents:

const PerformanceCounters = require("sql-performance-counters").PerformanceCounters;

module.exports = new PerformanceCounters();

Then throughout your application you may acces the same instance to get the metrics or record activity:

const performanceCounters = require("./PerformanceCountersSingleton");


performanceCounters.setLatency(50);

performanceCounters.onQuery();
console.log(performanceCounters.runningQueriesCount);

performanceCounters.onQuery();
console.log(performanceCounters.runningQueriesCount);


performanceCounters.onResult("SELECT 1 FROM table", /*nDurationMilliseconds*/ 100, /*nFetchedRows*/ 0);
console.log(performanceCounters.runningQueriesCount);

performanceCounters.onError("SELECT 1 FROM another_table", /*nDurationMilliseconds*/ 53, /*error*/ new Error("test"));
console.log(performanceCounters.runningQueriesCount);


const objMetrics = performanceCounters.metricsAsObject;
console.log(objMetrics);


performanceCounters.clear();

Usage with promise-mysql

If using MySQL and using promise-mysql, you may conveniently use the PerformanceCounters.onMySQLPromiseConnection() function to get started rapidly.

const MySQL = require("promise-mysql");

/* [...] */
const pool = MySQL.createPool(mysqljsConfigObject);

pool.on(
	"connection", 
	(connection) => {
		PerformanceCounters.onMySQLPromiseConnection(connection);

		/* [...] */
	}
);

(async() => {
	const connection = await MySQL.createConnection(mysqljsConfigObject);

	PerformanceCounters.onMySQLPromiseConnection(connection.connection);

	/* [...] */
})();

See PerformanceCounters.js for how PerformanceCounters.onMySQLPromiseConnection() works. 810279