-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_example_functions.cpp
More file actions
165 lines (145 loc) · 5.17 KB
/
Copy pathtest_example_functions.cpp
File metadata and controls
165 lines (145 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "log_duration.h"
#include "test_example_functions.h"
#include <vector>
using namespace std;
void AddDocument(SearchServer &search_server, int document_id, const std::string &document,
DocumentStatus status, const std::vector<int> &ratings) {
search_server.AddDocument (document_id, document, status, ratings);
}
void FindTopDocuments(const SearchServer &search_server, const std::string &raw_query)
{
{
LOG_DURATION_STREAM("Operation time"s, cout);
cout << "Результаты поиска по запросу: "s << raw_query << endl;
auto result = search_server.FindTopDocuments(raw_query);
for (auto &document : result) {
PrintDocument(document);
}
}
cout << endl;
}
void ProfileGetWordFrequencies() {
auto test = [](int docs) {
vector<string> temp_docs;
temp_docs.reserve(static_cast<size_t>(docs));
for (int i = 0; i < docs; ++i) {
string doc = to_string(i) + "_word1 " +
to_string(i) + "_word2 " +
to_string(i) + "_word3 ";
temp_docs.push_back(doc);
}
SearchServer server(""s);
for (int i = 0; i < docs; ++i) {
server.AddDocument(i, temp_docs[static_cast<size_t>(i)]);
}
LOG_DURATION_STREAM("GetWordFrequencies operation time for "s
+ to_string(docs) + " documents"s, cout);
// проверяем в цикле 10000 раз, иначе будет слишком быстро
for (int i = 0; i < 1000; ++i) {
server.GetWordFrequencies(50);
}
};
// тестируем для 100 документов
{
test(100);
}
// тестируем для 1000 документов
{
test(1000);
}
// тестируем для 10000 документов
{
test(10000);
}
// тестируем для 100000 документов
{
test(100000);
}
}
void ProfileRemoveDocument() {
auto test = [] (int words, int docs) {
vector<string> temp_docs;
temp_docs.reserve(static_cast<size_t>(docs));
for (int i = 0; i < docs; ++i) {
string doc;
doc.clear();
for (int j = 1 ; j <= words; ++j) {
doc += to_string(i) + "_word"s + to_string(j) + " "s;
}
temp_docs.push_back(doc);
}
SearchServer server(""s);
for (int i = 0; i < docs; ++i) {
server.AddDocument(i, temp_docs[static_cast<size_t>(i)]);
}
LOG_DURATION_STREAM("RemoveDocument operation time for "s + to_string(docs) +
" documents with "s + to_string(words) + " words"s, cout);
for (int i = 0; i < 10; ++i) {
server.RemoveDocument(i);
}
};
//// тестируем для 100 документов в 100 слов
{
test(100, 100);
}
//// тестируем для 1000 документов в 100 слов
{
test(1000, 100);
}
//// тестируем для 100 документов в 500 слов
{
test(100, 500);
}
//// тестируем для 1000 документов в 500 слов
{
test(1000, 500);
}
//// тестируем для 100 документов в 1000 слов
{
test(100, 1000);
}
//// тестируем для 1000 документов в 1000 слов
{
test(1000, 1000);
}
}
string GenerateWord(std::mt19937 &generator, int max_length) {
const int length = std::uniform_int_distribution(1, max_length)(generator);
std::string word;
word.reserve(static_cast<size_t>(length));
for (int i = 0; i < length; ++i) {
word.push_back(std::uniform_int_distribution('a', 'z')(generator));
}
return word;
}
std::vector<string> GenerateDictionary(std::mt19937 &generator, int word_count, int max_length) {
std::vector<std::string> words;
words.reserve(static_cast<size_t>(word_count));
for (int i = 0; i < word_count; ++i) {
words.push_back(GenerateWord(generator, max_length));
}
std::sort(words.begin(), words.end());
words.erase(unique(words.begin(), words.end()), words.end());
return words;
}
string GenerateQuery(mt19937& generator, const vector<string>& dictionary, int word_count, double minus_prob = 0) {
string query;
for (int i = 0; i < word_count; ++i) {
if (!query.empty()) {
query.push_back(' ');
}
if (uniform_real_distribution<>(0, 1)(generator) < minus_prob) {
query.push_back('-');
}
query += dictionary[static_cast<size_t>(uniform_int_distribution<int>(0, static_cast<int>(dictionary.size()) - 1)(generator))];
}
return query;
}
std::vector<string> GenerateQueries(std::mt19937 &generator, const std::vector<std::string> &dictionary, int query_count, int max_word_count) {
std::vector<std::string> queries;
queries.reserve(static_cast<size_t>(query_count));
for (int i = 0; i < query_count; ++i) {
queries.push_back(GenerateQuery(generator, dictionary, max_word_count));
}
return queries;
}