-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_parallel_work.h
84 lines (71 loc) · 2.63 KB
/
test_parallel_work.h
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
#include "search_server.h"
#include "log_duration.h"
#include <execution>
#include <iostream>
#include <random>
#include <string>
#include <vector>
using namespace std;
string GenerateWord(mt19937& generator, int max_length) {
const int length = uniform_int_distribution(1, max_length)(generator);
string word;
word.reserve(length);
for (int i = 0; i < length; ++i) {
word.push_back(uniform_int_distribution<>('a', 'z')(generator));
}
return word;
}
vector<string> GenerateDictionary(mt19937& generator, int word_count, int max_length) {
vector<string> words;
words.reserve(word_count);
for (int i = 0; i < word_count; ++i) {
words.push_back(GenerateWord(generator, max_length));
}
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[uniform_int_distribution<int>(0, dictionary.size() - 1)(generator)];
}
return query;
}
vector<string> GenerateQueries(mt19937& generator, const vector<string>& dictionary, int query_count, int max_word_count) {
vector<string> queries;
queries.reserve(query_count);
for (int i = 0; i < query_count; ++i) {
queries.push_back(GenerateQuery(generator, dictionary, max_word_count));
}
return queries;
}
template <typename ExecutionPolicy>
void Test(string_view mark, const SearchServer& search_server, const vector<string>& queries, ExecutionPolicy&& policy) {
LOG_DURATION(mark);
double total_relevance = 0;
for (const string_view query : queries) {
for (const auto& document : search_server.FindTopDocuments(policy, query)) {
total_relevance += document.relevance;
}
}
cout << total_relevance << endl;
}
#define TEST(policy) Test(#policy, search_server, queries, execution::policy)
void parallel_test() {
mt19937 generator;
const auto dictionary = GenerateDictionary(generator, 1000, 10);
const auto documents = GenerateQueries(generator, dictionary, 10'000, 70);
SearchServer search_server(dictionary[0]);
for (size_t i = 0; i < documents.size(); ++i) {
search_server.AddDocument(i, documents[i], DocumentStatus::ACTUAL, {1, 2, 3});
}
const auto queries = GenerateQueries(generator, dictionary, 100, 70);
TEST(seq);
TEST(par);
}