-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_test.cpp
More file actions
187 lines (154 loc) · 7.32 KB
/
performance_test.cpp
File metadata and controls
187 lines (154 loc) · 7.32 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
175
176
177
178
179
180
181
182
183
184
185
186
187
// BSD 3-Clause License
//
// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <chrono>
#include <iostream>
#include <memory>
#include <vector>
#include <atomic>
#include <thread>
#include "implementations/thread_pool/include/thread_pool.h"
#include "core/jobs/include/callback_job.h"
using namespace kcenon::thread;
using namespace kcenon::thread;
// Performance test to measure interface overhead
class performance_tester {
public:
struct test_results {
double jobs_per_second;
double avg_latency_ns;
std::chrono::milliseconds total_time;
};
static auto run_thread_pool_test(int num_jobs, int num_workers) -> test_results {
auto pool = std::make_shared<thread_pool>("perf_test");
// Add workers
std::vector<std::unique_ptr<thread_worker>> workers;
for (int i = 0; i < num_workers; ++i) {
workers.push_back(std::make_unique<thread_worker>());
}
pool->enqueue_batch(std::move(workers));
pool->start();
std::atomic<int> completed_jobs{0};
auto start_time = std::chrono::high_resolution_clock::now();
// Submit jobs using old interface
for (int i = 0; i < num_jobs; ++i) {
pool->enqueue(std::make_unique<callback_job>([&completed_jobs]() -> result_void {
completed_jobs.fetch_add(1);
return {};
}));
}
// Wait for completion
while (completed_jobs.load() < num_jobs) {
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
auto end_time = std::chrono::high_resolution_clock::now();
pool->stop();
auto total_time = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
double jobs_per_second = static_cast<double>(num_jobs) / total_time.count() * 1000.0;
double avg_latency_ns = static_cast<double>(total_time.count()) * 1000000.0 / num_jobs;
return {jobs_per_second, avg_latency_ns, total_time};
}
static auto run_interface_test(int num_jobs, int num_workers) -> test_results {
auto pool = std::make_shared<thread_pool>("interface_test");
// Add workers
std::vector<std::unique_ptr<thread_worker>> workers;
for (int i = 0; i < num_workers; ++i) {
workers.push_back(std::make_unique<thread_worker>());
}
pool->enqueue_batch(std::move(workers));
pool->start();
std::atomic<int> completed_jobs{0};
auto start_time = std::chrono::high_resolution_clock::now();
// Use enqueue with callback_job
for (int i = 0; i < num_jobs; ++i) {
auto job = std::make_unique<callback_job>([&completed_jobs]() -> common::VoidResult {
completed_jobs.fetch_add(1);
return common::ok();
});
pool->enqueue(std::move(job));
}
// Wait for completion
while (completed_jobs.load() < num_jobs) {
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
auto end_time = std::chrono::high_resolution_clock::now();
pool->stop();
auto total_time = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
double jobs_per_second = static_cast<double>(num_jobs) / total_time.count() * 1000.0;
double avg_latency_ns = static_cast<double>(total_time.count()) * 1000000.0 / num_jobs;
return {jobs_per_second, avg_latency_ns, total_time};
}
};
int main() {
const int num_jobs = 100000;
const int num_workers = 4;
const int num_runs = 3;
std::cout << "Performance Regression Test\n";
std::cout << "Jobs: " << num_jobs << ", Workers: " << num_workers << "\n";
std::cout << "Runs per test: " << num_runs << "\n\n";
// Warm up
std::cout << "Warming up...\n";
performance_tester::run_thread_pool_test(1000, 2);
double old_interface_total = 0.0;
double new_interface_total = 0.0;
for (int run = 0; run < num_runs; ++run) {
std::cout << "Run " << (run + 1) << ":\n";
// Test old interface
auto old_result = performance_tester::run_thread_pool_test(num_jobs, num_workers);
std::cout << " Old Interface: " << old_result.jobs_per_second
<< " jobs/sec, " << old_result.avg_latency_ns << " ns/job, "
<< old_result.total_time.count() << " ms\n";
old_interface_total += old_result.jobs_per_second;
// Test new interface
auto new_result = performance_tester::run_interface_test(num_jobs, num_workers);
std::cout << " New Interface: " << new_result.jobs_per_second
<< " jobs/sec, " << new_result.avg_latency_ns << " ns/job, "
<< new_result.total_time.count() << " ms\n";
new_interface_total += new_result.jobs_per_second;
double impact_percent = ((old_result.jobs_per_second - new_result.jobs_per_second)
/ old_result.jobs_per_second) * 100.0;
std::cout << " Performance Impact: " << impact_percent << "%\n\n";
}
// Calculate averages
double avg_old = old_interface_total / num_runs;
double avg_new = new_interface_total / num_runs;
double avg_impact = ((avg_old - avg_new) / avg_old) * 100.0;
std::cout << "Average Results:\n";
std::cout << " Old Interface: " << avg_old << " jobs/sec\n";
std::cout << " New Interface: " << avg_new << " jobs/sec\n";
std::cout << " Average Impact: " << avg_impact << "%\n";
if (std::abs(avg_impact) <= 5.0) {
std::cout << "✅ PASS: Performance impact " << avg_impact
<< "% is within 5% threshold\n";
return 0;
} else {
std::cout << "❌ FAIL: Performance impact " << avg_impact
<< "% exceeds 5% threshold\n";
return 1;
}
}