{"id":13459417,"url":"https://github.com/X-Profiler/xprofiler","last_synced_at":"2025-03-24T16:31:53.991Z","repository":{"id":37039023,"uuid":"217703664","full_name":"X-Profiler/xprofiler","owner":"X-Profiler","description":"🌀An addon for node.js, which supporting output performance log and real-time profiling through sampling.","archived":false,"fork":false,"pushed_at":"2025-03-10T13:58:26.000Z","size":886,"stargazers_count":435,"open_issues_count":15,"forks_count":57,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-03-18T10:48:33.743Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/X-Profiler.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-26T12:12:32.000Z","updated_at":"2025-03-10T13:58:29.000Z","dependencies_parsed_at":"2023-02-09T19:31:26.897Z","dependency_job_id":"dc0a562c-88cf-48ee-b1e7-780e00ffa031","html_url":"https://github.com/X-Profiler/xprofiler","commit_stats":{"total_commits":189,"total_committers":7,"mean_commits":27.0,"dds":"0.20105820105820105","last_synced_commit":"80ea311a5d4a2e2dcccfea7589c8f170f4e64ddd"},"previous_names":["hyj1991/nprofiler"],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/X-Profiler%2Fxprofiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/X-Profiler%2Fxprofiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/X-Profiler%2Fxprofiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/X-Profiler%2Fxprofiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/X-Profiler","download_url":"https://codeload.github.com/X-Profiler/xprofiler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245308727,"owners_count":20594298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T09:01:21.231Z","updated_at":"2025-03-24T16:31:48.972Z","avatar_url":"https://github.com/X-Profiler.png","language":"C++","readme":"# X-Profiler\n\n[![Npm](https://img.shields.io/npm/v/xprofiler)](https://www.npmjs.com/package/xprofiler)\n[![Codecov branch](https://img.shields.io/codecov/c/github/X-Profiler/xprofiler/master)](https://codecov.io/gh/X-Profiler/xprofiler/branch/master)\n[![Continuous integration](https://github.com/X-Profiler/xprofiler/actions/workflows/nodejs.yml/badge.svg?branch=master)](https://github.com/X-Profiler/xprofiler/actions/workflows/nodejs.yml?query=branch:master)\n[![Npm](https://img.shields.io/npm/dm/xprofiler)](https://www.npmjs.com/package/xprofiler)\n[![License](https://img.shields.io/badge/license-BSD--2--Clause-green)](LICENSE)\n\nEasy-Monitor v3.0 Node.js Runtime 插件，输出性能日志，并且可以进行实时的运行时状态采样。\n\n点击访问 [控制台 Demo](http://www.devtoolx.com/easy-monitor) 进行体验，完整的性能监控部署文档：https://www.yuque.com/hyj1991/easy-monitor\n\n## 兼容性\n\nxprofiler 插件支持三大主流操作系统：\n\n- Windows\n- Linux (x64 / arm64)\n- macosX (x64 / arm64)\n\n支持的 node.js runtime 版本：\n\n- v12.x\n- v13.x\n- v14.x\n- v15.x\n- v16.x\n- v17.x\n- v18.x\n- v19.x\n- v20.x\n- v21.x\n- v22.x\n\n更低的版本因为在此项目创建时已经不在官方 LTS 计划中，故正常情况下不再支持。\n\n需要在 `node-v12.x` 以下的版本中使用，可以使用 `v1.x-staging` 分支的的最新版本：[1.4.0](https://github.com/X-Profiler/xprofiler/tree/v1.x-staging)\n\n\n## 快速开始\n\n### 安装\n\n执行如下命令安装插件\n\n```bash\nnpm i xprofiler --save --xprofiler_binary_host_mirror=https://npmmirror.com/mirrors/xprofiler\n```\n\n如果 npmmirror 镜像暂时没同步的话，也可以执行阿里云镜像：\n\n```bash\nnpm i xprofiler --save --xprofiler_binary_host_mirror=http://devtoolx.com/xprofiler\n```\n\n\u003e 这里使用了 `@mapbox/node-pre-gyp` 帮助开发者无需进行本地编译即可使用此插件，默认插件位于 [Github Release](https://github.com/X-Profiler/xprofiler/releases) 页面，国内访问可能较慢，你也可以自行镜像到其它位置加速安装。\n\n### 嵌入项目\n\n在您的项目入口顶部引入即可，性能分析日志默认输出在 `os.tmpdir()` 下\n\n```js\n// 推荐以模块函数形式使用\nconst xprofiler = require('xprofiler');\nxprofiler.start();\n\n// 这里也可以直接使用\nrequire('xprofiler')();\n```\n\n此时插件会以 1min/次 的频率输出嵌入的宿主 Node.js 进程的性能日志到文件中，默认为 `os.tmpdir()` 目录下 `xprofiler-${YYYYMMDD}.log` 文件。\n\n### 可配置的参数\n\n本插件提供方以下的可配置参数:\n\n* **log_dir**: 内核输出日志和性能文件的目录，默认为 `os.tmpdir()`\n* **log_interval**: 内核对性能日志采样的时间间隔，默认为 `60s`\n* **log_level**: 输出日志信息级别，0 info，1 error，2 debug，默认只输出 info 和 error 日志，默认为 `1`\n* **log_type**: 日志输出位置，0 文件，1 控制台，默认为 `0`\n* **log_format_alinode**: 是否以 Node.js 性能平台（原 AliNode）的格式输出性能分析日志，默认为 `false`\n* **patch_http**: 是否对原生 http 模块进行 patch，输出 http 请求相关信息，默认 `true`\n* **patch_http_timeout**: 默认 http 请求超时时间，单位秒，作为 http 请求统计，默认 `30`\n* **check_throw**: `xprofiler` 启动时检测错误时是否需要 throw，默认 `true`\n* **auto_incr_heap_limit_size**: `enable_auto_incr_heap_limit` 开启后，每次自动增加的堆上限大小，默认为 `256` (MB)\n* **enable_log_uv_handles**: 是否要采集 libuv 句柄的详细分类信息，比如 tcp 句柄数量，timers 数量，文件句柄数量等，默认为 `true`\n* **enable_fatal_error_hook**: 是否需要在 V8 出现 FatalError 时配置钩子，默认 `true`\n* **enable_fatal_error_report**: 是否需要在 V8 出现 FataLError 时导出 Report 文件，默认 `true`\n* **enable_fatal_error_coredump**: 是否需要在 V8 出现 FataLError 时 Coredump，默认 `false`\n* **enable_http_profiling**: 是否需要 CPU 采样时进行 HTTP Profiling。默认 `false`\n* **enable_auto_incr_heap_limit**: 是否需要在 Node.js 进程达到堆上限时自动增加堆上限防止 OOM，默认 `false`\n\n\n您可以通过环境变量或者在 JavaScript 代码中引入插件时传入配置的方式来使用这些配置，具体如下所示：\n\n#### 1. 环境变量配置\n\n* **XPROFILER_LOG_DIR**: 其值为 String，覆盖 `log_dir`\n* **XPROFILER_LOG_INTERVAL**: 其值为 Number，覆盖 `log_interval`\n* **XPROFILER_LOG_LEVEL**: 其值为 String，覆盖 `log_level`\n* **XPROFILER_LOG_TYPE**: 其值为 String，覆盖 `log_type`\n* **XPROFILER_LOG_FORMAT_ALINODE**: 其值为 YES/NO，覆盖 `log_format_alinode`\n* **XPROFILER_PATCH_HTTP**: 其值为 YES/NO，覆盖 `patch_http`\n* **XPROFILER_PATCH_HTTP_TIMEOUT**: 其值为 String，覆盖 `patch_http_timeout`\n* **XPROFILER_CHECK_THROW**: 其值为 YES/NO 覆盖 `check_throw`\n* **XPROFILER_AUTO_INCR_HEAP_LIMIT_SIZE**: 其值为 String 覆盖 `auto_incr_heap_limit_size`\n* **XPROFILER_ENABLE_LOG_UV_HANDLES**: 其值为 YES/NO，覆盖 `enable_log_uv_handles`\n* **XPROFILER_ENABLE_FATAL_ERROR_HOOK**: 其值为 YES/NO，覆盖 `enable_fatal_error_hook`\n* **XPROFILER_ENABLE_FATAL_ERROR_REPORT**: 其值为 YES/NO，覆盖 `enable_fatal_error_report`\n* **XPROFILER_ENABLE_FATAL_ERROR_COREDUMP**: 其值为 YES/NO，覆盖 `enable_fatal_error_coredump`\n* **XPROFILER_ENABLE_HTTP_PROFILING**: 其值为 YES/NO，覆盖 `enable_http_profiling`\n* **XPROFILER_ENABLE_AUTO_INCR_HEAP_LIMIT**: 其值为 YES/NO 覆盖 `enable_auto_incr_heap_limit`\n\n\n#### 2. 引入插件时传入配置\n\n```js\nconst xprofiler = require('xprofiler');\nxprofiler.start({\n  log_dir: '/path/to/your/logdir', // 性能分析日志输出目录\n  log_interval: 120, // 采样间隔 120s\n  enable_log_uv_handles: false, // 不输出 uv 句柄分类详情\n  log_format_alinode: true, // 以 alinode 的格式输出日志\n  log_level: 0 // 只输出 info 日志\n  // 其它提供的可选配置\n});\n```\n\n#### 3. 配置覆盖规则\n\n配置覆盖顺序为 **用户传入参数的优先级最高，环境变量次之**，简单的使用代码来表示参数覆盖规则为：\n\n```js\nconst defaultConfig = {\n  log_dir: os.tmpdir(),\n  log_interval: 60, // seconds\n  log_level: 1,\n  log_type: 0,\n  log_format_alinode: false,\n  patch_http: true,\n  patch_http_timeout: 30, // seconds，\n  check_throw: true,\n  auto_incr_heap_limit_size: 128 // MB,\n  enable_log_uv_handles: true,\n  enable_fatal_error_hook: true,\n  enable_fatal_error_report: true,\n  enable_fatal_error_coredump: false,\n  enable_http_profiling: false,\n  enable_auto_incr_heap_limit: false,\n};\n\nconst xprofilerConfig = Object.assign({}, defaultConfig, envConfig, userConfig);\n```\n\n详细配置覆盖规则的测试可以参见 [config.test.js](https://github.com/X-Profiler/xprofiler/blob/master/test/config.test.js)\n\n### 性能分析日志\n\n本插件按照固定的格式来输出性能分析日志，但是也目前为了兼容已有的 alinode 运行时日志，提供了对 alinode 的支持，通过 `log_format_alinode: true` 配置或者 `XPROFILER_LOG_FORMAT_ALINODE=YES` 环境变量来启用 alinode 日志。两种日志文件名区别如下：\n\n* 默认 xprofiler 日志文件名称\n  * info 日志：xprofiler-${YYYYMMDD}.log\n  * error 日志：xprofiler-error-${YYYYMMDD}.log\n  * debug 日志：xprofiler-debug-${YYYYMMDD}.log\n* 以 alinode 方式输出日志文件名称\n  * info 日志：node-${YYYYMMDD}.log\n  * error 日志：node-error-${YYYYMMDD}.log\n  * debug 日志：node-debug-${YYYYMMDD}.log\n\n默认的 xprofiler 和 alinode 两种日志的主要区别在日志前缀上(当然输出的部分内容也不尽相同)，其中 xprofiler 日志前缀如下：\n\n```bash\n\"[YYYY-MM-DD HH:mm:ss] [日志级别] [日志类型] [pid] [xprofiler 版本] 日志详情\"\n```\n\n如果设置为 alinode 日志格式输出，日志前缀则为：\n\n```bash\n\"[YYYY-MM-DD HH:mm:ss.usec] [日志级别] [日志类型] [pid] 日志详情\"\n```\n\n插件 xprofiler 多采集一个输出日志信息时的版本，目的是方便服务端统计当前各个 xprofiler 插件版本以及当前对应的应用信息，以帮助找到负责人来推动核心插件版本的升级。\n\n本插件和 alinode 的另一个区别是 xprofiler 默认的日志目录为 `os.tmpdir()` 对应的目录，而非 alinode 原生默认的 `/tmp` ，原因是为了兼容 windows；并且上述的 node.js api 本身已经跨平台且安全，也避免了一些容器环境下人为将 `/tmp` 目录映射到挂载磁盘带来的问题。\n\n当然您也可以通过更改 `log_dir` 配置或者 `XPROFILER_LOG_DIR` 环境变量来切换日志输出的目录，只需要确保你的 node.js 应用和采集器有权限读写该目录即可。\n\n### 日志采集\n\n如果您配置了 `log_format_alinode: true`，即按照 alinode 的格式输出日志，那么直接使用官方提供的 [agenthub](http://npmjs.com/package/@alicloud/agenthub) 即可采集上报插件生成的日志，值得注意的是，此时需将 agenthub 的配置项 `logdir` 配置为插件 xprofiler 的 `log_dir` 对应的服务器路径。\n\n### 运行时状态采样\n\n如果全局安装 xprofiler 则可以使用 `xprofctl` 命令，如下所示:\n\n```bash\nnpm i xprofiler -g\n```\n\n此命令可以对安装并启用了 xprofiler 插件的 node.js 进程进行一些操作，安装后执行 `xprofctl -h` 可以查看其用法:\n\n```bash\nxprofctl \u003caction\u003e -p \u003cpid\u003e [-w \u003cthread_id\u003e] [-t profiling_time]\n\n命令：\n  xprofctl start_cpu_profiling   启动 cpu 采样\n  xprofctl stop_cpu_profiling    生成 cpuprofile\n  xprofctl start_heap_profiling  启动 heap 采样\n  xprofctl stop_heap_profiling   生成 heapprofile\n  xprofctl start_gc_profiling    启动 gc 采样\n  xprofctl stop_gc_profiling     生成 gcprofile\n  xprofctl heapdump              生成 heapsnapshot\n  xprofctl diag_report           生成诊断报告\n  xprofctl check_version         获取 xprofiler 版本号\n  xprofctl get_config            获取 xprofiler 配置\n  xprofctl set_config            设置 xprofiler 配置\n\n选项：\n  -p, --pid                  进程 pid               [必需]\n  -w, --worker_thread_id     线程 id                [可选]\n  -h, --help                 显示帮助信息            [布尔]\n  -v, --version              显示版本号              [布尔]\n\n示例：\n  xprofctl start_cpu_profiling -p 29156  触发进程 29156 开始进行 cpu 采样\n  xprofctl check_version -p 29156        获取进程 29156 使用的插件版本\n\ncopyright 2019\n```\n\n\n## 插件架构和实现原理\n\n参见 [文档 - 整体架构](https://www.yuque.com/hyj1991/easy-monitor/architecture)\n\n\n## 稳定性\n\nxprofiler 每个 commit 都会通过 github actions 进行在 windows/linux/macos 下的编译以及单元测试，如果您在使用中依旧出现意外的 crash，可以反馈到 [Issue 列表](https://github.com/X-Profiler/xprofiler/issues) 进行跟踪处理。\n\n\n## 测试与代码覆盖率\n\n在项目根目录下执行如下命令即可获取单元测试与代码覆盖率状况：\n\n```bash\nnpm run cov\n```\n\nmaster 分支的代码覆盖率情况可以参见 [codecov badge](https://codecov.io/gh/X-Profiler/xprofiler/branch/master)。\n\n\n## 如何参与贡献\n\n* fork 本仓库至你自己的 github 仓库列表中\n* clone 你 fork 出来的仓库至本地开发\n* 进行本地开发，添加功能或者修改 bug，并且附上必要的测试\n* 请在 commit 中描述下添加的功能或者修改 bug 的详细信息，并提交至你的远程仓库\n* 在 [PR](https://github.com/X-Profiler/xprofiler/pulls) 页面选择 New Pull Request，继续选择 compare across forks，在列表中选中你的 Fork，然后创建新的 PR\n* 查看 pr 下的 travis/appveypr CI 状态，如果执行失败请到对应页面查看失败原因并在你自己的仓库下进行修复，修复 commit 会自动同步到此 pr 下，无需关闭重启发起\n\n我们将在 review 后选择合并至本仓库内，贡献者也会加入到本项目的协作者列表中。\n\n\n## 贡献者\n\n[![contributors](https://contrib.rocks/image?repo=x-profiler/xprofiler\u0026max=240\u0026columns=26)](https://github.com/x-profiler/xprofiler/graphs/contributors)\n\n## License\n\n[BSD-2-Clause](LICENSE)\n","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FX-Profiler%2Fxprofiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FX-Profiler%2Fxprofiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FX-Profiler%2Fxprofiler/lists"}