wiki:Performance Tests

Version 10 (modified by rniwa@webkit.org, 9 years ago) (diff)

Updated Writing a Performance Test Using runner.js.

What is a Performance Test

A performance test measures the run-time performance and memory usage of WebKit. Unlike regression tests (a.k.a layout tests) or conformance tests such as of W3C's, it doesn't necessarily test the correctness of WebKit features. Since the run-time and memory used by each run of the same test may vary, we can't conclude whether a given performance test passed or failed by just looking at a single run. For this reason, performance tests yields "values" such as the time taken to run the test instead of simple PASS and FAIL.

Performance Test Results

We have continuous performance test bots on build.webkit.org. You can see test results submitted by these bots on http://perf.webkit.org/

The waterfall of the Performance bots on the Buildbot page Platform name on the results page
Apple Mavericks Release (Perf) mac-mavericks
Apple MountainLion Release (Perf) mac-mountainlion
EFL Linux 64-bit Release WK2 (Perf) efl
GTK Linux 64-bit Release (Perf) gtk

How to Run Performance Tests

WebKit's performance tests can be run by run-perf-tests. Specify a list of directories or performance tests to run a subset. e.g. run-perf-tests PerformanceTests/DOM or run-perf-tests DOM will only run tests in http://trac.webkit.org/browser/trunk/PerformanceTests/DOM. It will automatically build DumpRenderTree and WebKitTestRunner as needed just like run-webkit-tests.

Reducing noise on your machine

Before running performance tests, you may want to reboot your machine, disable screen savers and power saving features, and turn off anti-virus software to reduce the potential noise. Also disable network, bluetooth, and other network and wireless devices as they might cause undesirable CPU interrupts and context switches.

On Mac, you can run the following command to disable Spotlight:

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

(To re-enable, run the same command with load in place of unload)

Aggregating and Comparing Results

If you're running a performance test locally in order to verify your patch doesn't regress or improves the performance of WebKit, you may find --output-json-path useful. Specify a file path such as perf-test.json and run-perf-tests will automatically store the results in the JSON file and creates perf-test.html that visualizes the test results. Execute run-perf-tests multiple times with the same output JSON path and it will automatically aggregate results in the JSON and the corresponding HTML document.

Suppose we have two WebKit checkouts: one without a patch and another one with the patch applied. By executing run-perf-tests --output-json-path=/Users/WebKitten/perf-test.json in both checkouts, we can easily compare the test results from two runs by opening ~/perf-test.html.

You can also specify a build directory as follows along with the output JSON path:

run-perf-tests --no-build --build-directory /Users/WebKitten/MyCustomBuild/ --output-json-path=/Users/WebKitten/perf-test.json

This allows you to compare results from different builds without having to locally build DumpRenderTree or WebKitTestRunner.

Bisecting regressions

Suppose you're bisecting a regression for a performance regression on Bindings/node-list-access.html as seen here. Looking at the graph we see that the culprit lies between r124567 and r124582.

To bisect this regression, I create two WebKit checkouts one synced to r124567 and another synced to r124582, and run the following commands in each checkout:

svn up PerformanceTests
svn up Tools/Scripts/webkitpy/performance_tests
Tools/Scripts/build-webkit
Tools/Scripts/run-perf-tests --output-json-path=/Users/WebKitten/Desktop/node-list-access.json PerformanceTests/Bindings/node-list-access.html

This step automatically produces /Users/WebKitten/Desktop/node-list-access.html for me to compare the results, each results labeled r124567 and r124582 (you can use --description option to annotate the results further) and I can confirm whether the regression reproduces locally or not. Sometimes, regression doesn't produce on your local machine due to differences in environment such as compilers used, memory size, and CPU speed.

Once I confirmed that the regression is reproducible on my machine, I can start bisecting builds. Here, I sync the checkout initially synced to r124582 to a slightly older version, say, r124580 and generate results again as follows:

svn up -r 124580
svn up PerformanceTests
svn up Tools/Scripts/webkitpy/performance_tests
Tools/Scripts/build-webkit
Tools/Scripts/run-perf-tests --output-json-path=/Users/WebKitten/Desktop/node-list-access.json PerformanceTests/Bindings/node-list-access.html

I repeat this process until the results recovers to the level we had at r124567, at which I identified the culprit. I don't typically do a strict binary search on perf. regressions because that typically results to avoid rebuilding the entire WebKit all the time.

Writing a Performance Test Using runner.js

The easiest way to write a performance test is using runner.js, which provides PerfTestRunner with various utility functions. Once you wrote a test, put it inside PerformanceTests directory to be ran by run-perf-tests and performance bots.

Measuring Runs Per Second

Our preferred method of measurement is runs (function calls) per second. With runner.js, we can measure this metric by calling PerfTestRunner.measureRunsPerSecond with a test function. PerfTestRunner.measureRunsPerSecond measures the time of times run function could be called in one second, and reports the statistics after repeating it 20 times (configurable via run-perf-tests). The statistics includes arithmetic mean, standard deviation, median, minimum, and maximum values.

For example, see Parser/tiny-innerHTML.html:

