Chromium Code Reviews| Index: tools/perf/profile_creators/history_profile_extender.py |
| diff --git a/tools/perf/profile_creators/history_profile_extender.py b/tools/perf/profile_creators/history_profile_extender.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..120333eb5d63adbd850019643ab840bae5c264c1 |
| --- /dev/null |
| +++ b/tools/perf/profile_creators/history_profile_extender.py |
| @@ -0,0 +1,94 @@ |
| +# Copyright 2015 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| +import multiprocessing |
| +import tempfile |
| +import os |
| + |
| +from profile_creators import fast_navigation_profile_extender |
| + |
| + |
| +class _HistoryUrlIterator(object): |
| + """This iterator generates a temporary file each time next() is called. It |
| + returns a URI to that temporary file.""" |
| + def __init__(self, history_profile_extender): |
| + self._history_profile_extender = history_profile_extender |
| + |
| + def __iter__(self): |
| + return self |
| + |
| + def next(self): |
| + return self._history_profile_extender.MakeTemporaryFile() |
| + |
| + |
| +class HistoryProfileExtender( |
| + fast_navigation_profile_extender.FastNavigationProfileExtender): |
| + """This extender navigates Chrome to a large number of URIs pointing to local |
| + files. It continues running until the history DB becomes full.""" |
| + _HISTORY_DB_MAX_SIZE_IN_MB = 10 |
| + |
| + def __init__(self): |
| + # The rate limiting factors are the speed of page navigation, and the speed |
| + # of python bindings. The former is larger than the latter, so having a |
| + # large batch size skews the amortized average time per page load towards |
| + # the latter. |
| + maximum_batch_size = multiprocessing.cpu_count() * 2 |
| + super(HistoryProfileExtender, self).__init__(maximum_batch_size) |
| + |
| + # A list of paths of temporary files. The instance is responsible for |
| + # making sure that the files are deleted before they are removed from this |
| + # list. |
| + self._file_paths = [] |
|
nednguyen
2015/02/19 17:07:37
Can you change this to self._generated_temp_files?
erikchen
2015/02/19 18:24:49
Done.
|
| + |
| + def MakeTemporaryFile(self): |
| + """Makes a temporary file and returns a URI to the file. |
| + |
| + This method has the side effect of appending the temporary file to |
| + self._file_paths. The instance is responsible for deleting the file at a |
| + later point in time. |
| + """ |
| + # Adding a long suffix to the name of the file fills up the history |
| + # database faster. The suffix can't be too long, since some file systems |
| + # have a 256 character limit on the length of the path. While we could |
| + # dynamically vary the length of the path, that would generate different |
| + # profiles on different OSes, which is not ideal. |
| + suffix = "reallylongsuffixintendedtoartificiallyincreasethelengthoftheurl" |
| + handle, path = tempfile.mkstemp(suffix=suffix) |
| + os.close(handle) |
| + self._file_paths.append(path) |
| + |
| + file_url = "file://" + path |
| + return file_url |
| + |
| + def GetUrlIterator(self): |
| + """Superclass override.""" |
| + return _HistoryUrlIterator(self) |
|
nednguyen
2015/02/19 17:07:37
How about using yield?
while True:
yield self.
erikchen
2015/02/19 18:24:49
Ah yes, saves an entire class. Thanks.
|
| + |
| + def ShouldExitAfterBatchNavigation(self): |
| + """Superclass override.""" |
| + return self._IsHistoryDBAtMaxSize() |
| + |
| + def TearDown(self): |
| + """Superclass override.""" |
| + super(HistoryProfileExtender, self).TearDown() |
| + for path in self._file_paths: |
| + os.remove(path) |
| + self._file_paths = [] |
| + |
| + def CleanUpAfterBatchNavigation(self): |
| + """Superclass override.""" |
| + for path in self._file_paths: |
| + os.remove(path) |
| + self._file_paths = [] |
| + |
| + def _IsHistoryDBAtMaxSize(self): |
| + """Whether the history DB has reached its maximum size.""" |
| + history_db_path = os.path.join(self.profile_path, "Default", "History") |
| + stat_info = os.stat(history_db_path) |
| + size = stat_info.st_size |
| + |
| + max_size_threshold = 0.95 |
| + bytes_in_megabyte = 2**10 |
| + max_size = (bytes_in_megabyte * |
| + HistoryProfileExtender._HISTORY_DB_MAX_SIZE_IN_MB * max_size_threshold) |
| + return size > max_size |