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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
| #include <liburing.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <errno.h> #include <sys/time.h> #include <time.h>
/** * 性能测试结果结构 */ typedef struct { const char *test_name; long long execution_time_us; int operation_count; double throughput_ops; double average_latency_us; } performance_result_t;
/** * 获取当前时间(微秒) */ long long get_current_time_us() { struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec * 1000000LL + tv.tv_usec; }
/** * 传统同步I/O性能测试 */ int test_sync_io_performance(performance_result_t *result) { const int operation_count = 1000; const size_t buffer_size = 4096; char *buffer = malloc(buffer_size); long long start_time, end_time; if (!buffer) { return -1; } printf("执行同步I/O性能测试...\n"); // 创建测试文件 const char *filename = "sync_test.dat"; int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd == -1) { free(buffer); return -1; } start_time = get_current_time_us(); // 执行同步写入操作 for (int i = 0; i < operation_count; i++) { // 填充测试数据 for (size_t j = 0; j < buffer_size; j++) { buffer[j] = 'A' + (i + j) % 26; } ssize_t written = write(fd, buffer, buffer_size); if (written != (ssize_t)buffer_size) { printf("写入失败\n"); close(fd); unlink(filename); free(buffer); return -1; } } end_time = get_current_time_us(); close(fd); unlink(filename); free(buffer); result->execution_time_us = end_time - start_time; result->operation_count = operation_count; result->throughput_ops = (double)operation_count / (result->execution_time_us / 1000000.0); result->average_latency_us = (double)result->execution_time_us / operation_count; printf("同步I/O测试完成\n"); return 0; }
/** * io_uring异步I/O性能测试 */ int test_io_uring_performance(performance_result_t *result) { struct io_uring ring; const int operation_count = 1000; const size_t buffer_size = 4096; char **buffers; long long start_time, end_time; int ret; printf("执行io_uring异步I/O性能测试...\n"); // 初始化io_uring ret = io_uring_queue_init(256, &ring, 0); if (ret < 0) { printf("io_uring初始化失败: %s\n", strerror(-ret)); return -1; } // 分配缓冲区 buffers = malloc(operation_count * sizeof(char*)); if (!buffers) { io_uring_queue_exit(&ring); return -1; } for (int i = 0; i < operation_count; i++) { buffers[i] = malloc(buffer_size); if (!buffers[i]) { // 清理已分配的缓冲区 for (int j = 0; j < i; j++) { free(buffers[j]); } free(buffers); io_uring_queue_exit(&ring); return -1; } // 填充测试数据 for (size_t j = 0; j < buffer_size; j++) { buffers[i][j] = 'A' + (i + j) % 26; } } // 创建测试文件 const char *filename = "async_test.dat"; int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd == -1) { perror("创建测试文件失败"); // 清理缓冲区 for (int i = 0; i < operation_count; i++) { free(buffers[i]); } free(buffers); io_uring_queue_exit(&ring); return -1; } start_time = get_current_time_us(); // 提交异步写入操作 int submitted = 0; for (int i = 0; i < operation_count; i++) { struct io_uring_sqe *sqe = io_uring_get_sqe(&ring); if (!sqe) { printf("获取SQE失败\n"); break; } io_uring_prep_write(sqe, fd, buffers[i], buffer_size, i * buffer_size); sqe->user_data = i; submitted++; // 定期提交操作 if (submitted % 32 == 0 || i == operation_count - 1) { ret = io_uring_submit(&ring); if (ret < 0) { printf("提交操作失败: %s\n", strerror(-ret)); break; } } } printf("提交了 %d 个异步操作\n", submitted); // 等待所有操作完成 int completed = 0; while (completed < submitted) { struct io_uring_cqe *cqe; ret = io_uring_wait_cqe(&ring, &cqe); if (ret < 0) { printf("等待完成事件失败: %s\n", strerror(-ret)); break; } completed++; io_uring_cqe_seen(&ring, cqe); } end_time = get_current_time_us(); printf("完成了 %d 个异步操作\n", completed); // 清理资源 close(fd); unlink(filename); for (int i = 0; i < operation_count; i++) { free(buffers[i]); } free(buffers); io_uring_queue_exit(&ring); result->execution_time_us = end_time - start_time; result->operation_count = completed; result->throughput_ops = (double)completed / (result->execution_time_us / 1000000.0); result->average_latency_us = (double)result->execution_time_us / completed; printf("io_uring异步I/O测试完成\n"); return 0; }
/** * 演示性能对比测试 */ int demo_performance_comparison() { performance_result_t sync_result = {0}; performance_result_t async_result = {0}; printf("=== io_uring vs 同步I/O 性能对比 ===\n"); // 设置测试结果名称 sync_result.test_name = "同步I/O"; async_result.test_name = "io_uring异步I/O"; // 执行同步I/O测试 printf("1. 执行同步I/O测试:\n"); if (test_sync_io_performance(&sync_result) != 0) { printf(" 同步I/O测试失败\n"); return -1; } printf(" 测试完成\n"); // 执行io_uring测试 printf("\n2. 执行io_uring异步I/O测试:\n"); if (test_io_uring_performance(&async_result) != 0) { printf(" io_uring测试失败\n"); return -1; } printf(" 测试完成\n"); // 显示测试结果 printf("\n=== 性能测试结果 ===\n"); printf("%-20s %-15s %-15s %-15s %-15s\n", "测试类型", "操作次数", "耗时(μs)", "吞吐量(ops/s)", "平均延迟(μs)"); printf("%-20s %-15s %-15s %-15s %-15s\n", "--------", "--------", "--------", "------------", "------------"); printf("%-20s %-15d %-15lld %-15.0f %-15.2f\n", sync_result.test_name, sync_result.operation_count, sync_result.execution_time_us, sync_result.throughput_ops, sync_result.average_latency_us); printf("%-20s %-15d %-15lld %-15.0f %-15.2f\n", async_result.test_name, async_result.operation_count, async_result.execution_time_us, async_result.throughput_ops, async_result.average_latency_us); // 性能对比分析 printf("\n=== 性能对比分析 ===\n"); if (sync_result.execution_time_us > 0 && async_result.execution_time_us > 0) { double time_improvement = (double)sync_result.execution_time_us / async_result.execution_time_us; double throughput_improvement = async_result.throughput_ops / sync_result.throughput_ops; double latency_reduction = (sync_result.average_latency_us - async_result.average_latency_us) / sync_result.average_latency_us * 100; printf("执行时间对比: %.2f 倍提升\n", time_improvement); printf("吞吐量对比: %.2f 倍提升\n", throughput_improvement); printf("平均延迟减少: %.1f%%\n", latency_reduction); } // 显示优势分析 printf("\n=== 优势分析 ===\n"); printf("1. io_uring优势:\n"); printf(" ✓ 零拷贝数据传输\n"); printf(" ✓ 减少系统调用次数\n"); printf(" ✓ 提高I/O并发性能\n"); printf(" ✓ 更好的CPU利用率\n"); printf("\n2. 适用场景:\n"); printf(" ✓ 高并发网络服务器\n"); printf(" ✓ 大文件传输应用\n"); printf(" ✓ 实时数据处理\n"); printf(" ✓ 数据库存储引擎\n"); printf("\n3. 性能优化建议:\n"); printf(" ✓ 合理设置环形缓冲区大小\n"); printf(" ✓ 批量提交I/O操作\n"); printf(" ✓ 使用适当的等待策略\n"); printf(" ✓ 监控系统资源使用\n"); return 0; }
int main() { return demo_performance_comparison(); }
|