• 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

  • 相关阅读:
    MacOS开发环境搭建
    Day43:VUEX
    Web Components详解-Shadow DOM插槽
    编写高质量代码改善程序的157个建议:使用Dynamic来简化反射的实现
    面试官:ElasticSearch是什么,它有什么特性与使用场景?
    流媒体分析之rtmp协议srs服务器数据收发
    Modality to Modality Translation
    一类综合的模糊化自适应滑模控制
    数据大屏定时请求后端数据
    搭建LInux服务面板1Panel.
  • 原文地址:https://blog.csdn.net/weixin_56071908/article/details/133796023