Add benchmark for WebKit process launch times
[WebKit-https.git] / PerformanceTests / LaunchTime / startup.py
1 #! /usr/bin/env python
2 import time
3 from threading import Event
4
5 from launch_time import LaunchTimeBenchmark, DefaultLaunchTimeHandler
6
7
8 class StartupBenchmark(LaunchTimeBenchmark):
9     def initialize(self):
10         self.benchmark_description = 'Measure startup time of a given browser.'
11         self.response_handler = StartupBenchmark.ResponseHandler(self)
12         self.start_time = None
13         self.stop_time = None
14         self.stop_signal_was_received = Event()
15
16     def run_iteration(self):
17         self.start_time = time.time() * 1000
18         self.open_tab()
19         while self.stop_time is None:
20             self.stop_signal_was_received.wait()
21         result = self.stop_time - self.start_time
22         self.stop_time = None
23         self.stop_signal_was_received.clear()
24         self.quit_browser()
25         return result
26
27     @staticmethod
28     def ResponseHandler(startup_benchmark):
29         class Handler(DefaultLaunchTimeHandler):
30             def get_test_page(self):
31                 return '''<!DOCTYPE html>
32                 <html>
33                   <head>
34                     <title>Startup Benchmark</title>
35                     <meta http-equiv="Content-Type" content="text/html" />
36                     <script>
37                         function sendDone() {
38                             const time = Date.now();
39                             const request = new XMLHttpRequest();
40                             request.open("POST", "done", false);
41                             request.setRequestHeader('Content-Type', 'application/json');
42                             request.send(JSON.stringify(time));
43                         }
44                         window.onload = sendDone;
45                     </script>
46                   </head>
47                   <body>
48                     <h1>Startup Benchmark</h1>
49                   </body>
50                 </html>
51                 '''
52
53             def on_receive_stop_time(self, stop_time):
54                 startup_benchmark.stop_time = stop_time
55                 startup_benchmark.stop_signal_was_received.set()
56
57         return Handler
58
59
60 if __name__ == '__main__':
61     StartupBenchmark().run()