njs icon indicating copy to clipboard operation
njs copied to clipboard

Profile JS code

Open ram-vet opened this issue 2 years ago • 2 comments

Hi, is there a way to profile JS code in NJS ? Or any other possible integration for profiling just the JS part.

ram-vet avatar May 04 '23 15:05 ram-vet

Hi @ram-vet,

There is console.time() and console.timeEnd() pair for njs CLI. Otherwise there no profiling tools specific to njs.

Please elaborate what you are trying to profile and what part of nginx/njs you are trying to measure.

xeioex avatar May 05 '23 01:05 xeioex

Hi @ram-vet I wrote a little profiling tool for another NGINX user / customer.

https://gist.github.com/tippexs/59cc1a2d2e2b909947ba22a63608a90b

It is not in an state where it would make sense to call it a tool but what you can basically do with it is measure execution times of NJS functions and write them to a file.

$ ~> node index.js
event1: Min: 1, Middle 80.52228112910159, Max: 305, Std. Deviation: 44.29781599231003, Start: 167116922931, End: Start: 167116926031
event2 server 1: Min: 0, Middle 1.2707481005260082, Max: 101, Std. Deviation: 4.78749625209064, Start: 167116922872, End: Start: 167116926027
event3 server 2: Min: 1, Middle 78.99934512115259, Max: 294, Std. Deviation: 41.49847599276098, Start: 167116922931, End: Start: 167116926031
File successfully created!
File successfully created!

It will generate a log file and a little NodeJs application can actually create diagrams like this one.

image

All of this is very hacky :D and I feel very bad sharing the code as it will be (with a very high chance) the most ugliest piece of code you have ever seen in your live. Not kidding.

But let me know if this sounds useful to you. Happy to upload the stuff to a github repository.

tippexs avatar May 06 '23 03:05 tippexs