-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
149 lines (131 loc) · 6.94 KB
/
index.html
File metadata and controls
149 lines (131 loc) · 6.94 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Script</title>
</head>
<body>
<script>
// Retrieve the request count and timings from sessionStorage or set defaults
let requestCount = parseInt(sessionStorage.getItem("requestCount")) || 0;
let requestTimings =
JSON.parse(sessionStorage.getItem("requestTimings")) || [];
// Function to record performance data
function recordPerformanceData() {
const navigationEntries =
window.performance.getEntriesByType("navigation");
if (navigationEntries.length > 0) {
const entry = navigationEntries[0];
// Calculate metrics
const connectEndToRequestStart =
entry.requestStart - entry.connectEnd;
const startTimeToConnectEnd = entry.connectEnd - entry.startTime;
const responseEndToDomComplete =
entry.domComplete - entry.responseEnd;
const connectStartToConnectEnd =
entry.connectEnd - entry.connectStart;
const connectStart = entry.connectStart;
const connectEnd = entry.connectEnd;
const requestStart = entry.requestStart;
const domainLookupStart = entry.domainLookupStart;
const domainLookupEnd = entry.domainLookupEnd;
const fetchStart = entry.fetchStart;
window.performance.mark("connectStartVZ", { startTime: connectStart});
window.performance.mark("connectEndVZ", { startTime: connectEnd});
window.performance.mark("requestStartVZ", { startTime: requestStart});
window.performance.mark("domainLookupStartVZ", { startTime: domainLookupStart});
window.performance.mark("domainLookupEndVZ", { startTime: domainLookupEnd});
window.performance.mark("fetchStartVZ", { startTime: fetchStart});
// Store metrics as an object
requestTimings.push({
connectEndToRequestStart,
startTimeToConnectEnd,
responseEndToDomComplete,
connectStartToConnectEnd,
connectStart,
connectEnd,
requestStart
});
sessionStorage.setItem(
"requestTimings",
JSON.stringify(requestTimings)
);
}
}
// Function to calculate and log statistics (avg, p50, p75, p95)
function calculateAndLogStats() {
// Read the latest timings from sessionStorage to ensure we have the correct data
const storedTimings =
JSON.parse(sessionStorage.getItem("requestTimings")) || [];
if (storedTimings.length > 0) {
// Helper function to calculate percentiles
function calculatePercentiles(values) {
const sorted = [...values].sort((a, b) => a - b);
const avg =
sorted.reduce((sum, value) => sum + value, 0) / sorted.length;
const p50 = sorted[Math.floor(0.5 * sorted.length)];
const p75 = sorted[Math.floor(0.75 * sorted.length)];
const p95 = sorted[Math.floor(0.95 * sorted.length)];
const p99 = sorted[Math.floor(0.99 * sorted.length)];
return { avg, p50, p75, p95, p99 };
}
// Extract individual metrics
const connectEndToRequestStart = storedTimings.map(
(t) => t.connectEndToRequestStart
);
const startTimeToConnectEnd = storedTimings.map(
(t) => t.startTimeToConnectEnd
);
const responseEndToDomComplete = storedTimings.map(
(t) => t.responseEndToDomComplete
);
const connectStartToConnectEnd = storedTimings.map(
(t) => t.connectStartToConnectEnd
);
// Calculate statistics for each metric
const connectStats = calculatePercentiles(connectEndToRequestStart);
const startToConnectStats = calculatePercentiles(
startTimeToConnectEnd
);
const responseToDomStats = calculatePercentiles(
responseEndToDomComplete
);
const reuseConnectStats = calculatePercentiles(
connectStartToConnectEnd
);
// Log the results
console.log("ConnectEnd to RequestStart:", connectStats);
console.log("StartTime to ConnectEnd:", startToConnectStats);
console.log("ResponseEnd to DomComplete:", responseToDomStats);
console.log("ConnectStart to ConnectEnd:", reuseConnectStats);
} else {
console.log("No performance data available.");
}
// Clear sessionStorage after logging
sessionStorage.removeItem("requestCount");
sessionStorage.removeItem("requestTimings");
}
// Function to handle page reloads and data collection
function openPageAndCollectData() {
if (requestCount < 5) {
requestCount++;
// Save the updated request count and timings to sessionStorage
sessionStorage.setItem("requestCount", requestCount);
recordPerformanceData();
// Trigger the next page load after a short delay
setTimeout(() => {
window.location.reload(); // Reload the page
}, 10); // Delay before reloading the page
} else {
// Once we have 100 timings, calculate and log the statistics
calculateAndLogStats();
}
}
// Trigger the page load and data collection once the page is fully loaded
window.onload = function () {
openPageAndCollectData();
};
</script>
</body></html>