项目作者: indutny

项目描述 :
Trace outgoing http requests for an http server and track the time spent doing CPU intensive workload during each request.
高级语言: JavaScript
项目地址: git://github.com/indutny/breakdown.git
创建时间: 2020-07-15T23:47:43Z
项目社区:https://github.com/indutny/breakdown

开源协议:

下载


breakdown

Build Status
npm version

Trace outgoing http requests for an http server and track the time spent
doing CPU intensive workload during each such request and more.

Why?

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.

Usage

  1. const Breakdown = require('@indutny/breakdown');
  2. const b = new Breakdown();
  3. b.start('/path/to/log');
  4. const middleware = b.middleware();
  5. require('http').createServer((req, res) => {
  6. middleware(req, res);
  7. // ....
  8. }).listen(8000);

Analysis

Resulting log can be formatted into a (arguably) pretty markdown file
using (inarguably) ugly procedure (to be improved in the future):

  1. git clone git://github.com/indutny/breakdown
  2. cd breakdown
  3. npm i
  4. node tools/analyze.js /path/to/log > pretty.md

Output Format

Events are logged into a newline separated JSON objects:

  1. {"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"}}}
  2. {"type":"start","id":2,"ts":1594931478.992,"payload":{"parentId":1,"type":"DNS_LOOKUP","meta":{"family":"any","hostname":"example.com"}}}
  3. {"type":"start","id":3,"ts":1594931478.994,"payload":{"parentId":1,"type":"HTTP_CLIENT_REQUEST","meta":{"method":"GET","path":"/","headers":{"host":"example.com"}}}}
  4. {"type":"log","id":2,"ts":1594931479.003,"payload":{"error":false,"address":"93.184.216.34"}}
  5. {"type":"end","id":2,"ts":1594931479.003,"payload":{"spin":0.0005950850000000001,"selfSpin":0.0005950850000000001}}
  6. {"type":"log","id":3,"ts":1594931479.011,"payload":{"remoteAddress":"93.184.216.34","remotePort":80}}
  7. {"type":"end","id":3,"ts":1594931479.032,"payload":{"spin":0.006975610000000001,"selfSpin":0.006975610000000001}}
  8. {"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 id
  • ts - timestamp of the event in seconds (unix time)
  • payload - a payload object dependent on the type field.

start payload:

  • type - type of the event
  • parentId - event id of the parent or null
  • meta - various fields pertaining to particular type of event.

log payload:

  • anything specific to particular event type.

end payload:

  • spin - total CPU time in seconds spent during this event and its children
  • selfSpin - total CPU time in seconds spent during this event.

Supported Asynchronous Events

So far the only supported events are:

  • HTTP Server requests
  • HTTP Client requests
  • DNS lookups

How does this work?

This work through use of async_hooks APIs and some unfortunate use of
Node.js (semi-) internal code.

Which Node.js versions are supported?

The module was tested on Node.js versions starting from v10 and up to v14.

LICENSE

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.