const {performance} = require('perf_hooks');
let startTime = performance.now();
const extractedData = await readFunction()
let endTime = performance.now();
let timeTaken = endTime-startTime
I want to calculate time taken to execute readFunction(). Is this good approach to calculate total time taken to execute readFunction? Also on console.log(startTime) it gives some value instead of starting from 0.
So to get precise value should I do the following step or above step is enough to get precise time in milisecond.
const {performance} = require('perf_hooks');
let startTime = 0
const extractedData = await readFunction()
let endTime = performance.now();
let timeTaken = endTime-startTime
Is timeTaken value in milisecond? If anyone needs any further information please let me know.
function calculateTime(yourFunction) {
console.time();
yourFunction();
return console.timeEnd();
}
async function calculateTimeForPromises(yourPromise) {
console.time();
await yourPromise();
return console.timeEnd();
}
Is that helpful?
Related
Inside a function i have two blocks of code where i am trying to measure processing time of my api calls. Here's the code:
const start1 = new Date().getTime();
const trendingMoviesDay = await mainPageAPI.getTrendingMovies(true);
const trendingMoviesWeek = await mainPageAPI.getTrendingMovies();
const trendingTVDay = await mainPageAPI.getTrendingTV(true);
const trendingTVWeek = await mainPageAPI.getTrendingTV();
const popularMovies1 = await mainPageAPI.getPopularMovies(1);
const popularMovies2 = await mainPageAPI.getPopularMovies(2);
const popularTV1 = await mainPageAPI.getPopularTV(1);
const popularTV2 = await mainPageAPI.getPopularTV(2);
const end1 = new Date().getTime();
console.log(end1 - start1);
const start2 = new Date().getTime();
const results = await Promise.all(
[
mainPageAPI.getTrendingMovies(true),
mainPageAPI.getTrendingMovies(),
mainPageAPI.getTrendingTV(true),
mainPageAPI.getTrendingTV(),
mainPageAPI.getPopularMovies(1),
mainPageAPI.getPopularMovies(2),
mainPageAPI.getPopularTV(1),
mainPageAPI.getPopularTV(2)
]
);
const end2 = new Date().getTime();
console.log('promiseAll',end2 - start2);
In this case first console.log shows 650-850 ms on average, and 2nd - only 50-100ms, but if i swap around these two blocks the first one (promise.all) will be 500-700ms on average and second 100-250ms. What am i missing here? (probably a lot) I would appreciate if anyone could explane these results.
Here is line of my code. I can't access the data from const { quests } = this.props; which I'm pulling from redux state and I am getting undefined. I tried to put my code on componentDidUpdate my issue resolved from getting undefined. However, I can't make the time tick and my page not re-rendering.
componentDidUpdate() {
const { quests } = this.props || {}
let getTime;
getTime = quests.items?.items?.map(quests => {
let startTime = Date.parse(quests.createdAt)
let endTime = Date.parse(quests.expiresAt)
let timeLimit = endTime - startTime;
let timeDuration = moment.duration(timeLimit*1000, 'milliseconds');
let interval = 1000;
let today = moment().utc();
quests.timeDuration = timeDuration
setInterval(() => {
timeDuration = moment.duration(timeDuration - interval, 'milliseconds')
quests.timeDuration = timeDuration
}, interval)
})
return getTime;
}
Your timer function is mutating props (which is bad) because it is directly changing quests, but it is not causing a recognizable update in state or props, so it is not rerendering. Something like this might work, without more code to go off of from your project it's hard to provide an example that's much use, but maybe this will give you an idea.
state = {
quests: [],
elapsedSeconds: 0,
}
componentDidUpdate(prevProps) {
if (!prevProps.quests && this.props.quests) {
const { quests } = this.props;
let getTime = [];
quests.items?.items?.forEach((quest, index) => {
let startTime = Date.parse(quest.createdAt)
let endTime = Date.parse(quest.expiresAt)
let timeLimit = endTime - startTime;
let timeDuration = moment.duration(timeLimit*1000, 'milliseconds');
let today = moment().utc();
getTime.push(timeDuration);
})
this.setState({quests: getTime});
this.timer = setInterval(() => {
this.setState(({elapsedSeconds, quests}) => {
quests = quests.map(time => time - 1000)
return {elapsedSeconds: elapsedSeconds++, quests};
});
}, 1000);
}
}
componentWillUnmount() {
clearInterval(this.timer);
}
I'm trying to figure out the way I can set up a rolling window for a variable.
The variable will record a number increasing # amount of times from the minute before.
My basic interval timer
var kp123 = Number('1');
var myInt = setInterval(function () {
kp123 = 1;
}, 60000);
Whenever a specific command is sent, kp123 gets increased by 1:
kp123++;
This variable may increase 20 times every second, or only 2-3 times every second.
Right now how the system is set up, it records the variable # every minute, however, the data gets reset when the interval timer reaches one minute.
var kp123 = Number('1');
var kp123History = []; // The history of kp123 is stored in this variable each minute
var myInt = setInterval(function () {
kp123History.push(kp123);
kp123 = 1;
console.log(kp123History); // You can see the results in the console each minute like this
}, 60000);
or if you only want the previous value, and not the full history, try this
var kp123 = Number('1');
var prevKp123 = null; // The previous value of kp123 is stored in this variable each minute
var myInt = setInterval(function () {
prevKp123 = kp123;
kp123 = 1;
}, 60000);
It sounds like (per the comments) you want a rolling average (wiki). You don't really show enough of your code for me to give a specific answer to you, but in general, you can't deduce a rolling average from just averages, you'll need to know actual values and their timestamps. I.e. you can't summarize your data and throw away the timestmaps.
class RollingAverage {
constructor(windowMs = 1000) {
this.windowMs_ = windowMs;
this.values_ = [];
}
addValue(value = 1) {
let time = Date.now();
this.values_.push({value, time});
}
get average() {
let now = Date.now();
this.values_ = this.values_.filter(({time}) => now - time <= this.windowMs_ * 1000);
return this.values_
.map(({value}) => value)
.reduce((a, b) => a + b)
/ this.values_.length;
}
}
let test = async () => {
let sleep = ms => new Promise(r => setTimeout(r, ms));
let avg = new RollingAverage();
avg.addValue(1);
console.log(avg.average); // 1
await sleep(600);
console.log(avg.average); // 1
avg.addValue(3);
console.log(avg.average); // 2
await sleep(600);
console.log(avg.average); // 3
avg.addValue(5);
console.log(avg.average); // 4
}
test();
I would like to do the following:
console.time("processA");
for(let i; i < 10000; i++) {
// Just to simulate the process
}
console.timeEnd("processA");
but I want to capture the time end and use my own logger of information with it.
Is it possible to handle the console output of the timeEnd?
How can I measure the time interval of a process in nodejs?
Since you are targeting nodejs, you can use process.hrtime as stated in the docs
The process.hrtime() method returns the current high-resolution real time in a [seconds, nanoseconds] tuple Array, where nanoseconds is the remaining part of the real time that can't be represented in second precision.
So you can measure timings up to nanosecond, something that console.time can't, as you can see in your example console.time or Date difference measures 0s.
For example:
const NS_PER_SEC = 1e9;
const MS_PER_NS = 1e-6
const time = process.hrtime();
for (let i; i < 10000; i++) {
// Just to simulate the process
}
const diff = process.hrtime(time);
console.log(`Benchmark took ${diff[0] * NS_PER_SEC + diff[1]} nanoseconds`);
console.log(`Benchmark took ${ (diff[0] * NS_PER_SEC + diff[1]) * MS_PER_NS } milliseconds`);
Since I'm using timers in multiple places, I wrote a simple class based on Alex's answer:
const t = new Timer('For Loop')
// your code
t.runtimeMs() // => 1212.34
t.runtimeMsStr() // => 'For Loop took 1232.34 milliseconds'
Here's the code:
class Timer {
// Automatically starts the timer
constructor(name = 'Benchmark') {
this.NS_PER_SEC = 1e9;
this.MS_PER_NS = 1e-6
this.name = name;
this.startTime = process.hrtime();
}
// returns the time in ms since instantiation
// can be called multiple times
runtimeMs() {
const diff = process.hrtime(this.startTime);
return (diff[0] * this.NS_PER_SEC + diff[1]) * this.MS_PER_NS;
}
// retuns a string: the time in ms since instantiation
runtimeMsStr() {
return `${this.name} took ${this.runtimeMs()} milliseconds`;
}
}
Starting from Node v8.5, you can use browser-equivalent performance.now() which is easier to use than process.hrtime because it outputs the time in milliseconds directly, therefore you don't have to do a conversion as in process.hrtime
const { performance } = require("perf_hooks");
const start = performance.now();
doSomething();
const end = performance.now();
console.log(`time taken: ${end - start}ms`);
You can find more info from Node.js Docs on Performance API.
var startTime = new Date();
for(let i; i < 10000; i++) {
// Just to simulate the process
}
var endTime = new Date() - startTime;
You will get the total time that it takes to complete the operation
See here https://alligator.io/js/console-time-timeend/
var begin=console.time('t');
for(let i; i < 100000; i++) {
// Just to simulate the process
}
var end= console.timeEnd('t');
var timeSpent=(end-begin) / 1000 + "secs";
I need to send X requests to http://date.jsontest.com/ where X is an argument to my program. I've managed to get the latency for one request but now need to calculate the average time in milliseconds for multiple requests. I have an array responseTimes[] where I'd like to store the response times. How do I possibly add the multiple response times and get the average? Any help? Do I need to use the setTimeout function in the first place?
I'm using node v8.9.4 if fetchURL does not work on your end.
Here's my code
var fetchUrl = require("fetch").fetchUrl;
fetchUrl("http://date.jsontest.com", function(error, meta, body){
const start = new Date();
const responseTimes = [];
let count = 0;
setTimeout(function (argument) {
// execution time simulated with setTimeout function
var end = new Date() - start;
console.log("Execution time: %dms", end);
});
});
You need to put the start variable outside the request.
var fetchUrl = require("fetch").fetchUrl;
var start = new Date();
fetchUrl("http://date.jsontest.com", function(error, meta, body){
const requests = [];
let count = 0;
// execution time simulated with setTimeout function
var end = new Date() - start;
console.log("Execution time: %dms", end);
});
If you're storing responseTime[] then with that you can store count of requests. So at any point in time, you can get average by the following function -
function getAvg(timeArr, count){
let sum = 0;
for(let i=0; i<timeArr.length; i++){
sum += timeArr[i];
}
return sum/count;
}
Also, It's not necessary to store responseTime[]. You can just calculate avg on each request and store only the average and count If you just need average.
var fetchUrl = require("fetch").fetchUrl;
let responseTime = [];
let count = 0;
fetchUrl("http://date.jsontest.com", function(error, meta, body){
const start = new Date();
const requests = [];
let count = 0;
setTimeout(function (argument) {
// execution time simulated with setTimeout function
var end = new Date() - start;
console.log("Execution time: %dms", end);
responseTime.push(end);
count++;
let averageNow = getAvg(responseTime, count);
console.log(averageNow);// this is your average
});
});
You can use Artillery npm module to get the average response time of an API.