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

后端 未结 4 1337
迷失自我
迷失自我 2021-01-07 06:36

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

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-07 07:24

    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 warnings = new ArrayList();
            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 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;
    }
    

提交回复
热议问题