I'm currently utilizing the PerformanceObserver API to measure server traffic for given endpoints on the client side. However, I'm exploring alternative solutions due to potential limitations or lack of support in certain environments. Here's the code snippet for reference:
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
const request = entry.responseEnd - entry.requestStart;
console.log('Request time:', request);
});
});
observer.observe({ type: "resource", buffered: true });
// Simulating a fetch request
const endpoint = 'https://example.com/api/data';
fetch(endpoint, {
method: 'GET',
mode: 'no-cors'
})
.then(response => response.json())
.then(data => console.log('Data received:', data))
.catch(error => console.error('Error:', error));
In my actual application, there might be several endpoints(servers) to measure the traffic of and it's crucial to account for the time a request spends in the task queue. Are there alternative solutions or approaches that can effectively handle this requirement?