Add '__init__.py' and fix known bug in benchmark runner.
[WebKit-https.git] / Tools / Scripts / webkitpy / benchmark_runner / benchmark_runner.py
1 #!/usr/bin/env python
2
3 import json
4 import logging
5 import shutil
6 import signal
7 import subprocess
8 import tempfile
9 import time
10 import types
11 import os
12 import urlparse
13
14 from benchmark_builder.benchmark_builder_factory import BenchmarkBuilderFactory
15 from browser_driver.browser_driver_factory import BrowserDriverFactory
16 from http_server_driver.http_server_driver_factory import HTTPServerDriverFactory
17 from utils import loadModule, getPathFromProjectRoot
18 from utils import timeout
19
20
21 _log = logging.getLogger(__name__)
22
23
24 class BenchmarkRunner(object):
25
26     def __init__(self, planFile, buildDir, outputFile, platform, browser):
27         _log.info('Initializing benchmark running')
28         try:
29             with open(planFile, 'r') as fp:
30                 self.plan = json.load(fp)
31                 self.browserDriver = BrowserDriverFactory.create([platform, browser])
32                 self.httpServerDriver = HTTPServerDriverFactory.create([self.plan['http_server_driver']])
33                 self.benchmarks = self.plan['benchmarks']
34                 self.buildDir = os.path.abspath(buildDir)
35                 self.outputFile = outputFile
36         except IOError:
37             _log.error('Can not open plan file: %s' % planFile)
38         except ValueError:
39             _log.error('Plan file:%s may not follow json format' % planFile)
40         except:
41             raise
42
43     def execute(self):
44         _log.info('Start to execute the plan')
45         for benchmark in self.benchmarks:
46             _log.info('Start a new benchmark')
47             results = []
48             benchmarkBuilder = BenchmarkBuilderFactory.create([benchmark['benchmark_builder']])
49             webRoot = benchmarkBuilder.prepare(benchmark['original_benchmark'], benchmark['benchmark_patch'] if 'benchmark_patch' in benchmark else None)
50             for x in xrange(int(benchmark['count'])):
51                 _log.info('Start the iteration %d of current benchmark' % (x + 1))
52                 self.httpServerDriver.serve(webRoot)
53                 self.browserDriver.prepareEnv()
54                 self.browserDriver.launchUrl(urlparse.urljoin(self.httpServerDriver.baseUrl(), benchmark['entry_point']), self.buildDir)
55                 try:
56                     with timeout(benchmark['timeout']):
57                         result = json.loads(self.httpServerDriver.fetchResult())
58                         assert(result)
59                         results.append(result)
60                 except:
61                     _log.error('No result. Something went wrong. Will skip current benchmark.')
62                     self.browserDriver.closeBrowsers()
63                     break
64                 finally:
65                     self.browserDriver.closeBrowsers()
66                     _log.info('End of %d iteration of current benchmark' % (x + 1))
67             results = self.wrap(results)
68             self.dump(results, self.outputFile if self.outputFile else benchmark['output_file'])
69             benchmarkBuilder.clean()
70
71     @classmethod
72     def dump(cls, results, outputFile):
73         _log.info('Dumpping the results to file')
74         try:
75             with open(outputFile, 'w') as fp:
76                 json.dump(results, fp)
77         except IOError:
78             _log.error('Cannot open output file: %s' % outputFile)
79             _log.error('Results are:\n %s', json.dumps(results))
80
81     @classmethod
82     def wrap(cls, dicts):
83         if not dicts:
84             return None
85         ret = {}
86         for dic in dicts:
87             ret = cls.merge(ret, dic)
88         return ret
89
90     @classmethod
91     def merge(cls, a, b):
92         assert(isinstance(a, type(b)))
93         argType = type(a)
94         # special handle for list type, and should be handle before equal check
95         if argType == types.ListType:
96             return a + b
97         if a == b:
98             return a
99         if argType == types.DictType:
100             result = {}
101             for key, value in a.items():
102                 if key in b:
103                     result[key] = cls.merge(value, b[key])
104                 else:
105                     result[key] = value
106             for key, value in b.items():
107                 if key not in result:
108                     result[key] = value
109             return result
110         # for other types
111         return a + b