提交 7b8b2093 编写于 作者: Q qq_38870145

Auto Commit

上级 c9521c44
...@@ -2,11 +2,25 @@ ...@@ -2,11 +2,25 @@
<script setup> <script setup>
import { ref, onBeforeMount } from "vue"; import { ref, onBeforeMount, reactive } from "vue";
import { ListenDomChange } from '@/utils/index' import { ListenDomChange } from '@/utils/index'
import zhCN from "ant-design-vue/es/locale/zh_CN"; import zhCN from "ant-design-vue/es/locale/zh_CN";
import dayjs from "dayjs"; import dayjs from "dayjs";
import "dayjs/locale/zh-cn"; import "dayjs/locale/zh-cn";
import { useStore } from "vuex";
dayjs.locale("zh-cn");
const locale = ref(zhCN);
const findAppCount = ref(0)
const store = useStore();
const state = reactive({
performanceConfig: {
}
})
/** 下载图片 */ /** 下载图片 */
const downloadBase64 = (content, fileName) => { const downloadBase64 = (content, fileName) => {
const base64ToBlob = function (code) { const base64ToBlob = function (code) {
...@@ -46,24 +60,38 @@ const shotAction = () => { ...@@ -46,24 +60,38 @@ const shotAction = () => {
}); });
} }
dayjs.locale("zh-cn");
const locale = ref(zhCN);
const findAppCount = ref(0)
const findAppDom = () => { const findAppDom = () => {
const appDom = document.getElementById('app') const appDom = document.getElementById('app')
findAppCount.value += 1 findAppCount.value += 1
if (appDom) { if (appDom) {
console.log('appDom', appDom) console.log('appDom', appDom)
const listenDomChange = new ListenDomChange(appDom) const listenDomChange = new ListenDomChange()
console.log('getPerformance', listenDomChange.getPerformance()) // state.performanceConfig = {
// ...state.performanceConfig,
// ...listenDomChange.getPerformance()
// }
console.log('appDom', appDom)
listenDomChange.mutationAction(appDom, () => {
console.log('第一次渲染', performance.now())
state.performanceConfig = {
...state.performanceConfig,
fmp: performance.now()
}
store.commit("common/setPerformanceConfig", { ...state.performanceConfig });
})
} }
else if (findAppCount <= 1000) { else if (findAppCount <= 1000) {
findAppDom() findAppDom()
} }
} }
onBeforeMount(() => { onBeforeMount(() => {
// 白屏时间
const fp = performance.now()
state.performanceConfig = {
...state.performanceConfig,
fp: fp
}
findAppDom(); findAppDom();
}) })
</script> </script>
......
<script lang="js" setup> <script lang="js" setup>
import { reactive, onMounted } from 'vue'; import { reactive, onMounted } from 'vue';
import * as echarts from 'echarts'; import * as echarts from 'echarts';
import { useStore } from "vuex";
const store = useStore();
const state = reactive({ const state = reactive({
leftTitle: '原生的performance', leftTitle: '原生的performance',
leftDomId: 'visual-performance-id', leftDomId: 'visual-performance-id',
...@@ -26,8 +30,8 @@ const initLeftChart = () => { ...@@ -26,8 +30,8 @@ const initLeftChart = () => {
console.log(Object.keys(performance.timing)) console.log(Object.keys(performance.timing))
const label = [] const label = []
const data = [] const data = []
for ( let key in performance.timing) { for (let key in performance.timing) {
if (key!='toJSON') { if (key != 'toJSON') {
label.push(key) label.push(key)
data.push(performance.timing[key]) data.push(performance.timing[key])
} }
...@@ -87,10 +91,22 @@ const initRightChart = () => { ...@@ -87,10 +91,22 @@ const initRightChart = () => {
else { else {
return return
} }
const performanceConfig = store.getters["common/performanceConfig"]
console.log('performanceConfig________________', performanceConfig)
const label = []
const data = []
Object.keys(performanceConfig).forEach(key => {
data.push(performanceConfig[key])
label.push(key)
})
const myChart = echarts.init(domInstance); const myChart = echarts.init(domInstance);
const option = { const option = {
title: { title: {
text: state.chartTitle text: '自定义计算fmp'
}, },
tooltip: { tooltip: {
trigger: 'axis', trigger: 'axis',
...@@ -111,13 +127,13 @@ const initRightChart = () => { ...@@ -111,13 +127,13 @@ const initRightChart = () => {
}, },
yAxis: { yAxis: {
type: 'category', type: 'category',
data: ['fmp', 'fp', 'dns', 'res', 'dom'] data: label
}, },
series: [ series: [
{ {
name: '2011', name: 'fmp计算',
type: 'bar', type: 'bar',
data: [18203, 23489, 29034, 104970, 131744] data: data
} }
] ]
}; };
...@@ -141,16 +157,43 @@ onMounted(() => { ...@@ -141,16 +157,43 @@ onMounted(() => {
<template> <template>
<div> <div>
<div style="display:flex;justify-content: space-between;"> <div style="display:flex;">
<a-card :title="state.leftTitle" style="width: 600px"> <a-card :title="state.leftTitle" style="width: 600px">
<div :id="state.leftDomId" style="width: 500px;height:600px;"> <div :id="state.leftDomId" style="width: 500px;height:600px;">
</div> </div>
</a-card>
<a-card :title="state.rightTitle" style="width: 600px;margin-left:20px">
<div :id="state.rightDomId" style="width: 500px;height:600px;">
</div>
<div>
</div>
</a-card> </a-card>
<a-card style="margin-left:20px" title="定义">
<div>
<div>
fetchStart:浏览器发起资源请求时,有缓存时,则返回读取缓存的开始时间。<br>
domainLookupStart:查询 DNS 的开始时间。<br>
domainLookupEnd:查询 DNS 的结束时间。<br>
connectStart:浏览器开始与服务器连接时的时间。<br>
secureConnectionStart:如果页面使用 HTTPS,它的值是安全连接握手之前的时刻。<br>
connectEnd:当浏览器端完成与服务器端建立连接的时刻。<br>
responseStart:指客户端收到从服务器端(或缓存、本地资源)响应回的第一个字节的数据的时刻。<br>
<a-card :title="state.rightTitle" style="width: 600px"> responseEnd:指客户端收到从服务器端(或缓存、本地资源)响应回的最后一个字节的数据的时刻。<br>
<div :id="state.rightDOmId" style="width: 500px;height:600px;">
</div>
</div> </div>
</a-card> </a-card>
</div> </div>
......
...@@ -4,8 +4,10 @@ import Antd from 'ant-design-vue'; ...@@ -4,8 +4,10 @@ import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css'; import 'ant-design-vue/dist/antd.css';
import './assets/main.css' import './assets/main.css'
import Router from "./router/index.js"; import Router from "./router/index.js";
import store from "./store/index.js";
createApp(App) createApp(App)
.use(Antd) .use(Antd)
.use(store)
.use(Router) .use(Router)
.mount('#app') .mount('#app')
// @ts-ignore
import { createStore, createLogger } from "vuex";
import common from "./modules/common";
const debug = process.env.NODE_ENV !== "production";
export default createStore({
modules: {
common,
},
strict: debug,
plugins: debug ? [createLogger()] : [],
});
const state = () => ({
performanceConfig: {
}
});
const getters = {
performanceConfig: (state) => {
return state.performanceConfig;
}
};
const mutations = {
setPerformanceConfig(state, payload) {
console.log('payload', payload)
state.performanceConfig = payload;
}
}
const actions = {
setPerformanceConfigAction({ commit, state }, payload) {
commit("setPerformanceConfig", payload);
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};
\ No newline at end of file
class ListenDomChange { class ListenDomChange {
// 监听 dom 节点
dom = null;
// observer // observer
observer = null; observer = null
// performance // performance
performanceConfig = {}; performanceConfig = {}
constructor(dom, callbackDomChange) { constructor() {
this.dom = dom;
this.callbackDomChange = callbackDomChange;
// 初始化为{} // 初始化为{}
this.performanceConfig = {} this.performanceConfig = {}
// 计算performanceConfig // 计算performanceConfig
this.calcPerformance() this.calcPerformance()
this.mutationListen()
} }
...@@ -61,14 +56,15 @@ class ListenDomChange { ...@@ -61,14 +56,15 @@ class ListenDomChange {
} }
// 监听 dom变化 // 监听 dom变化
mutationListen() { mutationAction(listenDom, callbackAction) {
console.log('listenDom', listenDom)
// 观察器的配置(需要观察什么变动) // 观察器的配置(需要观察什么变动)
const config = { attributes: true, childList: true, subtree: true }; const config = { attributes: true, childList: true, subtree: true };
// 当观察到变动时执行的回调函数 // 当观察到变动时执行的回调函数
const callback = function (mutationsList, observer) { const callback = function (mutationsList, observer) {
console.log('this.dom', this.dom) console.log('listenDom', listenDom)
const renderHeight = this.dom.offsetHeight const renderHeight = listenDom.offsetHeight
console.log('renderHeight_____________', renderHeight) console.log('renderHeight_____________', renderHeight)
console.log('change_______________', mutationsList) console.log('change_______________', mutationsList)
...@@ -82,24 +78,19 @@ class ListenDomChange { ...@@ -82,24 +78,19 @@ class ListenDomChange {
// } // }
if (parseInt(renderHeight)) { if (parseInt(renderHeight)) {
this.setPerformance('fmp', performance.now()) // 第一次监听dom 存在高度则判定已经渲染完root节点,不关注子节点
this.clearObserver() callbackAction()
// 停止观察
observer.disconnect();
} }
}; };
// 创建一个观察器实例并传入回调函数 // 创建一个观察器实例并传入回调函数
this.observer = new MutationObserver(callback); const observer = new MutationObserver(callback);
// 以上述配置开始观察目标节点 // 以上述配置开始观察目标节点
this.observer.observe(this.dom, config); observer.observe(listenDom, config);
} }
// 可停止观察
clearObserver() {
this.observer.disconnect();
}
} }
export { export {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册