Add benchmark for WebKit process launch times
[WebKit-https.git] / PerformanceTests / LaunchTime / new_tab.py
1 #! /usr/bin/env python
2 import argparse
3 import time
4 from threading import Event
5
6 from launch_time import LaunchTimeBenchmark, DefaultLaunchTimeHandler
7 import feedback_server
8
9
10 class NewTabBenchmark(LaunchTimeBenchmark):
11     def _parse_wait_time(self, string):
12         values = string.split(':')
13
14         start = None
15         end = None
16         try:
17             if len(values) == 2:
18                 start = float(values[0])
19                 end = float(values[1])
20                 if start > end:
21                     raise
22             elif len(values) == 1:
23                 start = float(values[0])
24                 end = start
25             else:
26                 raise
27         except:
28             raise argparse.ArgumentTypeError(
29                 "'" + string + "' is not a range of numbers. Expected form is N:M where N < M")
30
31         return start, end
32
33     def initialize(self):
34         self.benchmark_description = "Measure time to open a new tab for a given browser."
35         self.response_handler = NewTabBenchmark.ResponseHandler(self)
36         self.start_time = None
37         self.stop_time = None
38         self.stop_signal_was_received = Event()
39
40     def run_iteration(self):
41         self.start_time = time.time() * 1000
42         self.open_tab()
43         while self.stop_time is None:
44             self.stop_signal_was_received.wait()
45         result = self.stop_time - self.start_time
46         self.stop_time = None
47         self.stop_signal_was_received.clear()
48         self.close_tab()
49         return result
50
51     def group_init(self):
52         self.launch_browser()
53
54     def will_parse_arguments(self):
55         self.argument_parser.add_argument('-g', '--groups', type=int,
56             help='number of groups of iterations to run (default: {})'.format(self.iteration_groups))
57         self.argument_parser.add_argument('-w', '--wait-time', type=self._parse_wait_time,
58             help='wait time to use between iterations or range to scan (format is "N" or "N:M" where N < M, default: {}:{})'.format(self.wait_time_low, self.wait_time_high))
59
60     def did_parse_arguments(self, args):
61         if args.groups:
62             self.iteration_groups = args.groups
63         if args.wait_time:
64             self.wait_time_low, self.wait_time_high = args.wait_time
65
66     @staticmethod
67     def ResponseHandler(new_tab_benchmark):
68         class Handler(DefaultLaunchTimeHandler):
69             def get_test_page(self):
70                 return '''<!DOCTYPE html>
71                 <html>
72                   <head>
73                     <title>New Tab Benchmark</title>
74                     <meta http-equiv="Content-Type" content="text/html" />
75                     <script>
76                         function sendDone() {
77                             var time = performance.timing.navigationStart
78                             var request = new XMLHttpRequest();
79                             request.open("POST", "done", false);
80                             request.setRequestHeader('Content-Type', 'application/json');
81                             request.send(JSON.stringify(time));
82                         }
83                         window.onload = sendDone;
84                     </script>
85                   </head>
86                   <body>
87                     <h1>New Tab Benchmark</h1>
88                   </body>
89                 </html>
90                 '''
91
92             def on_receive_stop_time(self, stop_time):
93                 new_tab_benchmark.stop_time = stop_time
94                 new_tab_benchmark.stop_signal_was_received.set()
95
96         return Handler
97
98
99 if __name__ == '__main__':
100     NewTabBenchmark().run()