Skip to content

Commit 72b2469

Browse files
author
zhangjipeng
committed
test(perf): fix performance test benchmark compare code
Signed-off-by: zhangjipeng <zhangjipeng@xiaomi.com>
1 parent 0a85a1d commit 72b2469

4 files changed

Lines changed: 31 additions & 26 deletions

File tree

perf_tests/perf_tests.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ FetchContent_Declare(
2525
)
2626
FetchContent_MakeAvailable(cJSON)
2727
add_library(CJson::CJson INTERFACE IMPORTED)
28-
target_compile_definitions(CJson::CJson INTERFACE ENABLE_CJSON_TEST=OFF)
28+
target_compile_definitions(CJson::CJson INTERFACE -DENABLE_CJSON_TEST=OFF)
2929
target_link_libraries(CJson::CJson INTERFACE cjson)
3030

3131

@@ -63,7 +63,7 @@ if (WIN32)
6363
COMMAND_EXPAND_LISTS
6464
)
6565
else()
66-
target_compile_definitions(${PERF_TESTS} PRIVATE LINUX=1)
66+
target_compile_definitions(${PERF_TESTS} PRIVATE -DLINUX=1)
6767
target_compile_options(${PERF_TESTS} PRIVATE -std=gnu++17)
6868
configure_file(${PROJECT_ROOT}/cfg/ZCOOLXiaoWei-Regular.ttf ${CMAKE_CURRENT_BINARY_DIR}/ZCOOLXiaoWei-Regular.ttf COPYONLY)
6969
endif()

perf_tests/test.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,13 @@ void clear_test_canvas(void)
142142
memset(test_buffer, 0xFF, TEST_WIDTH * TEST_HEIGHT * 4);
143143
}
144144

145-
void PerformanceTest::LoadBaseline()
145+
bool PerformanceTest::LoadBaseline(const std::string& file_name, BenchmarkData& data)
146146
{
147-
baseline_data.clear();
147+
data.clear();
148148

149-
std::ifstream file(baseline_file);
149+
std::ifstream file(file_name);
150150
if (!file.is_open()) {
151-
need_write_baseline = true;
152-
return;
151+
return false;
153152
}
154153

155154
std::string json_str((std::istreambuf_iterator<char>(file)),
@@ -158,8 +157,7 @@ void PerformanceTest::LoadBaseline()
158157

159158
if (json_str.empty()) {
160159
std::cerr << "Error: JSON file is empty" << std::endl;
161-
need_write_baseline = true;
162-
return;
160+
return false;
163161
}
164162

165163
cJSON* root = cJSON_Parse(json_str.c_str());
@@ -169,15 +167,13 @@ void PerformanceTest::LoadBaseline()
169167
std::cerr << "Error: JSON parse error before: " << error_ptr << std::endl;
170168
}
171169
std::cerr << "Error: Failed to parse JSON from file " << baseline_file << std::endl;
172-
need_write_baseline = true;
173-
return;
170+
return false;
174171
}
175172

176173
if (cJSON_IsObject(root) == 0) {
177174
std::cerr << "Error: Root JSON element is not an object" << std::endl;
178175
cJSON_Delete(root);
179-
need_write_baseline = true;
180-
return;
176+
return false;
181177
}
182178

183179
cJSON* current = NULL;
@@ -229,22 +225,23 @@ void PerformanceTest::LoadBaseline()
229225
}
230226

231227
if (valid) {
232-
baseline_data[key] = result;
228+
data[key] = result;
233229
}
234230
}
235231

236232
cJSON_Delete(root);
233+
return true;
237234
}
238235

