Js 101 Question
const a = {} function test1 (a) { a = { name: 'wdd' } } function test2 () { test1(a) } function test3 () { console.log(a) } test2() test3()
const a = {} function test1 (a) { a = { name: 'wdd' } } function test2 () { test1(a) } function test3 () { console.log(a) } test2() test3()
机器信息:4C32G 测试工具:wrk Node: v14.17.0 express.js 'use strict' const express = require('express') const app = express() app.get('/', function (req, res) { res.json({ hello: 'world' }) }) app.listen(3000) fastify.js 'use strict' const fastify = require('fastify')() fastify.get('/', function (req, reply) { reply.send({ hello: 'world' }) }) fastify.listen(3000) ~ 测试结果 # express.js Running 10s test @ http://127.0.0.1:3000 12 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 55.36ms 11.53ms 173.22ms 93.16% Req/Sec 602.58 113.03 830.00 84.97% 72034 requests in 10.10s, 17.31MB read Requests/sec: 7134.75 Transfer/sec: 1.71MB # fastify.js Running 10s test @ http://127.0.0.1:3000 12 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 16.26ms 5.73ms 105.76ms 96.26% Req/Sec 2.08k 490.82 14.63k 94.92% 249114 requests in 10.09s, 44.43MB read Requests/sec: 24688.94 Transfer/sec: 4.40MB fastify是express的3.4倍, 所以对性能有所追求的话,最好用fastify。 ...
环境: ARM64 <--- Last few GCs ---> <--- JS stacktrace ---> # # Fatal process OOM in insufficient memory to create an Isolate # 在Dockerfile上设置max-old-space-size的node.js启动参数, 亲测有效。 CMD node --report-on-fatalerror --max-old-space-size=1536 dist/index.js Currently, by default v8 has a memory limit of 512mb on 32-bit and 1gb on 64-bit systems. You can raise the limit by setting –max-old-space-size to a maximum of ~1gb for 32-bit and ~1.7gb for 64-bit systems. But it is recommended to split your single process into several workers if you are hitting memory limits. ...
1. 选择安装包 访问 https://nodejs.org/en/download/ 选择Linux Binaries(x64) 2. 解压 下载后的文件是一个tar.xz的文件。 xz -d node-xxxx.tar.zx // 解压xz tar -xvf node-xxxx.tar // 拿出文件夹 解压后的目录如下,其中 ➜ node-v14.17.0-linux-x64 ll total 600K drwxr-xr-x 2 wangdd staff 4.0K May 13 09:34 bin -rw-r--r-- 1 wangdd staff 469K May 12 02:14 CHANGELOG.md drwxr-xr-x 3 wangdd staff 4.0K May 13 09:34 include drwxr-xr-x 3 wangdd staff 4.0K May 13 09:34 lib -rw-r--r-- 1 wangdd staff 79K May 12 02:14 LICENSE -rw-r--r-- 1 wangdd staff 30K May 12 02:14 README.md drwxr-xr-x 5 wangdd staff 4.0K May 12 02:14 share // bin目录下就是nodejs的可执行程序 ➜ node-v14.17.0-linux-x64 ll bin total 71M -rwxr-xr-x 1 wangdd staff 71M May 12 02:14 node lrwxrwxrwx 1 wangdd staff 38 May 12 02:14 npm -> ../lib/node_modules/npm/bin/npm-cli.js lrwxrwxrwx 1 wangdd staff 38 May 12 02:14 npx -> ../lib/node_modules/npm/bin/npx-cli.js ➜ node-v14.17.0-linux-x64 ./bin/node --version v14.17.0 通过将bin目录加入到$PATH环境变量中这种方式,就可以直接调用node。 ...
在v11.7.0中加入实验性功能,诊断报告。诊断报告的输出是一个json文件,包括以下信息。 进程信息 操作系统信息 堆栈信息 内存资源使用 libuv状态 环境变量 共享库 诊断报告的原始信息 如何产生诊断报告 必需使用 –experimental-report 来启用 process.report.writeReport() 来输出诊断报告 node --experimental-report --diagnostic-report-filename=YYYYMMDD.HHMMSS.PID.SEQUENCE#.txt --eval "process.report.writeReport('report.json')" Writing Node.js report to file: report.json Node.js report completed 用编辑器打开诊断报告,可以看到类似下面的内容。 如何从诊断报告中分析问题? 诊断报告很长,不太好理解。IBM开发了report-toolkit工具,可以用来分析。 要求:node > 11.8.0 npm install report-toolkit --global 或者 yarn global add report-toolkit 查看帮助信息 rtk --help 自动出发报告 node --experimental-report \ --diagnostic-report-on-fatalerror \ --diagnostic-report-uncaught-exception \ index.js $ node –help grep report --experimental-report enable report generation 启用report功能 --diagnostic-report-on-fatalerror generate diagnostic report on fatal (internal) errors 产生报告当发生致命错误 --diagnostic-report-on-signal generate diagnostic report upon receiving signals 产生报告当收到信号 --diagnostic-report-signal=... causes diagnostic report to be produced on provided signal. Unsupported in Windows. (default: SIGUSR2) --diagnostic-report-uncaught-exception generate diagnostic report on uncaught exceptions 产生报告当出现未捕获的异常 --diagnostic-report-directory=... define custom report pathname. (default: current working directory of Node.js process) --diagnostic-report-filename=... define custom report file name. (default: YYYYMMDD.HHMMSS.PID.SEQUENCE#.txt) 参考 https://nodejs.org/dist/latest-v12.x/docs/api/report.html https://ibm.github.io/report-toolkit/quick-start https://developer.ibm.com/technologies/node-js/articles/introducing-report-toolkit-for-nodejs-diagnostic-reports
标准文档 WebRTC https://w3c.github.io/webrtc-pc/ MediaStream https://www.w3.org/TR/mediacapture-streams/ 实现接口 MediaStream: 获取媒体流,例如从用户的摄像机或者麦克风 RTCPeerConnection: 音频或者视频呼叫,以及加密和带宽管理 RTCDataChannel: 端到端的数据交互 WebRTC架构 架构图颜色标识说明: 紫色部分是Web开发者API层 蓝色实线部分是面向浏览器厂商的API层 蓝色虚线部分浏览器厂商可以自定义实现 WebRTC有三个模块: Voice Engine(音频引擎) Voice Engine包含iSAC/iLBC Codec(音频编解码器,前者是针对宽带和超宽带,后者是针对窄带) NetEQ for voice(处理网络抖动和语音包丢失) Echo Canceler(回声消除器)/ Noise Reduction(噪声抑制) Video Engine(视频引擎) VP8 Codec(视频图像编解码器) Video jitter buffer(视频抖动缓冲器,处理视频抖动和视频信息包丢失) Image enhancements(图像质量增强) Transport SRTP(安全的实时传输协议,用以音视频流传输) Multiplexing(多路复用) P2P,STUN+TURN+ICE(用于NAT网络和防火墙穿越的) 除此之外,安全传输可能还会用到DTLS(数据报安全传输),用于加密传输和密钥协商 整个WebRTC通信是基于UDP的 WebRTC底层协议 WebRTC 核心的协议都是在右侧基于 UDP 基础上搭建起来的。 其中,ICE、STUN、TURN 用于内网穿透, 解决了获取与绑定外网映射地址,以及 keep alive 机制 DTLS 用于对传输内容进行加密,可以看做是 UDP 版的 TLS。由于 WebRTC 对安全比较重视,这一层是必须的。所有WebRTC组件都必须加密,并且其JavaScript API只能用于安全源(HTTPS或本地主机)。信令机制并不是由WebRTC标准定义的,所以您必须确保使用安全协议。 SRTP 与 SRTCP 是对媒体数据的封装与传输控制协议 SCTP 是流控制传输协议,提供类似 TCP 的特性,SCTP 可以基于 UDP 上构建,在 WebRTC 里是在 DTLS 协议之上 RTCPeerConnection 用来建立和维护端到端连接,并提供高效的音视频流传输 RTCDataChannel 用来支持端到端的任意二进制数据传输 WebRTC 协议栈解释 ...
GC释放时机 当HeapUsed接近最大堆内存时,出发GC释放。 下图是深夜,压力比较小的时候。 下图是上午工作时间 内存泄漏 OOM
process.memoryUsage() { rss: 4935680, heapTotal: 1826816, heapUsed: 650472, external: 49879, arrayBuffers: 9386 } heapTotal 和 heapUsed指向V8’s 内存使用 external 指向 C++ 对象的内存使用, C++对象绑定js对象,并且由V8管理 rss, 实际占用内存,包括C++, js对象和代码三块的总计。使用 ps aux命令输出时,rss的值对应了RSS列的数值 node js 所有buffer占用的内存 heapTotal and heapUsed refer to V8’s memory usage. external refers to the memory usage of C++ objects bound to JavaScript objects managed by V8. rss, Resident Set Size, is the amount of space occupied in the main memory device (that is a subset of the total allocated memory) for the process, including all C++ and JavaScript objects and code. arrayBuffers refers to memory allocated for ArrayBuffers and SharedArrayBuffers, including all Node.js Buffers. This is also included in the external value. When Node.js is used as an embedded library, this value may be 0 because allocations for ArrayBuffers may not be tracked in that case. process.resourceUsage() userCPUTime maps to ru_utime computed in microseconds. It is the same value as process.cpuUsage().user. systemCPUTime maps to ru_stime computed in microseconds. It is the same value as process.cpuUsage().system. maxRSS maps to ru_maxrss which is the maximum resident set size used in kilobytes. sharedMemorySize maps to ru_ixrss but is not supported by any platform. unsharedDataSize maps to ru_idrss but is not supported by any platform. unsharedStackSize maps to ru_isrss but is not supported by any platform. minorPageFault maps to ru_minflt which is the number of minor page faults for the process, see this article for more details. majorPageFault maps to ru_majflt which is the number of major page faults for the process, see this article for more details. This field is not supported on Windows. swappedOut maps to ru_nswap but is not supported by any platform. fsRead maps to ru_inblock which is the number of times the file system had to perform input. fsWrite maps to ru_oublock which is the number of times the file system had to perform output. ipcSent maps to ru_msgsnd but is not supported by any platform. ipcReceived maps to ru_msgrcv but is not supported by any platform. signalsCount maps to ru_nsignals but is not supported by any platform. voluntaryContextSwitches maps to ru_nvcsw which is the number of times a CPU context switch resulted due to a process voluntarily giving up the processor before its time slice was completed (usually to await availability of a resource). This field is not supported on Windows. involuntaryContextSwitches maps to ru_nivcsw which is the number of times a CPU context switch resulted due to a higher priority process becoming runnable or because the current process exceeded its time slice. This field is not supported on Windows. console.log(process.resourceUsage()); /* Will output: { userCPUTime: 82872, systemCPUTime: 4143, maxRSS: 33164, sharedMemorySize: 0, unsharedDataSize: 0, unsharedStackSize: 0, minorPageFault: 2469, majorPageFault: 0, swappedOut: 0, fsRead: 0, fsWrite: 8, ipcSent: 0, ipcReceived: 0, signalsCount: 0, voluntaryContextSwitches: 79, involuntaryContextSwitches: 1 } */
v8内存模型 Code Segment: 代码被实际执行 Stack 本地变量 指向引用的变量 流程控制,例如函数 Heap V8负责管理 HeapTotal 堆的总大小 HeapUsed 实际使用的大小 Shallow size of an object: 对象自身占用的内存 Retained size of an object: 对象及其依赖对象删除后回释放的内存
网页上的报错,一般都会和HTTP请求出错有关。 在Chrome浏览器中,按F12或者command+option+i可以打开Dev tools,在网络面板中可以找到报错的的HTTP请求。 通过提交Copy as cURL 和 Copy response的内容,就会非常准确的把问题报告给开发。开发也会非常快速的定位问题。