【问题标题】:Different results by different methods on Chrome Performance Metrics?Chrome Performance Metrics 上不同方法的不同结果?
【发布时间】:2020-11-07 01:33:09
【问题描述】:

我正在编写一些代码来自动计算某些页面性能指标。我得到的页面大小结果因不同的方法而异:

我想要实现的是读取此屏幕截图中显示的这些值:

我正在使用的方法:

给出不同页面加载时间和不同传输大小的方法: Totalbytes 和 NetData 返回的数字非常不同,两者都与屏幕截图显示的相差甚远

public void testing() throws HarReaderException {
    JavascriptExecutor js1=((JavascriptExecutor)driver);
    try {
        Thread.sleep(5000);
    }catch(Exception e) {e.printStackTrace();}
    String url=driver.getCurrentUrl();
    System.out.println("Current URL :"+url);
    long pageLoadTime= (Long)js1.executeScript("return (window.performance.timing.loadEventEnd-window.performance.timing.responseStart)");
    long TTFB= (Long)js1.executeScript("return (window.performance.timing.responseStart-window.performance.timing.navigationStart)");
    long endtoendRespTime= (Long)js1.executeScript("return (window.performance.timing.loadEventEnd-window.performance.timing.navigationStart)");

    Date date = new Date();
    //Timestamp ts=new Timestamp(date.getTime());

    System.out.println("PageLoadTime Time :"+pageLoadTime);
    System.out.println("TTFB :"+TTFB);
    System.out.println("Customer perceived Time :"+endtoendRespTime);
    System.out.println("timeStamp");
    String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;";
    String netData = ((JavascriptExecutor)driver).executeScript(scriptToExecute).toString();
    System.out.println("Net data: " + netData);
    String anotherScript = "return performance\n" +
            "  .getEntriesByType(\"resource\")\n" +
            "  .map((x) => x.transferSize)\n" +
            "  .reduce((a, b) => (a + b), 0);";    //I have tried encodedSize here as well, still gives different results
    System.out.println("THIS IS HOPEFULLY THE TOTAL TRANSFER SIZE " + js1.executeScript((anotherScript)).toString());
    int totalBytes = 0;
    for (LogEntry entry : driver.manage().logs().get(LogType.PERFORMANCE)) {
        if (entry.getMessage().contains("Network.dataReceived")) {
            Matcher dataLengthMatcher = Pattern.compile("dataLength\":(.*?),").matcher(entry.getMessage());  //I tried encodedLength and other methods but always get different results from the actual page
            dataLengthMatcher.find();
            totalBytes = totalBytes + Integer.parseInt(dataLengthMatcher.group(1));
            //Do whatever you want with the data here.
        }
    }
    System.out.println(totalBytes);
}

设置 selenium Chrome 驱动,启用性能日志和 mobbrowser 代理:

@BeforeTest
public void setUp() {

    // start the proxy
    proxy = new BrowserMobProxyServer();
    proxy.start(0);

    //get the Selenium proxy object - org.openqa.selenium.Proxy;
    Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

    // configure it as a desired capability
    DesiredCapabilities capabilities = new DesiredCapabilities().chrome();
    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
    capabilities.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);

    capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--incognito");
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    //set chromedriver system property
    System.setProperty("webdriver.chrome.driver", driverPath);
    driver = new ChromeDriver(capabilities);
    // enable more detailed HAR capture, if desired (see CaptureType for the complete list)
    proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);



}

我用来分析页面的方法: 这个方法应该在 chrome 检查器中显示加载时间,但它总是显示一个较小的数字(我认为它显示的是收到的最后一个响应的时间,而不是 DOMContentLoaded 或加载时间)

public double calculatePageLoadTime(String filename) throws HarReaderException {
    HarReader harReader = new HarReader();
    de.sstoehr.harreader.model.Har har = harReader.readFromFile(new File(filename));

    HarLog log = har.getLog();
    // Access all pages elements as an object

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

    // Access all entries elements as an object

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

    long loadTime = 0;

    int entryIndex = 0;
    //Output "response" code of entries.
    for (HarEntry entry : hentry)
    {


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

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

        entryIndex++;
    }

    long loadTimeSpan = loadTime - startTime;

    Double webLoadTime = ((double)loadTimeSpan) / 1000;
    double webLoadTimeInSeconds = Math.round(webLoadTime * 100.0) / 100.0;

    return webLoadTimeInSeconds;
}

我通过从页面读取 HAR 文件来获取请求总数,但由于某种原因,它总是比实际少 10%:

    public int getNumberRequests(String filename) throws HarReaderException {
    HarReader harReader = new HarReader();
    de.sstoehr.harreader.model.Har har = harReader.readFromFile(new File(filename));

    HarLog log = har.getLog();
    return log.getEntries().size();
}

在 google 上对此进行测试,每种方法的结果都大不相同,通常与正确数字相差 10-200%。

为什么会这样?有没有一种简单的方法可以从 Chrome 或任何使这更容易的库中正确获取这些指标?我的任务是自动对数千页进行性能分析。

【问题讨论】:

  • 注意:我已经尝试过禁用缓存并在隐身模式下启动 chrome;这没有帮助。
  • discussion 对您有帮助吗?
  • 没有帮助 - 同样不准确的结果

标签: java selenium google-chrome performance-testing


【解决方案1】:

我个人在我的系统上一遍又一遍地分析了这个,然后想出了这个 -

  1. 它当前显示的资源大小是触发页面加载事件之前获取的资源量。

  2. 所以要克服这个问题,您需要在页面加载事件之后捕获资源大小变量,直到它稳定。然后它将匹配实际的控制台值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多