如何从HAR文件中获取总页面响应时间?

时间:2015-06-10 02:08:36

标签: javascript firebug page-load-time har

在下图中,我想要来自网页的总响应时间。我似乎无法在文件sample HAR file中找到它,即在这种情况下为38.79。有谁知道如何得到这个?

我将使用Selenium以及Firebug和NetExport来导出HAR文件,但是现在我正在尝试手动执行。添加单个响应不会给出正确的数字。

enter image description here

在某些时候,我希望Java程序能够找到并提取总响应时间。

4 个答案:

答案 0 :(得分:4)

总加载时间不是通过汇总所有请求时间而是通过最新请求结束时间来计算的。从图形上讲,它是在最右边结束的请求栏的右端。在您的示例屏幕截图中,它是最后一个,第三个到最后一个或第四个到最后一个请求。

请求结束时间由请求的startedDateTime属性指示的请求开始时间加上响应所需的时间跨度计算,该时间跨度可通过每个请求的time属性获得。 要获得最大请求结束时间,您需要遍历所有请求并比较每个请求的结束时间。请参阅以下代码:

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;

执行此代码时,变量loadTimeSpan将包含所需的时间跨度(以毫秒为单位)。

重要提示:

如此计算的时间跨度可能仍然与Firebug或在线HAR Viewer显示的时间不同,因为它们根据两个请求之间经过的时间将请求分成不同的阶段。然后,他们计算从每个阶段的第一个请求到最后一个请求的时间跨度,并总结那些时间跨度。

答案 1 :(得分:2)

根据@Sebastian Zartner的回答和Google sample,以下是Java尝试:

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

}

答案 2 :(得分:0)

这是一个返回页面加载时间的Java函数。此外,它允许您解析具有缺少元素或其他违规的损坏的HAR文件到规范。更多信息。在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;
}

答案 3 :(得分:0)

基于上面的输入

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