Add "-o/--output" option to startup.py and new_tab.py benchmark scripts to save the...
[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     def get_test_name(self):
28         return "StartupBenchmark"
29
30     @staticmethod
31     def ResponseHandler(startup_benchmark):
32         class Handler(DefaultLaunchTimeHandler):
33             def get_test_page(self):
34                 return '''<!DOCTYPE html>
35                 <html>
36                   <head>
37                     <title>Startup Benchmark</title>
38                     <meta http-equiv="Content-Type" content="text/html" />
39                     <script>
40                         function sendDone() {
41                             const time = Date.now();
42                             const request = new XMLHttpRequest();
43                             request.open("POST", "done", false);
44                             request.setRequestHeader('Content-Type', 'application/json');
45                             request.send(JSON.stringify(time));
46                         }
47                         window.onload = sendDone;
48                     </script>
49                   </head>
50                   <body>
51                     <h1>Startup Benchmark</h1>
52                   </body>
53                 </html>
54                 '''
55
56             def on_receive_stop_signal(self, data):
57                 startup_benchmark.stop_time = float(data)
58                 startup_benchmark.stop_signal_was_received.set()
59
60         return Handler
61
62
63 if __name__ == '__main__':
64     StartupBenchmark().run()