<!DOCTYPE html>
<body>
<script src="../resources/runner.js"></script>
<script>
PerfTestRunner.measureRunsPerSecond({run:function() {
    var testDiv = document.createElement("div");
    testDiv.style.display = "none";
    document.body.appendChild(testDiv);
    for (var x = 0; x < 100000; x++) {
        testDiv.innerHTML = "This is a tiny HTML document";
    }
    document.body.removeChild(testDiv);
}});
</script>
</body>

Measuring Time

In some tests, however, we cannot call the run function for an arbitrary number of times as done in measureRunsPerSecond. In those tests, we can use PerfTestRunner.measureTime to measure the time run took to execute. measureTime calls the specified function once in each iteration and runs 20 iterations by default.

Note that the runtime of a function gets smaller relative to the granularity of time measurement we can make as the WebKit's performance (or of the machines that run performance tests) improves.

Measuring Asynchronous Results

In some tests such as ones that measure fps, values are measured asynchronously. In those tests, we can use PerfTestRunner.prepareToMeasureValuesAsync and PerfTestRunner.measureValueAsync to report measured value at an arbitrary time. At the beginning of a test, call PerfTestRunner.prepareToMeasureValuesAsync with an object with unit property, which specifies the name of the unit (either one of "ms", "fps", or "runs/s"). Call PerfTestRunner.measureValueAsync as a newly measured value comes in. Once enough values are measured (20 by default), PerfTestRunner.measureValueAsync will automatically stop the test; do not expect or manually track the number of iterations in your test as this must be configurable via run-perf-tests.

For example, see Interactive/SelectAll.html:

<!DOCTYPE html>
<html>
<body>
<script src="../resources/runner.js"></script>
<script>

PerfTestRunner.prepareToMeasureValuesAsync({
    unit: 'ms',
    done: function () {
        var iframe = document.querySelector('iframe');
        iframe.parentNode.removeChild(iframe);
    }
});

function runTest() {
    var iframe = document.querySelector('iframe');
    iframe.contentWindow.getSelection().removeAllRanges();
    iframe.contentDocument.body.offsetTop;

    setTimeout(function () {
        var startTime = PerfTestRunner.now();
        iframe.contentDocument.execCommand('SelectAll');
        iframe.contentDocument.body.offsetTop;
        setTimeout(function () {
            if (!PerfTestRunner.measureValueAsync(PerfTestRunner.now() - startTime))
                return;
            PerfTestRunner.gc();
            setTimeout(runTest, 0);
        }, 0);
    }, 0);
}

</script>
<iframe src="../Parser/resources/html5.html" onload="runTest()" width="800" height="600">
</body>
</html>

Optional Arguments

measureRunsPerSecond, measureTime, and PerfTestRunner.prepareToMeasureValuesAsync described above optionally support the following arguments (as properties in the object they take):

  • setup - In measureRunsPerSecond and measureTime, this function gets called before the start of each iteration. With measureRunsPerSecond, this function gets called exactly once before run gets called many times in each iteration. If there is some work to be done before run can be called each time, use measureTime instead. PerfTestRunner.prepareToMeasureValuesAsync ignores this function as it can't know when the next iteration starts.
  • done - This function gets called once all iterations are finished.
  • description - The description of the test. run-perf-tests will print out this text.

Replay Performance Tests

Replay tests are highly "experimental" page loading tests. Historically, Apple and Google have used PLT and Page Cycler Tests but they could not be part of WebKit or publicly distributed otherwise because they contain copyrighted materials. WebKit replay tests works around this problem by measuring page loading time of web pages on Internet Archive using local caches provided by web-page-replay as a Web proxy.

A replay test consists of a single text file with an URL in it. For example, digg.com.replay contains

http://web.archive.org/web/20100730073647/http://digg.com/

as of July 27th, 2012. run-perf-tests creates digg.com.wpr and digg.com-expected.png when preparing the local cache, and creates digg.com-actual.png as it runs the test.

How to Run Replay Tests

Replay tests are currently supported on Mac port and Chromium port on Mac and Linux. To run tests, you must set the local proxy to localhost at port 8080 for HTTP and port 8443 for HTTPs. This will allow DumpRenderTree or WebKitTestRunner to talk to web-page-replay to cache pages locally instead of directly accessing archive.org. Exclude *.googlecode.com as web-page-replay needs to be downloaded from Google Code on the initial run.

  • On Mac, the proxy can be set at System Preferences > Network > Advanced > Proxies.
  • On Linux, the proxy can be set by $http_proxy, $https_proxy, $no_proxy (specifies hosts to be excluded) environmental variables.

Once the proxy is setup, run run-perf-tests --replay. Since all replay tests are located in PerformanceTests/Replay, you can only run replay tests by run-perf-tests --replay PerformanceTests/Replay. run-perf-tests will first prepares local caches using web-page-replay's record mode, and then makes 20 measurements of page load times using the play mode.

Make sure that .wpr files created for each test contain actual contents. For example, if the .wpr file is less than 100KB, it's likely that the test runner is accessing the remote servers directly and not going through web-page-replay. You can also make sure that tests are running properly by comparing the contents of digg.com-expected.png and digg.com-actual.png. Unfortunately, this image comparison cannot be automated as the image contains copyrighted material (preventing to be checked into the SVN repository) and it changes as WebKit is updated.

