Discard cached processes when clearing website data store
[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 get_test_name(self):
59         return "NewTabBenchmark"
60
61     def will_parse_arguments(self):
62         self.argument_parser.add_argument('-g', '--groups', type=int,
63             help='number of groups of iterations to run (default: {})'.format(self.iteration_groups))
64         self.argument_parser.add_argument('-w', '--wait-time', type=self._parse_wait_time,
65             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))
66         self.argument_parser.add_argument('--no-prewarm', action='store_true',
67             help='attempt to ignore process prewarming (will most likely raise standard deviation)')
68
69     def did_parse_arguments(self, args):
70         if args.groups:
71             self.iteration_groups = args.groups
72         if args.wait_time:
73             self.wait_time_low, self.wait_time_high = args.wait_time
74         if args.no_prewarm:
75             self.allow_prewarm = False
76
77     @staticmethod
78     def ResponseHandler(new_tab_benchmark):
79         class Handler(DefaultLaunchTimeHandler):
80             def get_test_page(self):
81                 return '''<!DOCTYPE html>
82                 <html>
83                   <head>
84                     <title>New Tab Benchmark</title>
85                     <meta http-equiv="Content-Type" content="text/html" />
86                     <script>
87                         function sendDone() {
88                             const time = performance.timing.navigationStart
89                             const request = new XMLHttpRequest();
90                             request.open("POST", "done", false);
91                             request.setRequestHeader('Content-Type', 'application/json');
92                             request.send(JSON.stringify(time));
93                         }
94                         window.onload = sendDone;
95                     </script>
96                   </head>
97                   <body>
98                     <h1>New Tab Benchmark</h1>
99                   </body>
100                 </html>
101                 '''
102
103             def on_receive_stop_signal(self, data):
104                 new_tab_benchmark.stop_time = float(data)
105                 new_tab_benchmark.stop_signal_was_received.set()
106
107         return Handler
108
109
110 if __name__ == '__main__':
111     NewTabBenchmark().run()