Add ability to ignore process prewarming for launch time benchmark
[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         self.allow_prewarm = True
40
41     def run_iteration(self):
42         tabs_to_open = 1 if self.allow_prewarm else 2
43         self.stop_time = None
44         for _ in range(tabs_to_open - 1):
45             self.open_tab(blank=True)
46         self.start_time = time.time() * 1000
47         self.open_tab()
48         while self.stop_time is None:
49             self.stop_signal_was_received.wait()
50         result = self.stop_time - self.start_time
51         self.stop_signal_was_received.clear()
52         for _ in range(tabs_to_open):
53             self.close_tab()
54
55         return result
56
57     def group_init(self):
58         self.launch_browser()
59
60     def will_parse_arguments(self):
61         self.argument_parser.add_argument('-g', '--groups', type=int,
62             help='number of groups of iterations to run (default: {})'.format(self.iteration_groups))
63         self.argument_parser.add_argument('-w', '--wait-time', type=self._parse_wait_time,
64             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))
65         self.argument_parser.add_argument('--no-prewarm', action='store_true',
66             help='attempt to ignore process prewarming (will most likely raise standard deviation)')
67
68     def did_parse_arguments(self, args):
69         if args.groups:
70             self.iteration_groups = args.groups
71         if args.wait_time:
72             self.wait_time_low, self.wait_time_high = args.wait_time
73         if args.no_prewarm:
74             self.allow_prewarm = False
75
76     @staticmethod
77     def ResponseHandler(new_tab_benchmark):
78         class Handler(DefaultLaunchTimeHandler):
79             def get_test_page(self):
80                 return '''<!DOCTYPE html>
81                 <html>
82                   <head>
83                     <title>New Tab Benchmark</title>
84                     <meta http-equiv="Content-Type" content="text/html" />
85                     <script>
86                         function sendDone() {
87                             const time = performance.timing.navigationStart
88                             const request = new XMLHttpRequest();
89                             request.open("POST", "done", false);
90                             request.setRequestHeader('Content-Type', 'application/json');
91                             request.send(JSON.stringify(time));
92                         }
93                         window.onload = sendDone;
94                     </script>
95                   </head>
96                   <body>
97                     <h1>New Tab Benchmark</h1>
98                   </body>
99                 </html>
100                 '''
101
102             def on_receive_stop_signal(self, data):
103                 new_tab_benchmark.stop_time = float(data)
104                 new_tab_benchmark.stop_signal_was_received.set()
105
106         return Handler
107
108
109 if __name__ == '__main__':
110     NewTabBenchmark().run()