161b665f992623951c69fc497df0d96da7439582
[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
8 class NewTabBenchmark(LaunchTimeBenchmark):
9     def _parse_wait_time(self, string):
10         values = string.split(':')
11
12         start = None
13         end = None
14         try:
15             if len(values) == 2:
16                 start = float(values[0])
17                 end = float(values[1])
18                 if start > end:
19                     raise
20             elif len(values) == 1:
21                 start = float(values[0])
22                 end = start
23             else:
24                 raise
25         except:
26             raise argparse.ArgumentTypeError(
27                 "'" + string + "' is not a range of numbers. Expected form is N:M where N < M")
28
29         return start, end
30
31     def initialize(self):
32         self.benchmark_description = "Measure time to open a new tab for a given browser."
33         self.response_handler = NewTabBenchmark.ResponseHandler(self)
34         self.start_time = None
35         self.stop_time = None
36         self.stop_signal_was_received = Event()
37         self.allow_prewarm = True
38
39     def run_iteration(self):
40         tabs_to_open = 1 if self.allow_prewarm else 2
41         self.stop_time = None
42         for _ in range(tabs_to_open - 1):
43             self.open_tab(blank=True)
44         self.start_time = time.time() * 1000
45         self.open_tab()
46         while self.stop_time is None:
47             self.stop_signal_was_received.wait()
48         result = self.stop_time - self.start_time
49         self.stop_signal_was_received.clear()
50         for _ in range(tabs_to_open):
51             self.close_tab()
52
53         return result
54
55     def group_init(self):
56         self.launch_browser()
57
58     def will_parse_arguments(self):
59         self.argument_parser.add_argument('-g', '--groups', type=int,
60             help='number of groups of iterations to run (default: {})'.format(self.iteration_groups))
61         self.argument_parser.add_argument('-w', '--wait-time', type=self._parse_wait_time,
62             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))
63         self.argument_parser.add_argument('--no-prewarm', action='store_true',
64             help='attempt to ignore process prewarming (will most likely raise standard deviation)')
65
66     def did_parse_arguments(self, args):
67         if args.groups:
68             self.iteration_groups = args.groups
69         if args.wait_time:
70             self.wait_time_low, self.wait_time_high = args.wait_time
71         if args.no_prewarm:
72             self.allow_prewarm = False
73
74     @staticmethod
75     def ResponseHandler(new_tab_benchmark):
76         class Handler(DefaultLaunchTimeHandler):
77             def get_test_page(self):
78                 return '''<!DOCTYPE html>
79                 <html>
80                   <head>
81                     <title>New Tab Benchmark</title>
82                     <meta http-equiv="Content-Type" content="text/html" />
83                     <script>
84                         function sendDone() {
85                             const time = performance.timing.navigationStart
86                             const request = new XMLHttpRequest();
87                             request.open("POST", "done", false);
88                             request.setRequestHeader('Content-Type', 'application/json');
89                             request.send(JSON.stringify(time));
90                         }
91                         window.onload = sendDone;
92                     </script>
93                   </head>
94                   <body>
95                     <h1>New Tab Benchmark</h1>
96                   </body>
97                 </html>
98                 '''
99
100             def on_receive_stop_signal(self, data):
101                 new_tab_benchmark.stop_time = float(data)
102                 new_tab_benchmark.stop_signal_was_received.set()
103
104         return Handler
105
106
107 if __name__ == '__main__':
108     NewTabBenchmark().run()