How to get total web page response time from a HAR file?

允我心安 提交于 2019-12-01 00:50:53

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.

Neo84

Based on @Sebastian Zartner's answer and the Google sample, the following is an Java attempt:

public class ParseHarFile {

     public static void main(String[] args) {
         String fileName = "www.google.com.har";

         ReadHarFile(fileName);
    }

    public static void ReadHarFile(String fileName){

         File f = new File("C:\\Users\\Administrator\\Desktop\\test_files\\" + fileName);
         HarFileReader r = new HarFileReader();

         try
         {
             System.out.println("Reading " + fileName);
             HarLog log = r.readHarFile(f);

             // Access all pages elements as an object
              HarPages pages = log.getPages();

              long startTime =   pages.getPages().get(0).getStartedDateTime().getTime();

              System.out.println("page start time: " + startTime);

             // Access all entries elements as an object
             HarEntries entries = log.getEntries();

             List<HarEntry> hentry = entries.getEntries();

             long loadTime = 0;

             int entryIndex = 0;
             //Output "response" code of entries.
             for (HarEntry entry : hentry)
             {
                 System.out.println("entry: " + entryIndex);
                 System.out.println("request code: " + entry.getRequest().getMethod()); //Output request type
                 System.out.println("    start time: " + entry.getStartedDateTime().getTime()); // Output start time
                 System.out.println("    time: " + entry.getTime()); // Output start time

                 long entryLoadTime = entry.getStartedDateTime().getTime() + entry.getTime();

                 if(entryLoadTime > loadTime){
                     loadTime = entryLoadTime;
                 }

                 System.out.println();
                 entryIndex++;
             }

             long loadTimeSpan = loadTime - startTime;
             System.out.println("loadTimeSpan: " + loadTimeSpan);

             Double webLoadTime = ((double)loadTimeSpan) / 1000;
             double webLoadTimeInSeconds = Math.round(webLoadTime * 100.0) / 100.0; 
             System.out.println("Web Load Time: " + webLoadTimeInSeconds) ;

         }
         catch (JsonParseException e)
         {
             e.printStackTrace();
             System.out.println("Parsing error during test");
         }
         catch (IOException e)
         {
             e.printStackTrace();
             System.out.println("IO exception during test");
         }
     }

}

Here's a Java function that returns page load time. In addition, it allows you to parse corrupted HAR files that have missing elements or other violations to the specification. More info. on HarLib here.

public long calculatePageLoadTime(String filename) {
    File file = new File("c:\\" + filename);
    HarFileReader fileReader = new HarFileReader();
    long pageLoadTime = 0;
    try {
        //Catch missing elements or other violations to HAR specification.
        //You can still read most of a file even if some parts are corrupted.
        List<HarWarning> warnings = new ArrayList<HarWarning>();
        HarLog log = fileReader.readHarFile(file, warnings);
        for (HarWarning warning : warnings)
            System.out.println("File:" + filename + " - Warning:" + warning);

        //Get page load start time
        HarPages pages = log.getPages();
        HarPage page = pages.getPages().get(0);
        long startTime = page.getStartedDateTime().getTime();

        //Traverse entries and determine the latest request end time
        long loadTime = 0;            
        HarEntries entries = log.getEntries();
        List<HarEntry> entryList = entries.getEntries();
        for (HarEntry entry : entryList)
        {
            long entryLoadTime = entry.getStartedDateTime().getTime() + entry.getTime();
            if(entryLoadTime > loadTime){
                loadTime = entryLoadTime;
            }
        }

        pageLoadTime = loadTime - startTime;
    }
    catch (JsonParseException e)
    {
      e.printStackTrace();
      System.out.println("Parsing error during test");
    }
    catch (IOException e)
    {
      e.printStackTrace();
      System.out.println("IO exception during test");
    }

    return pageLoadTime;
}

Based on input from above

import edu.umass.cs.benchlab.har.*;
import edu.umass.cs.benchlab.har.tools.*;

import java.io.File;
import java.io.*;
import java.util.List;
import org.codehaus.jackson.JsonParseException;

public class ParseHarFile {

     public static void main(String[] args) {
         String fileName = "test.har";
         ReadHarFile(fileName);
    }

    public static void ReadHarFile(String fileName){

         File f = new File(fileName);
         HarFileReader r = new HarFileReader();

         try
         {
             System.out.println("Reading " + fileName);
             HarLog log = r.readHarFile(f);

             // Access all pages elements as an object
              HarPages pages = log.getPages();

              long startTime =   pages.getPages().get(0).getStartedDateTime().getTime();

              System.out.println("page start time: " + startTime);

             // Access all entries elements as an object
             HarEntries entries = log.getEntries();

             List<HarEntry> hentry = entries.getEntries();

             long loadTime = 0;
             long responseSize=0;


             int entryIndex = 0;
             //Output "response" code of entries.
             for (HarEntry entry : hentry)
             {
                 System.out.println("entry: " + entryIndex);
                 System.out.println("request code: " + entry.getRequest().getMethod()); //Output request type
                 System.out.println("request url: "
                     + entry.getRequest().getUrl()); //Output request Url
                 System.out.println("    start time: " + entry.getStartedDateTime().getTime()); // Output start time
                 System.out.println("    time: " + entry.getTime()); // Output start time
                 System.out.println("response code: "
                     + entry.getResponse().getStatus()); //Output response code
                 responseSize=entry.getResponse().getHeadersSize()+entry.getResponse().getBodySize();


                 System.out.println("response size: "
                     + responseSize); //Output response size

                 long entryLoadTime = entry.getStartedDateTime().getTime() + entry.getTime();

                 if(entryLoadTime > loadTime){
                     loadTime = entryLoadTime;
                 }

                 System.out.println();
                 entryIndex++;
             }

             long loadTimeSpan = loadTime - startTime;
             System.out.println("loadTimeSpan: " + loadTimeSpan);

             Double webLoadTime = ((double)loadTimeSpan) / 1000;
             double webLoadTimeInSeconds = Math.round(webLoadTime * 100.0) / 100.0; 
             System.out.println("Web Load Time: " + webLoadTimeInSeconds) ;

         }
         catch (JsonParseException e)
         {
             e.printStackTrace();
             System.out.println("Parsing error during test");
         }
         catch (IOException e)
         {
             e.printStackTrace();
             System.out.println("IO exception during test");
         }
     }
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!