OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2015 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 import multiprocessing | |
5 import tempfile | |
6 import os | |
7 | |
8 from profile_creators import fast_navigation_profile_extender | |
9 | |
10 | |
11 class _HistoryUrlIterator(object): | |
12 """This iterator generates a temporary file each time next() is called. It | |
13 returns a URI to that temporary file.""" | |
14 def __init__(self, history_profile_extender): | |
15 self._history_profile_extender = history_profile_extender | |
16 | |
17 def __iter__(self): | |
18 return self | |
19 | |
20 def next(self): | |
21 return self._history_profile_extender.MakeTemporaryFile() | |
22 | |
23 | |
24 class HistoryProfileExtender( | |
25 fast_navigation_profile_extender.FastNavigationProfileExtender): | |
26 """This extender navigates Chrome to a large number of URIs pointing to local | |
27 files. It continues running until the history DB becomes full.""" | |
28 _HISTORY_DB_MAX_SIZE_IN_MB = 10 | |
29 | |
30 def __init__(self): | |
31 # The rate limiting factors are the speed of page navigation, and the speed | |
32 # of python bindings. The former is larger than the latter, so having a | |
33 # large batch size skews the amortized average time per page load towards | |
34 # the latter. | |
35 maximum_batch_size = multiprocessing.cpu_count() * 2 | |
36 super(HistoryProfileExtender, self).__init__(maximum_batch_size) | |
37 | |
38 # A list of paths of temporary files. The instance is responsible for | |
39 # making sure that the files are deleted before they are removed from this | |
40 # list. | |
41 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.
| |
42 | |
43 def MakeTemporaryFile(self): | |
44 """Makes a temporary file and returns a URI to the file. | |
45 | |
46 This method has the side effect of appending the temporary file to | |
47 self._file_paths. The instance is responsible for deleting the file at a | |
48 later point in time. | |
49 """ | |
50 # Adding a long suffix to the name of the file fills up the history | |
51 # database faster. The suffix can't be too long, since some file systems | |
52 # have a 256 character limit on the length of the path. While we could | |
53 # dynamically vary the length of the path, that would generate different | |
54 # profiles on different OSes, which is not ideal. | |
55 suffix = "reallylongsuffixintendedtoartificiallyincreasethelengthoftheurl" | |
56 handle, path = tempfile.mkstemp(suffix=suffix) | |
57 os.close(handle) | |
58 self._file_paths.append(path) | |
59 | |
60 file_url = "file://" + path | |
61 return file_url | |
62 | |
63 def GetUrlIterator(self): | |
64 """Superclass override.""" | |
65 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.
| |
66 | |
67 def ShouldExitAfterBatchNavigation(self): | |
68 """Superclass override.""" | |
69 return self._IsHistoryDBAtMaxSize() | |
70 | |
71 def TearDown(self): | |
72 """Superclass override.""" | |
73 super(HistoryProfileExtender, self).TearDown() | |
74 for path in self._file_paths: | |
75 os.remove(path) | |
76 self._file_paths = [] | |
77 | |
78 def CleanUpAfterBatchNavigation(self): | |
79 """Superclass override.""" | |
80 for path in self._file_paths: | |
81 os.remove(path) | |
82 self._file_paths = [] | |
83 | |
84 def _IsHistoryDBAtMaxSize(self): | |
85 """Whether the history DB has reached its maximum size.""" | |
86 history_db_path = os.path.join(self.profile_path, "Default", "History") | |
87 stat_info = os.stat(history_db_path) | |
88 size = stat_info.st_size | |
89 | |
90 max_size_threshold = 0.95 | |
91 bytes_in_megabyte = 2**10 | |
92 max_size = (bytes_in_megabyte * | |
93 HistoryProfileExtender._HISTORY_DB_MAX_SIZE_IN_MB * max_size_threshold) | |
94 return size > max_size | |
OLD | NEW |