OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/perf/generate_profile.h" | 5 #include "chrome/test/perf/generate_profile.h" |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_enumerator.h" | 10 #include "base/files/file_enumerator.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 inline float RandomFloat() { | 48 inline float RandomFloat() { |
49 return rand() / static_cast<float>(RAND_MAX); | 49 return rand() / static_cast<float>(RAND_MAX); |
50 } | 50 } |
51 | 51 |
52 // Return an integer uniformly in [min,max). | 52 // Return an integer uniformly in [min,max). |
53 inline int RandomInt(int min, int max) { | 53 inline int RandomInt(int min, int max) { |
54 return min + (rand() % (max-min)); | 54 return min + (rand() % (max-min)); |
55 } | 55 } |
56 | 56 |
57 // Return a string of |count| lowercase random characters. | 57 // Return a string of |count| lowercase random characters. |
58 string16 RandomChars(int count) { | 58 base::string16 RandomChars(int count) { |
59 string16 str; | 59 base::string16 str; |
60 for (int i = 0; i < count; ++i) | 60 for (int i = 0; i < count; ++i) |
61 str += L'a' + rand() % 26; | 61 str += L'a' + rand() % 26; |
62 return str; | 62 return str; |
63 } | 63 } |
64 | 64 |
65 string16 RandomWord() { | 65 base::string16 RandomWord() { |
66 // TODO(evanm): should we instead use the markov chain based | 66 // TODO(evanm): should we instead use the markov chain based |
67 // version of this that I already wrote? | 67 // version of this that I already wrote? |
68 | 68 |
69 // Sample a word length from kWordLengthProbabilities. | 69 // Sample a word length from kWordLengthProbabilities. |
70 float sample = RandomFloat(); | 70 float sample = RandomFloat(); |
71 size_t i; | 71 size_t i; |
72 for (i = 0; i < arraysize(kWordLengthProbabilities); ++i) { | 72 for (i = 0; i < arraysize(kWordLengthProbabilities); ++i) { |
73 sample -= kWordLengthProbabilities[i]; | 73 sample -= kWordLengthProbabilities[i]; |
74 if (sample < 0) break; | 74 if (sample < 0) break; |
75 } | 75 } |
76 const int word_length = i + 1; | 76 const int word_length = i + 1; |
77 return RandomChars(word_length); | 77 return RandomChars(word_length); |
78 } | 78 } |
79 | 79 |
80 // Return a string of |count| random words. | 80 // Return a string of |count| random words. |
81 string16 RandomWords(int count) { | 81 base::string16 RandomWords(int count) { |
82 string16 str; | 82 base::string16 str; |
83 for (int i = 0; i < count; ++i) { | 83 for (int i = 0; i < count; ++i) { |
84 if (!str.empty()) | 84 if (!str.empty()) |
85 str += L' '; | 85 str += L' '; |
86 str += RandomWord(); | 86 str += RandomWord(); |
87 } | 87 } |
88 return str; | 88 return str; |
89 } | 89 } |
90 | 90 |
91 // Return a random URL-looking string. | 91 // Return a random URL-looking string. |
92 GURL ConstructRandomURL() { | 92 GURL ConstructRandomURL() { |
93 return GURL(ASCIIToUTF16("http://") + RandomChars(3) + ASCIIToUTF16(".com/") + | 93 return GURL(ASCIIToUTF16("http://") + RandomChars(3) + ASCIIToUTF16(".com/") + |
94 RandomChars(RandomInt(5, 20))); | 94 RandomChars(RandomInt(5, 20))); |
95 } | 95 } |
96 | 96 |
97 // Return a random page title-looking string. | 97 // Return a random page title-looking string. |
98 string16 ConstructRandomTitle() { | 98 base::string16 ConstructRandomTitle() { |
99 return RandomWords(RandomInt(3, 15)); | 99 return RandomWords(RandomInt(3, 15)); |
100 } | 100 } |
101 | 101 |
102 // Insert a batch of |batch_size| URLs, starting at pageid |page_id|. | 102 // Insert a batch of |batch_size| URLs, starting at pageid |page_id|. |
103 void InsertURLBatch(Profile* profile, | 103 void InsertURLBatch(Profile* profile, |
104 int page_id, | 104 int page_id, |
105 int batch_size, | 105 int batch_size, |
106 int types) { | 106 int types) { |
107 HistoryService* history_service = | 107 HistoryService* history_service = |
108 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); | 108 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 path = file_iterator.Next(); | 244 path = file_iterator.Next(); |
245 } | 245 } |
246 | 246 |
247 printf("Finished creating profiles for testing.\n"); | 247 printf("Finished creating profiles for testing.\n"); |
248 | 248 |
249 // Restore the random seed. | 249 // Restore the random seed. |
250 srand(static_cast<unsigned int>(Time::Now().ToInternalValue())); | 250 srand(static_cast<unsigned int>(Time::Now().ToInternalValue())); |
251 | 251 |
252 return true; | 252 return true; |
253 } | 253 } |
OLD | NEW |