JSPM

  • Created
  • Published
  • Downloads 50
  • Score
    100M100P100Q60017F
  • License GNU General Public License v3.0

The most high resolution timing in NodeJs that use processor command RDTSC

Package Exports

  • rdtsc

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rdtsc) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Description

The most high resolution timing in NodeJs

This module provide function rdtsc() that call C++ function __rdtsc() that call processor command RDTSC (Read Time Stamp Counter)

rdtsc() return counts the number of cycles since computer start

(rdtsc() - rdtsc()) is always < 0

Tested on Windows

Install

npm i rdtsc -S

Features

RDTSC

var { rdtsc } = require('rdtsc');
console.log(rdtsc()); // 3864063236708616 cycles
console.log(rdtsc() - rdtsc()); // -2971 cycles

Calc Performance

Syntax

calcPerformance(func0, func1, testTimeMilliseconds);
// result = <min cycles of func1> - <min cycles of func0>;

calcPerformance(null, func1, testTimeMilliseconds);
// result = <min cycles of func1>;

calcPerformance(func0, null, testTimeMilliseconds);
// result = <min cycles of func0>

Example

var { calcPerformance } = require('rdtsc');

var result = calcPerformance(
() => {

},
() => {
    Object.keys(Math);
},
1000);

console.log('"Object.keys(Math)" min cycles =', result); //about 20-40 cycles

Thread Priority

Implemented only for Windows platform

Examples

const { runInRealtimePriority, getThreadPriority, getProcessPriority } = require('rdtsc');

runInRealtimePriority(() => {
    console.log("getThreadPriority = ", getThreadPriority()); // === THREAD_PRIORITY_REALTIME
    console.log("getProcessPriority = ", getProcessPriority()); // === PROCESS_PRIORITY_REALTIME
});
const { setThreadPriority, getThreadPriority, isWin, THREAD_PRIORITY_REALTIME } = require('rdtsc');

if (isWin) {
    console.log(getThreadPriority()); // === THREAD_PRIORITY_NORMAL
} else {
    console.log(getThreadPriority()); // === undefined
}

var previousPriority = setThreadPriority(THREAD_PRIORITY_REALTIME);

try {
    <your code>
} finally {
    setThreadPriority(previousPriority);
}

Priorities list

const THREAD_PRIORITY_IDLE = -15;
const THREAD_PRIORITY_LOWEST = -2;
const THREAD_PRIORITY_BELOW_NORMAL = -1;
const THREAD_PRIORITY_NORMAL = 0;
const THREAD_PRIORITY_ABOVE_NORMAL = 1;
const THREAD_PRIORITY_HIGHEST = 2;
const THREAD_PRIORITY_REALTIME = 15; // THREAD_PRIORITY_REALTIME

Process Priority

Implemented only for Windows platform

Example

const { setProcessPriority, getProcessPriority, isWin, PROCESS_PRIORITY_REALTIME } = require('rdtsc');

if (isWin) {
    console.log(getProcessPriority()); // === PROCESS_PRIORITY_NORMAL
} else {
    console.log(getProcessPriority()); // === undefined
}

var previousPriority = setProcessPriority(PROCESS_PRIORITY_REALTIME);

try {
    <your code>
} finally {
    setProcessPriority(previousPriority);
}

Priorities list

const PROCESS_PRIORITY_IDLE = 0x00000040; // IDLE_PRIORITY_CLASS
const PROCESS_PRIORITY_BELOW_NORMAL = 0x00004000; // BELOW_NORMAL_PRIORITY_CLASS
const PROCESS_PRIORITY_NORMAL = 0x00000020; // NORMAL_PRIORITY_CLASS
const PROCESS_PRIORITY_ABOVE_NORMAL = 0x00008000; // ABOVE_NORMAL_PRIORITY_CLASS
const PROCESS_PRIORITY_HIGHEST = 0x00000080; // HIGH_PRIORITY_CLASS
const PROCESS_PRIORITY_REALTIME = 0x00000100; // REALTIME_PRIORITY_CLASS