JavaScript系列(58)--性能监控系统详解
JavaScript性能监控系统详解 📊
今天,让我们深入探讨JavaScript的性能监控系统。性能监控对于保证应用的稳定性和用户体验至关重要。
性能监控基础概念 🌟
💡 小知识:JavaScript性能监控是指通过收集和分析各种性能指标,来评估和优化应用性能的过程。这包括页面加载时间、资源使用情况、运行时性能等多个方面。
性能指标收集 📈
// 1. 性能指标收集器
class PerformanceCollector {constructor() {this.metrics = new Map();this.observers = new Set();}// 收集页面加载性能collectPageMetrics() {const timing = performance.timing;const metrics = {dnsTime: timing.domainLookupEnd - timing.domainLookupStart,tcpTime: timing.connectEnd - timing.connectStart,ttfb: timing.responseStart - timing.requestStart,domReady: timing.domContentLoadedEventEnd - timing.navigationStart,loadComplete: timing.loadEventEnd - timing.navigationStart};this.metrics.set('page', metrics);this.notifyObservers('page', metrics);}// 收集资源加载性能collectResourceMetrics() {const resources = performance.getEntriesByType('resource');const metrics = resources.map(resource => ({name: resource.name,type: resource.initiatorType,duration: resource.duration,size: resource.transferSize,startTime: resource.startTime}));this.metrics.set('resources', metrics);this.notifyObservers('resources', metrics);}// 收集运行时性能collectRuntimeMetrics() {const metrics = {memory: performance.memory ? {usedJSHeapSize: performance.memory.usedJSHeapSize,totalJSHeapSize: performance.memory.totalJSHeapSize,jsHeapSizeLimit: performance.memory.jsHeapSizeLimit} : null,fps: this.calculateFPS()};this.metrics.set('runtime', metrics);this.notifyObservers('runtime', metrics);}calculateFPS() {let lastTime = performance.now();let frames = 0;let fps = 0;const updateFPS = () => {const now = performance.now();frames++;if (now > lastTime + 1000) {fps = Math.round((frames * 1000) / (now - lastTime));frames = 0;lastTime = now;}requestAnimationFrame(updateFPS);};requestAnimationFrame(updateFPS);return () => fps;}addObserver(observer) {this.observers.add(observer);}removeObserver(observer) {this.observers.delete(observer);}notifyObservers(type, data) {this.observers.forEach(observer => {observer.update(type, data);});}
}// 2. 性能标记管理器
class PerformanceMarker {constructor() {this.marks = new Map();}mark(name) {performance.mark(name);this.marks.set(name, performance.now());}measure(name, startMark, endMark) {performance.measure(name, startMark, endMark);const measure = performance.getEntriesByName(name, 'measure')[0];return {name,duration: measure.duration,startTime: measure.startTime};}clearMarks() {performance.clearMarks();this.marks.clear();}clearMeasures() {performance.clearMeasures();}
}// 3. 用户交互监控
class InteractionMonitor {constructor() {this.interactions = [];this.setupListeners();}setupListeners() {const observer = new PerformanceObserver(list => {const entries = list.getEntries();this.processInteractions(entries);});observer.observe({ entryTypes: ['first-input', 'layout-shift', 'largest-contentful-paint']});}processInteractions(entries) {entries.forEach(entry => {this.interactions.push({type: entry.entryType,startTime: entry.startTime,duration: entry.duration,value: entry.value,element: entry.target});});}getInteractionMetrics() {return {fid: this.calculateFID(),cls: this.calculateCLS(),lcp: this.calculateLCP()};}calculateFID() {const fidEntry = this.interactions.find(i => i.type === 'first-input');return fidEntry ? fidEntry.duration : null;}calculateCLS() {return this.interactions.filter(i => i.type === 'layout-shift').reduce((sum, entry) => sum + entry.value, 0);}calculateLCP() {const lcpEntries = this.interactions.filter(i => i.type === 'largest-contentful-paint');return lcpEntries.length > 0 ? lcpEntries[lcpEntries.length - 1].startTime : null;}
}
性能分析工具 🔍
// 1. 性能分析器
class PerformanceAnalyzer {constructor() {this.thresholds = new Map();this.issues = [];}setThreshold(metric, value) {this.thresholds.set(metric, value);}analyzeMetrics(metrics) {this.issues = [];// 分析页面加载性能if (metrics.page) {this.analyzePageMetrics(metrics.page);}// 分析资源加载性能if (metrics.resources) {this.analyzeResourceMetrics(metrics.resources);}// 分析运行时性能if (metrics.runtime) {this.analyzeRuntimeMetrics(metrics.runtime);}return this.issues;}analyzePageMetrics(pageMetrics) {if (pageMetrics.ttfb > this.thresholds.get('ttfb')) {this.issues.push({type: 'warning',metric: 'ttfb',message: 'Time to First Byte is too high',value: pageMetrics.ttfb,threshold: this.thresholds.get('ttfb')});}if (pageMetrics.domReady > this.thresholds.get('domReady')) {this.issues.push({type: 'error',metric: 'domReady',message: 'DOM Ready time is too high',value: pageMetrics.domReady,threshold: this.thresholds.get('domReady')});}}analyzeResourceMetrics(resources) {const largeResources = resources.filter(r => r.size > this.thresholds.get('resourceSize'));if (largeResources.length > 0) {this.issues.push({type: 'warning',metric: 'resourceSize',message: 'Large resources detected',resources: largeResources});}}analyzeRuntimeMetrics(runtime) {if (runtime.memory && runtime.memory.usedJSHeapSize > this.thresholds.get('heapSize')) {this.issues.push({type: 'warning',metric: 'memory',message: 'High memory usage detected',value: runtime.memory.usedJSHeapSize});}if (runtime.fps() < this.thresholds.get('fps')) {this.issues.push({type: 'error',metric: 'fps',message: 'Low FPS detected',value: runtime.fps()});}}generateReport() {return {timestamp: new Date(),issueCount: this.issues.length,criticalIssues: this.issues.filter(i => i.type === 'error'),warnings: this.issues.filter(i => i.type === 'warning'),recommendations: this.generateRecommendations()};}generateRecommendations() {return this.issues.map(issue => {switch (issue.metric) {case 'ttfb':return 'Consider using a CDN or optimizing server response time';case 'domReady':return 'Optimize JavaScript execution and reduce blocking resources';case 'resourceSize':return 'Compress and optimize large resources';case 'memory':return 'Check for memory leaks and optimize memory usage';case 'fps':return 'Optimize animations and reduce DOM operations';default:return 'Review and optimize the affected area';}});}
}// 2. 性能监控仪表板
class PerformanceDashboard {constructor() {this.metrics = [];this.charts = new Map();}addMetric(metric) {this.metrics.push({...metric,timestamp: new Date()});this.updateCharts();}createChart(type, data) {// 实现图表创建逻辑return {type,data,render: () => {// 渲染图表}};}updateCharts() {// 更新页面加载时间趋势this.charts.set('loadTime', this.createChart('line',this.metrics.map(m => ({x: m.timestamp,y: m.page.loadComplete}))));// 更新资源使用趋势this.charts.set('resources', this.createChart('bar',this.metrics.map(m => ({x: m.timestamp,y: m.resources.length}))));// 更新内存使用趋势this.charts.set('memory', this.createChart('area',this.metrics.map(m => ({x: m.timestamp,y: m.runtime.memory?.usedJSHeapSize}))));}generateReport(timeRange) {const filteredMetrics = this.metrics.filter(m => m.timestamp >= timeRange.start && m.timestamp <= timeRange.end);return {summary: this.calculateSummary(filteredMetrics),trends: this.calculateTrends(filteredMetrics),issues: this.findIssues(filteredMetrics)};}
}// 3. 性能优化建议生成器
class OptimizationAdvisor {constructor() {this.rules = new Map();}addRule(name, condition, recommendation) {this.rules.set(name, { condition, recommendation });}analyze(metrics) {const recommendations = [];for (const [name, rule] of this.rules) {if (rule.condition(metrics)) {recommendations.push({name,recommendation: rule.recommendation,priority: this.calculatePriority(metrics, name)});}}return recommendations.sort((a, b) => b.priority - a.priority);}calculatePriority(metrics, ruleName) {// 根据性能指标的严重程度计算优化建议的优先级const weights = {loadTime: 0.3,memory: 0.2,fps: 0.3,resources: 0.2};let score = 0;if (metrics.page?.loadComplete > 3000) {score += weights.loadTime;}if (metrics.runtime?.memory?.usedJSHeapSize > metrics.runtime?.memory?.jsHeapSizeLimit * 0.8) {score += weights.memory;}if (metrics.runtime?.fps() < 30) {score += weights.fps;}if (metrics.resources?.length > 50) {score += weights.resources;}return score;}
}
报警和通知系统 🚨
// 1. 报警管理器
class AlertManager {constructor() {this.alerts = new Map();this.handlers = new Map();}addAlert(name, condition, options = {}) {this.alerts.set(name, {condition,threshold: options.threshold,cooldown: options.cooldown || 5 * 60 * 1000, // 5分钟lastTriggered: 0,status: 'inactive'});}addHandler(severity, handler) {if (!this.handlers.has(severity)) {this.handlers.set(severity, []);}this.handlers.get(severity).push(handler);}checkAlerts(metrics) {const now = Date.now();const triggeredAlerts = [];for (const [name, alert] of this.alerts) {if (alert.status === 'inactive' && now - alert.lastTriggered > alert.cooldown) {if (alert.condition(metrics)) {alert.status = 'active';alert.lastTriggered = now;triggeredAlerts.push(name);}}}return triggeredAlerts;}async notify(alertName, data) {const alert = this.alerts.get(alertName);if (!alert) return;const handlers = this.handlers.get(alert.severity) || [];await Promise.all(handlers.map(handler => handler(alertName, data)));}
}// 2. 通知发送器
class NotificationSender {constructor() {this.channels = new Map();}addChannel(name, sender) {this.channels.set(name, sender);}async send(channel, message) {const sender = this.channels.get(channel);if (!sender) {throw new Error(`Channel ${channel} not found`);}try {await sender(message);} catch (error) {console.error(`Failed to send notification to ${channel}:`, error);throw error;}}async broadcast(message) {const results = await Promise.allSettled(Array.from(this.channels.entries()).map(([channel, sender]) => this.send(channel, message)));return results.reduce((summary, result, index) => {const channel = Array.from(this.channels.keys())[index];summary[channel] = result.status === 'fulfilled';return summary;}, {});}
}// 3. 报告生成器
class ReportGenerator {constructor() {this.templates = new Map();}addTemplate(name, template) {this.templates.set(name, template);}generateReport(data, templateName = 'default') {const template = this.templates.get(templateName);if (!template) {throw new Error(`Template ${templateName} not found`);}return template(data);}async saveReport(report, format = 'json') {const fileName = `performance-report-${new Date().toISOString()}.${format}`;switch (format) {case 'json':await fs.writeFile(fileName, JSON.stringify(report, null, 2));break;case 'html':await fs.writeFile(fileName, this.generateHtmlReport(report));break;case 'pdf':await this.generatePdfReport(report, fileName);break;default:throw new Error(`Unsupported format: ${format}`);}return fileName;}generateHtmlReport(report) {// 生成HTML格式的报告return `<!DOCTYPE html><html><head><title>Performance Report</title></head><body><h1>Performance Report</h1><div class="metrics">${this.renderMetrics(report.metrics)}</div><div class="issues">${this.renderIssues(report.issues)}</div><div class="recommendations">${this.renderRecommendations(report.recommendations)}</div></body></html>`;}
}
最佳实践建议 💡
- 性能监控策略
// 1. 性能预算管理器
class PerformanceBudgetManager {constructor() {this.budgets = new Map();}setBudget(metric, limit) {this.budgets.set(metric, limit);}checkBudget(metric, value) {const limit = this.budgets.get(metric);if (!limit) return true;return value <= limit;}generateBudgetReport(metrics) {const report = {timestamp: new Date(),violations: [],status: 'pass'};for (const [metric, limit] of this.budgets) {const value = metrics[metric];if (value > limit) {report.violations.push({metric,limit,value,overage: value - limit});report.status = 'fail';}}return report;}
}// 2. 性能监控配置管理器
class MonitoringConfigManager {constructor() {this.config = {sampleRate: 0.1,metrics: new Set(),alertThresholds: new Map()};}setSampleRate(rate) {if (rate < 0 || rate > 1) {throw new Error('Sample rate must be between 0 and 1');}this.config.sampleRate = rate;}enableMetric(metric) {this.config.metrics.add(metric);}disableMetric(metric) {this.config.metrics.delete(metric);}setAlertThreshold(metric, threshold) {this.config.alertThresholds.set(metric, threshold);}shouldCollect() {return Math.random() < this.config.sampleRate;}
}// 3. 性能数据存储管理器
class MetricsStorageManager {constructor() {this.storage = new Map();this.retention = 30 * 24 * 60 * 60 * 1000; // 30天}store(metrics) {const timestamp = Date.now();this.storage.set(timestamp, metrics);this.cleanup();}cleanup() {const cutoff = Date.now() - this.retention;for (const [timestamp] of this.storage) {if (timestamp < cutoff) {this.storage.delete(timestamp);}}}query(timeRange) {const results = [];for (const [timestamp, metrics] of this.storage) {if (timestamp >= timeRange.start && timestamp <= timeRange.end) {results.push({ timestamp, metrics });}}return results;}
}
结语 📝
JavaScript性能监控系统是保证应用性能和用户体验的关键工具。通过本文,我们学习了:
- 性能指标的收集和分析
- 性能监控工具的实现
- 报警和通知系统的构建
- 性能报告的生成
- 最佳实践和优化建议
💡 学习建议:在实施性能监控时,要注意平衡监控的全面性和系统性能的开销。选择合适的采样率和监控指标,避免监控系统本身成为性能瓶颈。同时,要建立完善的报警机制和响应流程,确保能够及时发现和解决性能问题。
如果你觉得这篇文章有帮助,欢迎点赞收藏,也期待在评论区看到你的想法和建议!👇
终身学习,共同成长。
咱们下一期见
💻
相关文章:
JavaScript系列(58)--性能监控系统详解
JavaScript性能监控系统详解 📊 今天,让我们深入探讨JavaScript的性能监控系统。性能监控对于保证应用的稳定性和用户体验至关重要。 性能监控基础概念 🌟 💡 小知识:JavaScript性能监控是指通过收集和分析各种性能指…...

GESP2023年12月认证C++六级( 第三部分编程题(1)闯关游戏)
参考程序代码: #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <string> #include <map> #include <iostream> #include <cmath> using namespace std;const int N 10…...
git 新项目
新项目git 新建的项目如何进行git 配置git git config --global user.name "cc" git config --global user.email ccexample.com配置远程仓库路径 // 添加 git remote add origin http://gogs/cc/mc.git //如果配错了,删除 git remote remove origin初…...
系统URL整合系列视频一(需求方案)
视频 系统URL整合系列视频一(需求方案) 视频介绍 (全国)某大型分布式系统Web资源URL整合需求实现方案讲解。当今社会各行各业对软件系统的web资源访问权限控制越来越严格,控制粒度也越来越细。安全级别提高的同时也增…...
Vue.js 使用组件库构建 UI
Vue.js 使用组件库构建 UI 在 Vue.js 项目中,构建漂亮又高效的用户界面(UI)是很重要的一环。组件库就是你开发 UI 的好帮手,它可以大大提高开发效率,减少重复工作,还能让你的项目更具一致性和专业感。今天…...

计算图 Compute Graph 和自动求导 Autograd | PyTorch 深度学习实战
前一篇文章,Tensor 基本操作5 device 管理,使用 GPU 设备 | PyTorch 深度学习实战 本系列文章 GitHub Repo: https://github.com/hailiang-wang/pytorch-get-started PyTorch 计算图和 Autograd 微积分之于机器学习Computational Graphs 计算图Autograd…...

51单片机入门_05_LED闪烁(常用的延时方法:软件延时、定时器延时;while循环;unsigned char 可以表示的数字是0~255)
本篇介绍编程实现LED灯闪烁,需要学到一些新的C语言知识。由于单片机执行的速度是非常快的,如果不进行延时的话,人眼是无法识别(停留时间要大于20ms)出LED灯是否在闪烁所以需要学习如何实现软件延时。另外IO口与一个字节位的数据对应关系。 文…...

如何获取sql数据中时间的月份、年份(类型为date)
可用自带的函数month来实现 如: 创建表及插入数据: create table test (id int,begindate datetime) insert into test values (1,2015-01-01) insert into test values (2,2015-02-01) 执行sql语句,获取月份: select MONTH(begindate)…...

【单层神经网络】softmax回归的从零开始实现(图像分类)
softmax回归 该回归分析为后续的多层感知机做铺垫 基本概念 softmax回归用于离散模型预测(分类问题,含标签) softmax运算本质上是对网络的多个输出进行了归一化,使结果有一个统一的判断标准,不必纠结为什么要这么算…...

使用开源项目:pdf2docx,让PDF转换为Word
目录 1.安装python 2.安装 pdf2docx 3.使用 pdf2docx 转换 PDF 到 Word pdf2docx:GitCode - 全球开发者的开源社区,开源代码托管平台 环境:windows电脑 1.安装python Download Python | Python.org 最好下载3.8以上的版本 安装时记得选择上&#…...

保姆级教程Docker部署KRaft模式的Kafka官方镜像
目录 一、安装Docker及可视化工具 二、单节点部署 1、创建挂载目录 2、运行Kafka容器 3、Compose运行Kafka容器 4、查看Kafka运行状态 三、集群部署 四、部署可视化工具 1、创建挂载目录 2、运行Kafka-ui容器 3、Compose运行Kafka-ui容器 4、查看Kafka-ui运行状态 …...

ChatGPT提问技巧:行业热门应用提示词案例--咨询法律知识
ChatGPT除了可以协助办公,写作文案和生成短视频脚本外,和还可以做为一个法律工具,当用户面临一些法律知识盲点时,可以向ChatGPT咨询获得解答。赋予ChatGPT专家的身份,用户能够得到较为满意的解答。 1.咨询法律知识 举…...

openRv1126 AI算法部署实战之——Tensorflow模型部署实战
在RV1126开发板上部署Tensorflow算法,实时目标检测RTSP传输。视频演示地址 rv1126 yolov5 实时目标检测 rtsp传输_哔哩哔哩_bilibili 一、准备工作 从官网下载tensorflow模型和数据集 手动在线下载: https://github.com/tensorflow/models/b…...

STM32 TIM定时器配置
TIM简介 TIM(Timer)定时器 定时器可以对输入的时钟进行计数,并在计数值达到设定值时触发中断 16位计数器、预分频器、自动重装寄存器的时基单元,在72MHz计数时钟下可以实现最大59.65s的定时 不仅具备基本的定时中断功能ÿ…...

51单片机 05 矩阵键盘
嘻嘻,LCD在RC板子上可以勉强装上,会有一点歪。 一、矩阵键盘 在键盘中按键数量较多时,为了减少I/O口的占用,通常将按键排列成矩阵形式;采用逐行或逐列的“扫描”,就可以读出任何位置按键的状态。…...

SSRF 漏洞利用 Redis 实战全解析:原理、攻击与防范
目录 前言 SSRF 漏洞深度剖析 Redis:强大的内存数据库 Redis 产生漏洞的原因 SSRF 漏洞利用 Redis 实战步骤 准备环境 下载安装 Redis 配置漏洞环境 启动 Redis 攻击机远程连接 Redis 利用 Redis 写 Webshell 防范措施 前言 在网络安全领域࿰…...

kubernetes学习-配置管理(九)
一、ConfigMap (1)通过指定目录,创建configmap # 创建一个config目录 [rootk8s-master k8s]# mkdir config[rootk8s-master k8s]# cd config/ [rootk8s-master config]# mkdir test [rootk8s-master config]# cd test [rootk8s-master test…...

python 语音识别
目录 一、语音识别 二、代码实践 2.1 使用vosk三方库 2.2 使用SpeechRecognition 2.3 使用Whisper 一、语音识别 今天识别了别人做的这个app,觉得虽然是个日记app 但是用来学英语也挺好的,能进行语音识别,然后矫正语法,自己说的时候 ,实在不知道怎么说可以先乱说,然…...

一文速览DeepSeek-R1的本地部署——可联网、可实现本地知识库问答:包括671B满血版和各个蒸馏版的部署
前言 自从deepseek R1发布之后「详见《一文速览DeepSeek R1:如何通过纯RL训练大模型的推理能力以比肩甚至超越OpenAI o1(含Kimi K1.5的解读)》」,deepseek便爆火 爆火以后便应了“人红是非多”那句话,不但遭受各种大规模攻击,即便…...

[mmdetection]fast-rcnn模型训练自己的数据集的详细教程
本篇博客是由本人亲自调试成功后的学习笔记。使用了mmdetection项目包进行fast-rcnn模型的训练,数据集是自制图像数据。废话不多说,下面进入训练步骤教程。 注:本人使用linux服务器进行展示,Windows环境大差不差。另外࿰…...

iOS 26 携众系统重磅更新,但“苹果智能”仍与国行无缘
美国西海岸的夏天,再次被苹果点燃。一年一度的全球开发者大会 WWDC25 如期而至,这不仅是开发者的盛宴,更是全球数亿苹果用户翘首以盼的科技春晚。今年,苹果依旧为我们带来了全家桶式的系统更新,包括 iOS 26、iPadOS 26…...
React Native 导航系统实战(React Navigation)
导航系统实战(React Navigation) React Navigation 是 React Native 应用中最常用的导航库之一,它提供了多种导航模式,如堆栈导航(Stack Navigator)、标签导航(Tab Navigator)和抽屉…...

2025年能源电力系统与流体力学国际会议 (EPSFD 2025)
2025年能源电力系统与流体力学国际会议(EPSFD 2025)将于本年度在美丽的杭州盛大召开。作为全球能源、电力系统以及流体力学领域的顶级盛会,EPSFD 2025旨在为来自世界各地的科学家、工程师和研究人员提供一个展示最新研究成果、分享实践经验及…...
day52 ResNet18 CBAM
在深度学习的旅程中,我们不断探索如何提升模型的性能。今天,我将分享我在 ResNet18 模型中插入 CBAM(Convolutional Block Attention Module)模块,并采用分阶段微调策略的实践过程。通过这个过程,我不仅提升…...

Redis相关知识总结(缓存雪崩,缓存穿透,缓存击穿,Redis实现分布式锁,如何保持数据库和缓存一致)
文章目录 1.什么是Redis?2.为什么要使用redis作为mysql的缓存?3.什么是缓存雪崩、缓存穿透、缓存击穿?3.1缓存雪崩3.1.1 大量缓存同时过期3.1.2 Redis宕机 3.2 缓存击穿3.3 缓存穿透3.4 总结 4. 数据库和缓存如何保持一致性5. Redis实现分布式…...

ETLCloud可能遇到的问题有哪些?常见坑位解析
数据集成平台ETLCloud,主要用于支持数据的抽取(Extract)、转换(Transform)和加载(Load)过程。提供了一个简洁直观的界面,以便用户可以在不同的数据源之间轻松地进行数据迁移和转换。…...

Python爬虫(一):爬虫伪装
一、网站防爬机制概述 在当今互联网环境中,具有一定规模或盈利性质的网站几乎都实施了各种防爬措施。这些措施主要分为两大类: 身份验证机制:直接将未经授权的爬虫阻挡在外反爬技术体系:通过各种技术手段增加爬虫获取数据的难度…...

2025盘古石杯决赛【手机取证】
前言 第三届盘古石杯国际电子数据取证大赛决赛 最后一题没有解出来,实在找不到,希望有大佬教一下我。 还有就会议时间,我感觉不是图片时间,因为在电脑看到是其他时间用老会议系统开的会。 手机取证 1、分析鸿蒙手机检材&#x…...

项目部署到Linux上时遇到的错误(Redis,MySQL,无法正确连接,地址占用问题)
Redis无法正确连接 在运行jar包时出现了这样的错误 查询得知问题核心在于Redis连接失败,具体原因是客户端发送了密码认证请求,但Redis服务器未设置密码 1.为Redis设置密码(匹配客户端配置) 步骤: 1).修…...

10-Oracle 23 ai Vector Search 概述和参数
一、Oracle AI Vector Search 概述 企业和个人都在尝试各种AI,使用客户端或是内部自己搭建集成大模型的终端,加速与大型语言模型(LLM)的结合,同时使用检索增强生成(Retrieval Augmented Generation &#…...