239-
void PerformanceTest::SaveBaseline()
236+
void PerformanceTest::SaveBaseline(const std::string& file_name)
240237
{
241238
cJSON* root = cJSON_CreateObject();
242239
if (!root) {
243240
std::cerr << "Error: Failed to create root JSON object" << std::endl;
244241
return;
245242
}
246243

247-
for (const auto& pair : baseline_data) {
244+
for (const auto& pair : newbase_data) {
248245
const std::string& key = pair.first;
249246
const BenchmarkResult& result = pair.second;
250247

@@ -270,9 +267,9 @@ void PerformanceTest::SaveBaseline()
270267
return;
271268
}
272269

273-
std::ofstream file(baseline_file);
270+
std::ofstream file(file_name);
274271
if (!file.is_open()) {
275-
std::cerr << "Error: Failed to open file " << baseline_file << " for writing" << std::endl;
272+
std::cerr << "Error: Failed to open file " << file_name << " for writing" << std::endl;
276273
free(json_str);
277274
cJSON_Delete(root);
278275
return;

perf_tests/test.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ class PerformanceTest : public ::testing::Test
8787
double max_ms;
8888
};
8989

90-
std::map<std::string, BenchmarkResult> baseline_data;
90+
typedef std::map<std::string, BenchmarkResult> BenchmarkData;
91+
BenchmarkData baseline_data;
92+
BenchmarkData newbase_data;
9193
std::string baseline_file;
94+
std::string newbase_file;
9295
bool need_write_baseline;
9396

9497
static void SetUpTestSuite()
@@ -104,25 +107,29 @@ class PerformanceTest : public ::testing::Test
104107
void Init(const std::string& test_name)
105108
{
106109
baseline_file = "./benchmark/" + test_name + "_" + SYSTEM + "_" + ARCH + ".json";
110+
newbase_file = "./benchmark/" + test_name + "_" + SYSTEM + "_" + ARCH + "_new.json";
107111
}
108112

109113
void SetUp() override
110114
{
111115
need_write_baseline = false;
112-
LoadBaseline();
116+
if (!LoadBaseline(baseline_file, baseline_data)) {
117+
need_write_baseline = true;
118+
}
119+
LoadBaseline(newbase_file, newbase_data);
113120

114121
clear_dcache();
115122
}
116123

117124
void TearDown() override
118125
{
119126
if (need_write_baseline) {
120-
SaveBaseline();
127+
SaveBaseline(newbase_file);
121128
}
122129
}
123130

124-
void LoadBaseline();
125-
void SaveBaseline();
131+
bool LoadBaseline(const std::string& file_name, BenchmarkData& data);
132+
void SaveBaseline(const std::string& file_name);
126133

127134
template<typename Func>
128135
BenchmarkResult RunBenchmark(const std::string& test_name, Func&& func)
@@ -175,7 +182,7 @@ class PerformanceTest : public ::testing::Test
175182
std::string key = test_name;
176183

177184
if (baseline_data.find(key) == baseline_data.end()) {
178-
baseline_data[key] = result;
185+
newbase_data[key] = result;
179186
std::cout << "[New Baseline] " << test_name << ": "
180187
<< std::setprecision(6)
181188
<< "median: " << result.mid_ms << " ms (avg: "
@@ -191,8 +198,9 @@ class PerformanceTest : public ::testing::Test
191198
<< std::setprecision(6) << "median: " << result.mid_ms << " ms (avg: " << result.avg_ms
192199
<< ", min: " << result.min_ms
193200
<< ", max: " << result.max_ms << ")" << std::endl;
194-
baseline_data[key] = result;
201+
need_write_baseline = true;
195202
}
203+
newbase_data[key] = result;
196204
} else {
197205
EXPECT_LE(std::abs(diff_percent), TOLERANCE_PERCENT)
198206
<< "Performance regression detected for " << test_name

unit_tests/unit_tests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ if (WIN32)
6262
COMMAND_EXPAND_LISTS
6363
)
6464
else()
65-
target_compile_definitions(${UNIT_TESTS} PRIVATE LINUX=1)
65+
target_compile_definitions(${UNIT_TESTS} PRIVATE -DLINUX=1)
6666
target_compile_options(${UNIT_TESTS} PRIVATE -std=gnu++17)
6767
configure_file(${PROJECT_ROOT}/cfg/ZCOOLXiaoWei-Regular.ttf ${CMAKE_CURRENT_BINARY_DIR}/ZCOOLXiaoWei-Regular.ttf COPYONLY)
6868
endif()

0 commit comments

Comments
 (0)