This repository was archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathquery_logger_test.cpp
142 lines (111 loc) · 5.22 KB
/
query_logger_test.cpp
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
//===----------------------------------------------------------------------===//
//
// Peloton
//
// query_logger_test.cpp
//
// Identification: test/tuning/query_logger_test.cpp
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "common/harness.h"
#include "brain/query_logger.h"
#include "settings/settings_manager.h"
#include "sql/testing_sql_util.h"
namespace peloton {
namespace test {
class QueryLoggerTests : public PelotonTests {
protected:
void SetUp() override {
settings::SettingsManager::SetBool(settings::SettingId::brain, true);
PelotonInit::Initialize();
// query to check that logging is done
select_query_ =
"SELECT query_string, fingerprint FROM "
"peloton.pg_catalog.pg_query_history;";
brain::QueryLogger::Fingerprint fingerprint{select_query_};
select_query_fingerprint_ = fingerprint.GetFingerprint();
wait_time_ = 2;
}
void TearDown() override { PelotonInit::Shutdown(); }
// Executes the given query and then checks if the queries that are executed
// till now are actually logged
void TestSimpleUtil(std::string const &test_query,
std::vector<std::string> &expected_result) {
brain::QueryLogger::Fingerprint fingerprint{test_query};
std::string test_query_fingerprint = fingerprint.GetFingerprint();
expected_result.push_back(test_query + "|" + test_query_fingerprint);
TestingSQLUtil::ExecuteSQLQuery(test_query.c_str());
// give some time to actually log this query
sleep(wait_time_);
TestingSQLUtil::ExecuteSQLQueryAndCheckResult(select_query_.c_str(),
expected_result, true);
// the select query we used will also be logged for next time
expected_result.push_back(select_query_ + "|" + select_query_fingerprint_);
}
// Executes the given query and then checks if the queries that are executed
// until now are actually logged only when the transaction commits. Otherwise
// stores to queries for checking this later when commit happens.
void TestTransactionUtil(std::string const &test_query,
std::vector<std::string> &expected_result,
bool committed) {
static std::vector<std::string> temporary_expected_result;
brain::QueryLogger::Fingerprint fingerprint{test_query};
std::string test_query_fingerprint = fingerprint.GetFingerprint();
temporary_expected_result.push_back(test_query + "|" +
test_query_fingerprint);
TestingSQLUtil::ExecuteSQLQuery(test_query.c_str());
// give some time to actually log this query
sleep(wait_time_);
// only check once the the transaction has committed
if (committed) {
// accounting for the select_query_ that happened before this txn
expected_result.push_back(select_query_ + "|" +
select_query_fingerprint_);
expected_result.insert(expected_result.end(),
temporary_expected_result.begin(),
temporary_expected_result.end());
temporary_expected_result.clear();
TestingSQLUtil::ExecuteSQLQueryAndCheckResult(select_query_.c_str(),
expected_result, true);
// the select query we used will also be logged for next time
expected_result.push_back(select_query_ + "|" +
select_query_fingerprint_);
} else {
// verify that the logging does not happen before the txn commit
TestingSQLUtil::ExecuteSQLQueryAndCheckResult(select_query_.c_str(),
expected_result, true);
// the select query we used will also be logged for next time
temporary_expected_result.push_back(select_query_ + "|" +
select_query_fingerprint_);
}
}
protected:
// fixed query to check the queries logged in the table
std::string select_query_;
// fingerprint for the fixed query
std::string select_query_fingerprint_;
// time to wait in seconds for the query to log into the table
int wait_time_;
};
// Testing the functionality of query logging
TEST_F(QueryLoggerTests, QueriesTest) {
// used to store the expected result
std::vector<std::string> expected_result;
// create the table and do some inserts and check
TestSimpleUtil("CREATE TABLE test(a INT);", expected_result);
TestSimpleUtil("INSERT INTO test VALUES (1);", expected_result);
TestSimpleUtil("INSERT INTO test VALUES (2);", expected_result);
expected_result
.pop_back(); // the select_query_ done at the end of above test
// won't be logged till the txn below commits
// check if the queries are logged only when the transaction actually commits
TestTransactionUtil("BEGIN;", expected_result, false);
TestTransactionUtil("INSERT INTO test VALUES (1);", expected_result, false);
TestTransactionUtil("COMMIT;", expected_result, true);
// final check to see if everything is ok
TestSimpleUtil(select_query_, expected_result);
}
} // namespace test
} // namespace peloton