• JS 堆栈&内存快照& tracre跟踪


    堆栈跟踪 API

    V8 中抛出的所有内部错误在创建时都会捕获堆栈跟踪。可以通过非标准error.stack属性从 JavaScript 访问此堆栈跟踪。V8 还具有各种钩子,用于控制堆栈跟踪的收集和格式化方式,以及允许自定义错误也收集堆栈跟踪。本文档概述了 V8 的 JavaScript 堆栈跟踪 API。

    
    function getStack(error) {
      const orig = Error.prepareStackTrace;
      Error.prepareStackTrace = (_, stack) => stack;
      const stack = error.stack;
      Error.prepareStackTrace = orig;
      return stack;
    }
    
    function trace() {
      try {
        throw new Error('stack');
      }
      catch (error) {
        console.log(getStack(error)[0].getFunctionName());
      }
    }
    
    function b() {
      trace();
    }
    
    function a() {
      b();
    }
    
    a()
    
    
    // 参考文档:堆栈跟踪 API:(https://v8.dev/docs/stack-trace-api)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • https://github.com/tj/callsite

    tracre 记录

    const { Session } = require('inspector');
    const session = new Session();
    session.connect();
    function post(message, data) {
      return new Promise((resolve, reject) => {
        session.post(message, data, (err, result) => {
          if (err)
            reject(new Error(JSON.stringify(err)));
          else
            resolve(result);
        });
      });
    }
    
    async function test() {
    	session.on('NodeTracing.dataCollected', (data) => {
    	  console.log(data.params.value);
    	});
    	
    	session.on('NodeTracing.tracingComplete', () => {
    		console.log('done');
    	});
    	const { categories } = await post('NodeTracing.getCategories');
    	const traceConfig = { includedCategories: categories };
    	await post('NodeTracing.start', { traceConfig });
    	setTimeout(() => {
    		post('NodeTracing.stop');
    	}, 1000);
    }
    
    test();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    参考:https://blog.csdn.net/THEANARKH/article/details/123452468

    内存快照

    const { writeHeapSnapshot } = require('node:v8');
    const {
      Worker,
      isMainThread,
      parentPort,
    } = require('node:worker_threads');
    
    if (isMainThread) {
      const worker = new Worker(__filename);
    
      worker.once('message', (filename) => {
        console.log(`worker heapdump: ${filename}`);
        // Now get a heapdump for the main thread.
        console.log(`main thread heapdump: ${writeHeapSnapshot()}`);
      });
    
      // Tell the worker to create a heapdump.
      worker.postMessage('heapdump');
    } else {
      parentPort.once('message', (message) => {
        if (message === 'heapdump') {
          // Generate a heapdump for the worker
          // and return the filename to the parent.
          parentPort.postMessage(writeHeapSnapshot());
        }
      });
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    参考:https://nodejs.org/api/v8.html#v8writeheapsnapshotfilenameoptions

  • 相关阅读:
    《Java》【速学】对象-多态
    《最新出炉》系列初窥篇-Python+Playwright自动化测试-39-highlight() 方法之追踪定位
    一文拿捏分布式协调Redis客服端-Redisson
    FastAdmin表格添加统计信息
    【运维自动化-配置平台】如何通过模板创建集群和模块
    【Python基础篇020】网络编程初识
    8-15外部排序-最佳归并树
    C++PrimerPlus(第6版)中文版:Chapter16.5.1函数对象_函数符概念
    C++之C++11字符串字面量后缀总结(二百四十八)
    基于非支配排序遗传算法的多目标水光互补优化调度附Matlab代码
  • 原文地址:https://blog.csdn.net/weixin_56071908/article/details/133796023