How to Write a Replay Test

To write a new replay test for an URL, go visit Internet Archive and look for an archive of the URL. If there is no archive for the URL, then we cannot create a replay test for this page. Also, if the archive doesn't contain a significant amount of essential non-HTML contents such as images, css, and plugins, it may not be suitable as a replay test.

Once you've obtained an archive.org URL, then create a .reply file in PerformanceTests/Replay and run run-perf-tests --replay <path to new .replay file> (don't forget to setup the proxy).

Look for any errors web-page-replay reports. For example, failures to inject script is a very common error and can be ignored in most cases. However, "pipe broken" errors and other python exceptions tend to be an indication of the content not being served properly via web-page-replay. If these errors occur, try other archives of the same URL.

When the tests finish successfully without errors, look at the mean and the standard deviation of the test. If the standard deviation is higher than 4-6% of the mean, try other archives of the same URL. It's important to recognize that different archives of the same URL can yield significantly different variances as follows:

http://web.archive.org/web/20110729050650/http://www.kp.ru/
RESULT Replay: Russian: www.kp.ru.replay= 2164.80790941 ms
median= 2173.47407341 ms, stdev= 216.239083036 ms, min= 2067.66700745 ms, max= 2248.77309799 ms

http://web.archive.org/web/20110119021944/http://www.kp.ru/
RESULT Replay: Russian: www.kp.ru.replay= 3299.88499692 ms
median= 3802.16002464 ms, stdev= 4244.51026529 ms, min= 1394.64211464 ms, max= 3824.78284836 ms

http://web.archive.org/web/20110318023959/http://kp.ru/
RESULT Replay: Russian: www.kp.ru.replay= 1667.41889401 ms
median= 1667.26398468 ms, stdev= 67.7172770899 ms, min= 1643.22805405 ms, max= 1702.38494873 ms

If python exceptions or other serious errors persist, or the ratio of standard deviation to mean is consistently higher than 7-10%, don't add the URL as a replay test regardless of how important that website is because we can't make a use of performance tests that have 10% variance.

Profiling Performance Tests

run-perf-tests --profile can be used to attach and run the default platform CPU profiler against the provided test(s).

Additionally, the --profiler=PROFILER option can select which profiler to use from the built-in profilers:

perf linux (default), chromium-android (default)
iprofiler mac (default)
sample mac
pprof mac, linux (chromium-only, requires using tcmalloc)

For perf and pprof profilers --profile provides per-test "10 hottest functions" output, which is useful for obtaining a high level overview of where the test is spending it's time. This has been surprisingly helpful for finding hot non-inlined functions, or other low-hanging fruit.

% run-perf-tests --profile
Running 113 tests
Running Animation/balls.html (1 of 113)
Finished: 3.079851 s

[ perf record: Woken up 3 times to write data ]
[ perf record: Captured and wrote 0.678 MB /src/WebKit/Source/WebKit/chromium/webkit/Release/layout-test-results/test-44.data (~29642 samples) ]
     5.99%  DumpRenderTree  perf-5981.map            [.] 0x250f5ef06321  
     2.46%  DumpRenderTree  DumpRenderTree           [.] v8::internal::FastDtoa(double, v8::internal::FastDtoaMode, int, v8::internal::Vector<char>, int*, int*)
     1.86%  DumpRenderTree  DumpRenderTree           [.] WebCore::Length::initFromLength(WebCore::Length const&)
     1.74%  DumpRenderTree  DumpRenderTree           [.] WebCore::RenderStyle::diff(WebCore::RenderStyle const*, unsigned int&) const
     1.69%  DumpRenderTree  libfreetype.so.6.8.0     [.] 0x493ab         
     1.35%  DumpRenderTree  DumpRenderTree           [.] tc_free
     1.30%  DumpRenderTree  [kernel.kallsyms]        [k] 0xffffffff8103b51a
     1.27%  DumpRenderTree  DumpRenderTree           [.] tc_malloc
     1.25%  DumpRenderTree  DumpRenderTree           [.] v8::internal::JSObject::SetPropertyWithInterceptor(v8::internal::String*, v8::internal::Object*, PropertyAttributes, v8::internal::StrictModeFlag)
     1.22%  DumpRenderTree  DumpRenderTree           [.] WTF::double_conversion::Strtod(WTF::double_conversion::Vector<char const>, int)

To view the full profile, run:
perf report -i /src/WebKit/Source/WebKit/chromium/webkit/Release/layout-test-results/test-44.data

(Note: perf prints out a bunch of kernel-related warnings which I've stripped from the above output sample.)

For more in-depth analysis, all profilers print instructions after profiling as to how to explore the full sample data, as shown above.

Adding support for a new profiler

run-perf-tests --profile --profiler=PROFILER uses Profilers provided by webkitpy.common.system.profiler, except for perf on Android which is currently implemented in chromium_android.py. Profilers should conform to the Profiler class interface, and ProfilerFactory needs to know how to create one. All of this is defined in profiler.py. If you have any trouble, email eric@webkit.