Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import csv | |
| 6 import json | |
| 7 import os | |
| 8 import shutil | |
| 9 | |
| 10 import chrome_cache | |
| 11 import common_util | |
| 12 import emulation | |
| 13 import sandwich_metrics | |
| 14 import sandwich_misc | |
| 15 import sandwich_runner | |
| 16 import task_manager | |
| 17 | |
| 18 | |
| 19 def NetworkSimulationTransformer(network_condition): | |
| 20 """Creates a function that accepts a SandwichRunner as a parameter and sets | |
| 21 network emulation options on it. | |
| 22 | |
| 23 Args: | |
| 24 network_condition: The network condition to apply to the sandwich runner. | |
| 25 | |
| 26 Returns: | |
| 27 A callback transforming the SandwichRunner given in argument accordingly | |
| 28 """ | |
| 29 assert network_condition in emulation.NETWORK_CONDITIONS | |
| 30 def Transformer(runner): | |
| 31 assert isinstance(runner, sandwich_runner.SandwichRunner) | |
| 32 runner.network_condition = network_condition | |
| 33 return Transformer | |
| 34 | |
| 35 | |
| 36 class SandwichTaskBuilder(task_manager.Builder): | |
| 37 """A builder for a graph of tasks, each prepares or invokes a SandwichRunner. | |
| 38 """ | |
| 39 | |
| 40 def __init__(self, output_directory, job_path, url_repeat): | |
| 41 """Constructor. | |
| 42 | |
| 43 Args: | |
| 44 output_directory: As in task_manager.Builder.__init__ | |
| 45 job_path: Path of the sandwich's job. | |
| 46 url_repeat: Non null integer controlling how many times the URLs should be | |
| 47 repeated in the benchmarks. | |
| 48 """ | |
| 49 task_manager.Builder.__init__(self, output_directory) | |
| 50 self._job_path = job_path | |
| 51 self._url_repeat = url_repeat | |
| 52 self._default_final_tasks = [] | |
| 53 | |
| 54 self._original_wpr_task = None | |
| 55 self._patched_wpr_task = None | |
| 56 self._reference_cache_task = None | |
| 57 self._subresources_for_urls_run_task = None | |
| 58 self._subresources_for_urls_task = None | |
| 59 | |
| 60 @property | |
| 61 def default_final_tasks(self): | |
| 62 return self._default_final_tasks | |
| 63 | |
| 64 def _CreateSandwichRunner(self): | |
| 65 """Create a runner for non benchmark purposes.""" | |
| 66 runner = sandwich_runner.SandwichRunner() | |
| 67 runner.LoadJob(self._job_path) | |
| 68 return runner | |
| 69 | |
| 70 def OverridePathToWprArchive(self, original_wpr_path): | |
| 71 """Sets the original WPR archive path's to be used. | |
| 72 | |
| 73 Args: | |
| 74 original_wpr_path: Path of the original WPR archive to be used. | |
| 75 """ | |
| 76 self._original_wpr_task = \ | |
| 77 self.CreateStaticTask('common/webpages.wpr', original_wpr_path) | |
| 78 | |
| 79 def PopulateWprRecordingTask(self): | |
| 80 """Records the original WPR archive.""" | |
| 81 @self.RegisterTask('common/webpages.wpr') | |
| 82 def BuildOriginalWpr(): | |
| 83 common_util.EnsureParentDirectoryExists(BuildOriginalWpr.path) | |
| 84 runner = self._CreateSandwichRunner() | |
| 85 runner.wpr_archive_path = BuildOriginalWpr.path | |
| 86 runner.wpr_record = True | |
| 87 runner.Run() | |
| 88 | |
| 89 self._original_wpr_task = BuildOriginalWpr | |
| 90 | |
| 91 def PopulateCommonPipelines(self): | |
| 92 """Creates necessary tasks to produce initial cache archive. | |
| 93 | |
| 94 Also creates a task for producing a json file with a mapping of URLs to | |
| 95 subresources (urls-resources.json). | |
| 96 | |
| 97 Here is the full dependency tree for the returned task: | |
| 98 common/cache-ref-validation.log | |
| 99 depends on: common/cache-ref.zip | |
| 100 depends on: common/webpages-patched.wpr | |
| 101 depends on: common/webpages.wpr | |
| 102 depends on: common/urls-resources.json | |
| 103 depends on: common/urls-resources-run/ | |
| 104 depends on: common/webpages.wpr | |
| 105 | |
| 106 Returns: | |
| 107 The last task of the pipeline. | |
| 108 """ | |
| 109 @self.RegisterTask('common/webpages-patched.wpr', [self._original_wpr_task]) | |
| 110 def BuildPatchedWpr(): | |
| 111 common_util.EnsureParentDirectoryExists(BuildPatchedWpr.path) | |
| 112 shutil.copyfile(self._original_wpr_task.path, BuildPatchedWpr.path) | |
| 113 sandwich_misc.PatchWpr(BuildPatchedWpr.path) | |
| 114 | |
| 115 @self.RegisterTask('common/cache-ref.zip', [BuildPatchedWpr]) | |
| 116 def BuildReferenceCache(): | |
| 117 runner = self._CreateSandwichRunner() | |
| 118 runner.wpr_archive_path = BuildPatchedWpr.path | |
| 119 runner.cache_archive_path = BuildReferenceCache.path | |
| 120 runner.cache_operation = 'save' | |
| 121 runner.Run() | |
| 122 | |
| 123 @self.RegisterTask('common/subresources-for-urls-run/', | |
| 124 dependencies=[self._original_wpr_task]) | |
| 125 def UrlsResourcesRun(): | |
| 126 runner = self._CreateSandwichRunner() | |
| 127 runner.wpr_archive_path = self._original_wpr_task.path | |
| 128 runner.cache_operation = 'clear' | |
| 129 runner.trace_output_directory = UrlsResourcesRun.path | |
| 130 runner.Run() | |
| 131 | |
| 132 @self.RegisterTask('common/subresources-for-urls.json', [UrlsResourcesRun]) | |
| 133 def ListUrlsResources(): | |
| 134 json_content = sandwich_misc.ListResourceUrls(UrlsResourcesRun.path) | |
| 135 with open(ListUrlsResources.path, 'w') as output: | |
| 136 json.dump(json_content, output) | |
| 137 | |
| 138 @self.RegisterTask('common/cache-ref-validation.log', | |
| 139 [BuildReferenceCache, ListUrlsResources]) | |
| 140 def ValidateReferenceCache(): | |
| 141 json_content = json.load(open(ListUrlsResources.path)) | |
| 142 ref_urls = set() | |
| 143 for urls in json_content.values(): | |
| 144 ref_urls.update(set(urls)) | |
| 145 sandwich_misc.ValidateCacheArchiveContent( | |
| 146 ref_urls, BuildReferenceCache.path) | |
| 147 | |
| 148 self._patched_wpr_task = BuildPatchedWpr | |
| 149 self._reference_cache_task = BuildReferenceCache | |
| 150 self._subresources_for_urls_run_task = UrlsResourcesRun | |
| 151 self._subresources_for_urls_task = ListUrlsResources | |
| 152 | |
| 153 self._default_final_tasks.append(ValidateReferenceCache) | |
| 154 return ValidateReferenceCache | |
| 155 | |
| 156 def PopulateLoadBenchmark(self, subresource_discoverer, | |
| 157 runner_transformer_name, runner_transformer): | |
| 158 """Populate the a benchmark's pipeline from it's setup tasks. | |
| 159 | |
| 160 Args: | |
| 161 subresource_discoverer: Name of a sub-resources discoverer. | |
| 162 runner_transformer: A closure that would be applied once to SandwichRunner | |
|
pasko
2016/04/20 18:57:22
This is not a closure.
"A function that takes an
gabadie
2016/04/21 09:32:46
I insist on the closure. A function is a closure b
pasko
2016/04/21 18:21:44
As discussed offline, 'closure' can be confusing f
gabadie
2016/04/22 14:16:42
Done.
| |
| 163 before it is run. | |
|
pasko
2016/04/20 18:57:22
4 space indent for arg descriptions
gabadie
2016/04/21 09:32:46
Done.
| |
| 164 runner_transformer_name: Name of the runner transformer used to generate | |
| 165 task names. | |
| 166 benchmark_name: The benchmark's name for that runner modifier. | |
| 167 | |
| 168 Returns: | |
| 169 The last task_manager.Task of the pipeline. | |
|
pasko
2016/04/20 18:57:22
Plz add the dependency tree for the returned task
gabadie
2016/04/21 09:32:46
Done.
| |
| 170 """ | |
| 171 assert subresource_discoverer in sandwich_misc.SUBRESOURCE_DISCOVERERS | |
| 172 assert 'shared' not in sandwich_misc.SUBRESOURCE_DISCOVERERS | |
|
pasko
2016/04/20 18:57:22
we have 'shared' and 'common' as directories, whic
gabadie
2016/04/21 09:32:46
Merged shared into common.
| |
| 173 shared_task_prefix = os.path.join('shared', subresource_discoverer) | |
| 174 task_prefix = os.path.join(runner_transformer_name, | |
| 175 subresource_discoverer) | |
|
pasko
2016/04/20 18:57:22
fits on one line
gabadie
2016/04/21 09:32:46
My bad. Done.
| |
| 176 | |
| 177 @self.RegisterTask(shared_task_prefix + '-setup.json', merge=True, | |
|
pasko
2016/04/20 18:57:22
Making sure that merge=True is not misused is comp
gabadie
2016/04/21 09:32:46
As agreed offline, let's post pone this in a separ
pasko
2016/04/21 18:21:44
Acknowledged.
| |
| 178 dependencies=[self._subresources_for_urls_task]) | |
| 179 def SetupBenchmark(): | |
| 180 trace_path = os.path.join(self._subresources_for_urls_run_task.path, '0', | |
| 181 sandwich_runner.TRACE_FILENAME) | |
| 182 whitelisted_urls = sandwich_misc.ExtractDiscoverableUrls( | |
| 183 trace_path, subresource_discoverer) | |
| 184 | |
| 185 urls_resources = json.load(open(self._subresources_for_urls_task.path)) | |
| 186 assert len(urls_resources) == 1, \ | |
| 187 "This recipe is not ready for multiple urls." | |
|
pasko
2016/04/20 18:57:22
no need in this explanation, the assert is enough.
gabadie
2016/04/21 09:32:46
Done.
pasko
2016/04/21 18:21:44
Really done?
gabadie
2016/04/22 14:16:42
My bad.
| |
| 188 url = urls_resources.keys()[0] | |
| 189 url_resources = urls_resources[url] | |
| 190 common_util.EnsureParentDirectoryExists(SetupBenchmark.path) | |
| 191 with open(SetupBenchmark.path, 'w') as output: | |
| 192 json.dump({ | |
| 193 'cache_whitelist': [url for url in whitelisted_urls], | |
| 194 'url_resources': url_resources, | |
| 195 }, output) | |
| 196 | |
| 197 @self.RegisterTask(shared_task_prefix + '-cache.zip', merge=True, | |
| 198 dependencies=[ | |
| 199 SetupBenchmark, self._reference_cache_task]) | |
| 200 def BuildBenchmarkCacheArchive(): | |
| 201 setup = json.load(open(SetupBenchmark.path)) | |
| 202 chrome_cache.ApplyUrlWhitelistToCacheArchive( | |
| 203 cache_archive_path=self._reference_cache_task.path, | |
| 204 whitelisted_urls=setup['cache_whitelist'], | |
| 205 output_cache_archive_path=BuildBenchmarkCacheArchive.path) | |
| 206 | |
| 207 @self.RegisterTask(task_prefix + '-run/', | |
| 208 dependencies=[BuildBenchmarkCacheArchive]) | |
| 209 def RunBenchmark(): | |
| 210 runner = self._CreateSandwichRunner() | |
| 211 # runner.record_video = True | |
| 212 runner.job_repeat = self._url_repeat | |
| 213 runner_transformer(runner) | |
| 214 runner.wpr_archive_path = self._patched_wpr_task.path | |
| 215 runner.wpr_out_log_path = os.path.join(RunBenchmark.path, 'wpr.log') | |
| 216 runner.cache_archive_path = BuildBenchmarkCacheArchive.path | |
| 217 runner.cache_operation = 'push' | |
| 218 runner.trace_output_directory = RunBenchmark.path | |
| 219 runner.Run() | |
| 220 | |
| 221 @self.RegisterTask(task_prefix + '-metrics.csv', | |
| 222 dependencies=[RunBenchmark]) | |
| 223 def ExtractMetrics(): | |
| 224 sandwich_misc.VerifyBenchmarkOutputDirectory( | |
| 225 SetupBenchmark.path, RunBenchmark.path) | |
| 226 trace_metrics_list = sandwich_metrics.PullMetricsFromOutputDirectory( | |
| 227 RunBenchmark.path) | |
| 228 trace_metrics_list.sort(key=lambda e: e['id']) | |
| 229 with open(ExtractMetrics.path, 'w') as csv_file: | |
| 230 writer = csv.DictWriter(csv_file, | |
| 231 fieldnames=sandwich_metrics.CSV_FIELD_NAMES) | |
| 232 writer.writeheader() | |
| 233 for trace_metrics in trace_metrics_list: | |
| 234 writer.writerow(trace_metrics) | |
| 235 | |
| 236 self._default_final_tasks.append(ExtractMetrics) | |
| 237 return ExtractMetrics | |
| OLD | NEW |