更新时间:2022-10-19 15:06:40
The total load time is not calculated by summarizing all request times but by the latest request end time. Graphically spoken it is the right end of the request bar ending at the far right. In your example screenshot it's either the last, third to last or fourth to last request.
The request end time is calculated by the request start time indicated by the startedDateTime
property of a request plus the time span needed for the response, which is available through the time
property of each request.
To get the maximum request end time, you need to loop over all requests and compare the end time of each request. See the following code:
var startTime = new Date(har.log.pages[0].startedDateTime);
var loadTime = 0;
// Loop over all entries to determine the latest request end time
// The variable 'har' contains the JSON of the HAR file
har.log.entries.forEach(function(entry) {
var entryLoadTime = new Date(entry.startedDateTime);
// Calculate the current request's end time by adding the time it needed to load to its start time
entryLoadTime.setMilliseconds(entryLoadTime.getMilliseconds() + entry.time);
// If the current request's end time is greater than the current latest request end time, then save it as new latest request end time
if (entryLoadTime > loadTime) {
loadTime = entryLoadTime;
}
});
var loadTimeSpan = loadTime - startTime;
Executing this code the variable loadTimeSpan
will contain the wanted time span in milliseconds.
Important note:
The so calculated time span may still differ from the time displayed by Firebug or the online HAR Viewer, because they split the requests into different phases depending on the time elapsed between two requests. They then calculate the time span from the first to the last request of each phase and summarize those time spans in the end.