forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser_perf.cpp
More file actions
174 lines (164 loc) · 4.71 KB
/
Copy pathtest_parser_perf.cpp
File metadata and controls
174 lines (164 loc) · 4.71 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
166
167
168
169
170
171
172
173
174
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#include "sql/parser/ob_parser.h"
#include <gtest/gtest.h>
#include "lib/allocator/page_arena.h"
#include <fstream>
#include <iterator>
#include <vector>
#include <string>
#include <iostream>
using namespace oceanbase;
using namespace oceanbase::common;
using namespace oceanbase::sql;
namespace test
{
static char *parse_file = NULL;
static int LOOP_COUNT = 1;
static bool PRINT_STAT = false;
static bool IS_FP = false;
class TestParserPerf
{
public:
TestParserPerf();
virtual ~TestParserPerf();
void do_parse(const char* query_str);
private:
DISALLOW_COPY_AND_ASSIGN(TestParserPerf);
public:
ObArenaAllocator allocator_;
int64_t total_t_;
int64_t total_cnt_;
int64_t succ_cnt_;
};
TestParserPerf::TestParserPerf()
: allocator_(ObModIds::TEST),
total_t_(0),
total_cnt_(0),
succ_cnt_(0)
{
}
TestParserPerf::~TestParserPerf()
{
}
void TestParserPerf::do_parse(const char* query_str)
{
int64_t t0 = 0, t1 = 0;
ObSQLMode mode = SMO_DEFAULT;
ObParser parser(allocator_, mode);
ParseResult parse_result;
ParseMode pmode = IS_FP ? FP_MODE : STD_MODE;
parse_result.is_fp_ = IS_FP;
ObString query = ObString::make_string(query_str);
int ret = OB_SUCCESS;
t0 = ObTimeUtility::current_time();
ret = parser.parse(query, parse_result, pmode);
t1 = ObTimeUtility::current_time();
if (OB_SUCC(ret)) {
succ_cnt_ ++;
}
total_t_ += t1 - t0;
total_cnt_++;
if (PRINT_STAT) {
printf("==%s\n",query_str);
printf("==%s\n", parse_result.no_param_sql_);
printf("time:%ld, len:%d, ",t1 - t0 , parse_result.no_param_sql_len_);
printf("param_node_num:%d\n", parse_result.param_node_num_);
ParamList *param = parse_result.param_nodes_;
for (int32_t i = 0; OB_SUCC(ret) && i < parse_result.param_node_num_ && NULL != param; i ++) {
printf(" param_%d: type:%d; value:%ld, str_value:%s, raw_text:%s, pos_:%ld\n",
i, param->node_->type_,
param->node_->value_,
param->node_->str_value_,
param->node_->raw_text_,
param->node_->pos_);
param = param->next_;
}
}
parser.free_result(parse_result);
allocator_.reset();
}
int load_sql(const char *test_file, std::vector<std::string> &sql_array)
{
int ret = OB_SUCCESS;
std::ifstream if_tests(test_file);
if (!if_tests.is_open()) {
SQL_PC_LOG(ERROR, "maybe reach max file open");
ret = OB_ERROR;
}
std::string line;
std::string total_line;
;
while (std::getline(if_tests, line)) {
// allow human readable formatting
if (line.size() <= 0) continue;
std::size_t begin = line.find_first_not_of('\t');
if (line.at(begin) == '#') continue;
std::size_t end = line.find_last_not_of('\t');
std::string exact_line = line.substr(begin, end - begin + 1);
total_line += exact_line;
total_line += " ";
if (exact_line.at(exact_line.length() - 1) != ';') continue;
else {
sql_array.push_back(total_line);
total_line = "";
}
}
if_tests.close();
return ret;
}
void run() {
std::vector<std::string> test_sql_array;
load_sql("./test_parser.sql", test_sql_array);
TestParserPerf pp;
for(int i = 0; i < LOOP_COUNT; i++) {
for (int j = 0; j < (int)test_sql_array.size(); j++) {
pp.do_parse(test_sql_array.at(j).c_str());
}
}
std::cout << "====" << "succ_cnt:" << pp.succ_cnt_ << std::endl;
std::cout << "====" << "total_cnt:" << pp.total_cnt_ << std::endl;
std::cout << "====" << "avg_time:" << (double)(pp.total_t_)/(double)(pp.total_cnt_) << std::endl;
}
}
int main(int argc, char **argv)
{
OB_LOGGER.set_log_level("ERROR");
OB_LOGGER.set_file_name("test_parser.log", true);
int c = 0;
while(-1 != (c = getopt(argc, argv, "q:pfl:n:"))) {
switch(c) {
case 'q':
test::parse_file = optarg;
break;
case 'n':
test::LOOP_COUNT = atoi(optarg);
break;
case 'f':
test::IS_FP = true;
break;
case 'l':
if (NULL != optarg) {
OB_LOGGER.set_log_level(optarg);
}
break;
case 'p':
test::PRINT_STAT = true;
break;
default:
printf("usage:");
break;
}
}
::test::run();
return 0;
}