Trace outgoing http requests for an http server and track the time spent doing CPU intensive workload during each request.
Trace outgoing http requests for an http server and track the time spent
doing CPU intensive workload during each such request and more.
When optimizing app’s performance, requests to a remote server can turn out to
be the bottleneck. breakdown
helps identify such scenarios and provides
insights into the latency and CPU usage for such requests as well as for the
server endpoints themselves.
const Breakdown = require('@indutny/breakdown');
const b = new Breakdown();
b.start('/path/to/log');
const middleware = b.middleware();
require('http').createServer((req, res) => {
middleware(req, res);
// ....
}).listen(8000);
Resulting log can be formatted into a (arguably) pretty markdown file
using (inarguably) ugly procedure (to be improved in the future):
git clone git://github.com/indutny/breakdown
cd breakdown
npm i
node tools/analyze.js /path/to/log > pretty.md
Events are logged into a newline separated JSON objects:
{"type":"start","id":1,"ts":1594931478.984,"payload":{"parentId":null,"type":"HTTP_SERVER_REQUEST","meta":{"method":"GET","headers":{"host":"127.0.0.1:8000","user-agent":"curl/7.54.0","accept":"*/*"},"url":"/a"}}}
{"type":"start","id":2,"ts":1594931478.992,"payload":{"parentId":1,"type":"DNS_LOOKUP","meta":{"family":"any","hostname":"example.com"}}}
{"type":"start","id":3,"ts":1594931478.994,"payload":{"parentId":1,"type":"HTTP_CLIENT_REQUEST","meta":{"method":"GET","path":"/","headers":{"host":"example.com"}}}}
{"type":"log","id":2,"ts":1594931479.003,"payload":{"error":false,"address":"93.184.216.34"}}
{"type":"end","id":2,"ts":1594931479.003,"payload":{"spin":0.0005950850000000001,"selfSpin":0.0005950850000000001}}
{"type":"log","id":3,"ts":1594931479.011,"payload":{"remoteAddress":"93.184.216.34","remotePort":80}}
{"type":"end","id":3,"ts":1594931479.032,"payload":{"spin":0.006975610000000001,"selfSpin":0.006975610000000001}}
{"type":"end","id":1,"ts":1594931479.032,"payload":{"spin":0.018026091,"selfSpin":0.010455395999999999}}
Field description:
type
- either start
, log
, or end
id
- unique event idts
- timestamp of the event in seconds (unix time)payload
- a payload object dependent on the type
field.start
payload:
type
- type of the eventparentId
- event id of the parent or null
meta
- various fields pertaining to particular type of event.log
payload:
end
payload:
spin
- total CPU time in seconds spent during this event and its childrenselfSpin
- total CPU time in seconds spent during this event.So far the only supported events are:
This work through use of async_hooks
APIs and some unfortunate use of
Node.js (semi-) internal code.
The module was tested on Node.js versions starting from v10 and up to v14.
This software is licensed under the MIT License.
Copyright Fedor Indutny, 2020.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
“Software”), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.