农业网站建设方案/seo查询站长工具
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环境大差不差。另外࿰…...

1. Kubernetes组成及常用命令
Pods(k8s最小操作单元)ReplicaSet & Label(k8s副本集和标签)Deployments(声明式配置)Services(服务)k8s常用命令Kubernetes(简称K8s)是一个开源的容器编排系统,用于自动化应用程序的部署、扩展和管理。自2014年发布以来,K8s迅速成为容器编排领域的行业标准,被…...

linux下ollama更换模型路径
Linux下更换Ollama模型下载路径指南 在使用Ollama进行AI模型管理时,有时需要根据实际需求更改模型文件的存储路径。本文将详细介绍如何在Linux系统中更改Ollama模型的下载路径。 一、关闭Ollama服务 在更改模型路径之前,需要先停止Ollama服务。…...

本地Ollama部署DeepSeek R1模型接入Word
目录 1.本地部署DeepSeek-R1模型 2.接入Word 3.效果演示 4.问题反馈 上一篇文章办公新利器:DeepSeekWord,让你的工作更高效-CSDN博客https://blog.csdn.net/qq_63708623/article/details/145418457?spm1001.2014.3001.5501https://blog.csdn.net/qq…...

【自学笔记】Git的重点知识点-持续更新
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 Git基础知识Git高级操作与概念Git常用命令 总结 Git基础知识 Git简介 Git是一种分布式版本控制系统,用于记录文件内容的改动,便于开发者追踪…...

[EAI-028] Diffusion-VLA,能够进行多模态推理和机器人动作预测的VLA模型
Paper Card 论文标题:Diffusion-VLA: Scaling Robot Foundation Models via Unified Diffusion and Autoregression 论文作者:Junjie Wen, Minjie Zhu, Yichen Zhu, Zhibin Tang, Jinming Li, Zhongyi Zhou, Chengmeng Li, Xiaoyu Liu, Yaxin Peng, Chao…...

实现数组的扁平化
文章目录 1 实现数组的扁平化1.1 递归1.2 reduce1.3 扩展运算符1.4 split和toString1.5 flat1.6 正则表达式和JSON 1 实现数组的扁平化 1.1 递归 通过循环递归的方式,遍历数组的每一项,如果该项还是一个数组,那么就继续递归遍历,…...

登录认证(5):过滤器:Filter
统一拦截 上文我们提到(登录认证(4):令牌技术),现在大部分项目都使用JWT令牌来进行会话跟踪,来完成登录功能。有了JWT令牌可以标识用户的登录状态,但是完整的登录逻辑如图所示&…...

pytorch实现门控循环单元 (GRU)
人工智能例子汇总:AI常见的算法和例子-CSDN博客 特性GRULSTM计算效率更快,参数更少相对较慢,参数更多结构复杂度只有两个门(更新门和重置门)三个门(输入门、遗忘门、输出门)处理长时依赖一般适…...

Word List 2
词汇颜色标识解释 词汇表中的生词 词汇表中的词组成的搭配、派生词 例句中的生词 我自己写的生词(用于区分易混淆的词,无颜色标识) 不认识的单词或句式 单词的主要汉语意思 不太理解的句子语法和结构 Word List 2 英文音标中文regi…...

机器学习常用包numpy篇(四)函数运算
目录 前言 一、三角函数 二、双曲函数 三、数值修约 四、 求和、求积与差分 五、 指数与对数 六、算术运算 七、 矩阵与向量运算 八、代数运算 九、 其他数学工具 总结 前言 Python 的原生运算符可实现基础数学运算(加减乘除、取余、取整、幂运算&#…...