Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
async_executor_demo.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕ðŸŒĨ 🌊
3// See the LICENSE file in the project root for full license information.
4
21#include <iostream>
22#include <chrono>
23#include <vector>
25
26using namespace database::async;
27using namespace std::chrono;
28
30 std::cout << "=== Basic async_executor Usage ===\n";
31
32 // Create executor with default thread count
33 async_executor executor;
34
35 std::cout << "Executor created with " << executor.thread_count() << " threads\n";
36 std::cout << "Using thread_system: " << (executor.is_using_thread_system() ? "YES" : "NO") << "\n\n";
37
38 // Submit simple tasks
39 std::cout << "Submitting 5 simple tasks...\n";
40 std::vector<std::future<int>> futures;
41
42 auto start = high_resolution_clock::now();
43
44 for (int i = 0; i < 5; ++i) {
45 auto future = executor.submit([i]() {
46 // Simulate some work
47 std::this_thread::sleep_for(milliseconds(100));
48 return i * i;
49 });
50 futures.push_back(std::move(future));
51 }
52
53 // Collect results
54 std::cout << "Collecting results:\n";
55 for (size_t i = 0; i < futures.size(); ++i) {
56 int result = futures[i].get();
57 std::cout << " Task " << i << " result: " << result << "\n";
58 }
59
60 auto end = high_resolution_clock::now();
61 auto duration = duration_cast<milliseconds>(end - start);
62
63 std::cout << "Total time: " << duration.count() << "ms\n";
64 std::cout << "(Expected ~100ms with concurrent execution)\n\n";
65}
66
68 std::cout << "=== High-Throughput Performance Test ===\n";
69
70 async_executor executor(8);
71
72 const int num_tasks = 10000;
73 std::cout << "Submitting " << num_tasks << " lightweight tasks...\n";
74
75 auto start = high_resolution_clock::now();
76
77 std::vector<std::future<int>> futures;
78 futures.reserve(num_tasks);
79
80 for (int i = 0; i < num_tasks; ++i) {
81 futures.push_back(executor.submit([i]() {
82 // Very lightweight task
83 return i * 2;
84 }));
85 }
86
87 // Wait for completion
88 executor.wait_for_completion();
89
90 auto end = high_resolution_clock::now();
91 auto duration = duration_cast<microseconds>(end - start);
92
93 std::cout << "Submitted " << num_tasks << " tasks in "
94 << duration.count() << " microseconds\n";
95 std::cout << "Average latency: "
96 << (duration.count() / num_tasks) << " microseconds/task\n";
97
98 if (executor.is_using_thread_system()) {
99 std::cout << "✅ thread_system: Expected ~77ns latency per task\n";
100 } else {
101 std::cout << "⚠ïļ Fallback mode: Expected ~2-5Ξs latency per task\n";
102 }
103
104 std::cout << "\n";
105}
106
108 std::cout << "=== Error Handling ===\n";
109
110 async_executor executor(4);
111
112 // Submit task that throws exception
113 auto future = executor.submit([]() -> int {
114 throw std::runtime_error("Simulated error in task");
115 return 42;
116 });
117
118 try {
119 int result = future.get();
120 std::cout << "Result: " << result << "\n";
121 } catch (const std::exception& e) {
122 std::cout << "✅ Caught exception: " << e.what() << "\n";
123 }
124
125 std::cout << "\n";
126}
127
129 std::cout << "=== Graceful Shutdown ===\n";
130
131 async_executor executor(4);
132
133 // Submit long-running tasks
134 std::cout << "Submitting 3 long-running tasks...\n";
135 std::vector<std::future<void>> futures;
136
137 for (int i = 0; i < 3; ++i) {
138 futures.push_back(executor.submit([i]() {
139 std::cout << " Task " << i << " starting...\n";
140 std::this_thread::sleep_for(milliseconds(200));
141 std::cout << " Task " << i << " completed\n";
142 }));
143 }
144
145 std::cout << "Pending tasks: " << executor.pending_tasks() << "\n";
146
147 // Wait for completion
148 std::cout << "Waiting for tasks to complete...\n";
149 for (auto& future : futures) {
150 future.get();
151 }
152
153 std::cout << "✅ All tasks completed, shutting down...\n";
154 executor.shutdown();
155 std::cout << "✅ Executor shut down gracefully\n\n";
156}
157
159 std::cout << "=== Performance Comparison ===\n";
160 std::cout << "async_executor vs std::async\n\n";
161
162 const int num_tasks = 1000;
163
164 // Test async_executor
165 {
166 async_executor executor(8);
167 auto start = high_resolution_clock::now();
168
169 std::vector<std::future<int>> futures;
170 for (int i = 0; i < num_tasks; ++i) {
171 futures.push_back(executor.submit([i]() { return i * 2; }));
172 }
173
174 for (auto& f : futures) {
175 f.get();
176 }
177
178 auto end = high_resolution_clock::now();
179 auto duration = duration_cast<microseconds>(end - start);
180
181 std::cout << "async_executor: " << duration.count() << " Ξs\n";
182 std::cout << " (" << (duration.count() / num_tasks) << " Ξs/task)\n";
183 }
184
185 // Test std::async
186 {
187 auto start = high_resolution_clock::now();
188
189 std::vector<std::future<int>> futures;
190 for (int i = 0; i < num_tasks; ++i) {
191 futures.push_back(std::async(std::launch::async, [i]() { return i * 2; }));
192 }
193
194 for (auto& f : futures) {
195 f.get();
196 }
197
198 auto end = high_resolution_clock::now();
199 auto duration = duration_cast<microseconds>(end - start);
200
201 std::cout << "std::async: " << duration.count() << " Ξs\n";
202 std::cout << " (" << (duration.count() / num_tasks) << " Ξs/task)\n";
203 }
204
205 std::cout << "\n";
206}
207
208int main() {
209 std::cout << "╔════════════════════════════════════════════════╗\n";
210 std::cout << "║ async_executor Demonstration ║\n";
211 std::cout << "║ thread_system Integration for Database ║\n";
212 std::cout << "╚════════════════════════════════════════════════╝\n\n";
213
214 try {
220
221 std::cout << "╔════════════════════════════════════════════════╗\n";
222 std::cout << "║ ✅ All demonstrations completed successfully ║\n";
223 std::cout << "╚════════════════════════════════════════════════╝\n";
224
225 return 0;
226 }
227 catch (const std::exception& e) {
228 std::cerr << "❌ Error: " << e.what() << "\n";
229 return 1;
230 }
231}
void demonstrate_basic_usage()
void compare_with_legacy()
void demonstrate_shutdown()
void demonstrate_high_throughput()
void demonstrate_error_handling()
int main()
High-performance asynchronous executor using thread_system.
auto submit(F &&func, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > >
Submits a task for asynchronous execution.
void wait_for_completion()
Waits for all pending tasks to complete.
size_t thread_count() const
Returns the number of worker threads.
void shutdown()
Gracefully shuts down the executor.
size_t pending_tasks() const
Returns the number of pending tasks.
constexpr bool is_using_thread_system() const
Checks if using thread_system implementation.