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

我的梦境 提交于 2019-11-30 19:07:05

问题


In the following image, I want the total response time from the webpage. I can't seem to find it in the file sample HAR file, i.e. 38.79s in this case. Does anyone know how to get this?

I am going to use Selenium along with Firebug and NetExport to export the HAR file, but right now I am trying to do it manually. Adding the individual responses does not give correct numbers.

At some point I would like a Java program to find and extract the total response time.


回答1:


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.




回答2:


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");
         }
     }

}




回答3:


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;
}



回答4:


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");
         }
     }
 }


来源:https://stackoverflow.com/questions/30745931/how-to-get-total-web-page-response-time-from-a-har-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!