+ +
5797689 fix(windows): resolve MSVC test linkage, missing unistd, and export declarations
saifmukhtar · 2026-11-24
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * test/openavx_stream_bench.c - End-to-End Streaming, Codec, WAN & Fanout Benchmark |
| 7 | */ |
| 8 | |
| 9 | #define _POSIX_C_SOURCE 200809L |
| 10 | #define _DEFAULT_SOURCE |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <stdint.h> |
| 15 | #include <stdbool.h> |
| 16 | #include <string.h> |
| 17 | #include <time.h> |
| 18 | #include <errno.h> |
| 19 | #if !defined(_WIN32) |
| 20 | #include <pthread.h> |
| 21 | #include <sys/resource.h> |
| 22 | #endif |
| 23 | |
| 24 | #include "openavx.h" |
| 25 | #include "avx_platform.h" |
| 26 | #include "avx_speedhq.h" |
| 27 | |
| 28 | #define BENCH_RING_1080P 16 |
| 29 | #define BENCH_RING_4K 8 |
| 30 | #define BENCH_MAX_CLIENTS 8 |
| 31 | |
| 32 | typedef struct { |
| 33 | int64_t wall_ns; |
| 34 | uint64_t frames_sent; |
| 35 | uint64_t frames_recv; |
| 36 | uint64_t dropped_tx; |
| 37 | double cpu_pct; |
| 38 | double fps; |
| 39 | double mbps; |
| 40 | double jitter_ms; |
| 41 | float rtt_ms; |
| 42 | uint32_t tcp_retrans; |
| 43 | uint32_t congestion; |
| 44 | double p50_ms; |
| 45 | double p95_ms; |
| 46 | double p99_ms; |
| 47 | double max_ms; |
| 48 | } bench_metrics_t; |
| 49 | |
| 50 | typedef struct { |
| 51 | oavx_stream_t *rx; |
| 52 | volatile bool stop; |
| 53 | bool stalled; |
| 54 | int64_t bench_start_ns; |
| 55 | int64_t last_rx_ns; |
| 56 | int64_t audio_frames; |
| 57 | double *lat_ms; |
| 58 | size_t lat_cap; |
| 59 | size_t lat_count; |
| 60 | } rx_worker_t; |
| 61 | |
| 62 | static int64_t mono_ns(void) { |
| 63 | struct timespec ts; |
| 64 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 65 | return (int64_t)ts.tv_sec * 1000000000LL + (int64_t)ts.tv_nsec; |
| 66 | } |
| 67 | |
| 68 | static double proc_cpu_sec(void) { |
| 69 | #if defined(_WIN32) |
| 70 | FILETIME create_time, exit_time, kernel_time, user_time; |
| 71 | if (GetProcessTimes(GetCurrentProcess(), &create_time, &exit_time, &kernel_time, &user_time)) { |
| 72 | ULARGE_INTEGER k, u; |
| 73 | k.LowPart = kernel_time.dwLowDateTime; |
| 74 | k.HighPart = kernel_time.dwHighDateTime; |
| 75 | u.LowPart = user_time.dwLowDateTime; |
| 76 | u.HighPart = user_time.dwHighDateTime; |
| 77 | return (double)(k.QuadPart + u.QuadPart) * 1e-7; |
| 78 | } |
| 79 | return 0.0; |
| 80 | #else |
| 81 | struct rusage ru; |
| 82 | if (getrusage(RUSAGE_SELF, &ru) != 0) return 0.0; |
| 83 | double u = (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec * 1e-6; |
| 84 | double s = (double)ru.ru_stime.tv_sec + (double)ru.ru_stime.tv_usec * 1e-6; |
| 85 | return u + s; |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | static void sleep_until_ns(int64_t target_ns) { |
| 90 | #if defined(__APPLE__) || defined(_WIN32) |
| 91 | int64_t now = mono_ns(); |
| 92 | if (target_ns > now) { |
| 93 | int64_t diff_ns = target_ns - now; |
| 94 | struct timespec ts; |
| 95 | ts.tv_sec = (time_t)(diff_ns / 1000000000LL); |
| 96 | ts.tv_nsec = (long)(diff_ns % 1000000000LL); |
| 97 | nanosleep(&ts, NULL); |
| 98 | } |
| 99 | #else |
| 100 | struct timespec ts; |
| 101 | ts.tv_sec = (time_t)(target_ns / 1000000000LL); |
| 102 | ts.tv_nsec = (long)(target_ns % 1000000000LL); |
| 103 | while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR) {} |
| 104 | #endif |
| 105 | } |
| 106 | |
| 107 | static int cmp_double_asc(const void *a, const void *b) { |
| 108 | double da = *(const double*)a; |
| 109 | double db = *(const double*)b; |
| 110 | return (da > db) - (da < db); |
| 111 | } |
| 112 | |
| 113 | static void summarize_latencies(double *samples, size_t n, |
| 114 | double *p50, double *p95, double *p99, double *maxv) { |
| 115 | *p50 = *p95 = *p99 = *maxv = 0.0; |
| 116 | if (!samples || n == 0) return; |
| 117 | qsort(samples, n, sizeof(double), cmp_double_asc); |
| 118 | *p50 = samples[(size_t)((n - 1) * 0.50)]; |
| 119 | *p95 = samples[(size_t)((n - 1) * 0.95)]; |
| 120 | *p99 = samples[(size_t)((n - 1) * 0.99)]; |
| 121 | *maxv = samples[n - 1]; |
| 122 | } |
| 123 | |
| 124 | static void fill_uyvy_frame(uint8_t *buf, int w, int h, uint32_t seed) { |
| 125 | size_t total = (size_t)w * (size_t)h * 2; |
| 126 | uint32_t rng = seed ? seed : 0x13579BDFu; |
| 127 | for (int y = 0; y < h; ++y) { |
| 128 | uint8_t *row = buf + (size_t)y * (size_t)w * 2; |
| 129 | for (int x = 0; x < w; x += 2) { |
| 130 | rng = rng * 1664525u + 1013904223u; |
| 131 | uint8_t noise = (uint8_t)(rng >> 28); /* mild texture + gradient */ |
| 132 | uint8_t base_y = (uint8_t)(((x * 220) / (w > 1 ? w : 1)) + ((y + (int)seed) & 0x1F)); |
| 133 | row[x * 2 + 0] = (uint8_t)(64 + (((x + (int)seed * 7) * 128) / (w > 1 ? w : 1))); |
| 134 | row[x * 2 + 1] = (uint8_t)(base_y ^ noise); |
| 135 | row[x * 2 + 2] = (uint8_t)(192 - (((y + (int)seed * 3) * 128) / (h > 1 ? h : 1))); |
| 136 | row[x * 2 + 3] = (uint8_t)((base_y + 1) ^ (noise >> 1)); |
| 137 | } |
| 138 | } |
| 139 | (void)total; |
| 140 | } |
| 141 | |
| 142 | static void* rx_worker_main(void *arg) { |
| 143 | rx_worker_t *w = (rx_worker_t*)arg; |
| 144 | while (!w->stop) { |
| 145 | if (w->stalled) { |
| 146 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 20000000L }; |
| 147 | nanosleep(&ts, NULL); |
| 148 | continue; |
| 149 | } |
| 150 | oavx_video_frame_t v = {0}; |
| 151 | oavx_audio_frame_t a = {0}; |
| 152 | oavx_frame_type_t ft = oavx_stream_receive(w->rx, &v, &a, NULL, 25); |
| 153 | int64_t t_rx = mono_ns(); |
| 154 | if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 155 | w->last_rx_ns = t_rx; |
| 156 | if (v.pts_ns >= w->bench_start_ns && t_rx >= v.pts_ns && w->lat_count < w->lat_cap) { |
| 157 | w->lat_ms[w->lat_count++] = (double)(t_rx - v.pts_ns) / 1e6; |
| 158 | } else if (w->lat_count < w->lat_cap) { |
| 159 | w->lat_ms[w->lat_count++] = 0.0; |
| 160 | } |
| 161 | oavx_stream_release_video(w->rx, &v); |
| 162 | } else if (ft == OAVX_FRAME_TYPE_AUDIO) { |
| 163 | w->audio_frames++; |
| 164 | oavx_stream_release_audio(w->rx, &a); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (!w->stalled) { |
| 169 | /* Drain any trailing queued frames after sender stops */ |
| 170 | for (int idle = 0; idle < 6; ) { |
| 171 | oavx_video_frame_t v = {0}; |
| 172 | oavx_audio_frame_t a = {0}; |
| 173 | oavx_frame_type_t ft = oavx_stream_receive(w->rx, &v, &a, NULL, 20); |
| 174 | int64_t t_rx = mono_ns(); |
| 175 | if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 176 | idle = 0; |
| 177 | w->last_rx_ns = t_rx; |
| 178 | if (v.pts_ns >= w->bench_start_ns && t_rx >= v.pts_ns && w->lat_count < w->lat_cap) { |
| 179 | w->lat_ms[w->lat_count++] = (double)(t_rx - v.pts_ns) / 1e6; |
| 180 | } |
| 181 | oavx_stream_release_video(w->rx, &v); |
| 182 | } else if (ft == OAVX_FRAME_TYPE_AUDIO) { |
| 183 | idle = 0; |
| 184 | w->audio_frames++; |
| 185 | oavx_stream_release_audio(w->rx, &a); |
| 186 | } else { |
| 187 | idle++; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | /* ========================================================================= */ |
| 195 | /* LAYER 1: Full-Frame SpeedHQ Codec Benchmark (Cold Buffer Ring) */ |
| 196 | /* ========================================================================= */ |
| 197 | |
| 198 | static void bench_codec_case(int w, int h, int quality, int ring_n, int total_iters) { |
| 199 | size_t raw_bytes = (size_t)w * (size_t)h * 2; |
| 200 | size_t max_comp = raw_bytes + 4096; |
| 201 | |
| 202 | uint8_t **in_ring = (uint8_t**)calloc((size_t)ring_n, sizeof(uint8_t*)); |
| 203 | uint8_t **comp_ring = (uint8_t**)calloc((size_t)ring_n, sizeof(uint8_t*)); |
| 204 | size_t *comp_sizes = (size_t*)calloc((size_t)ring_n, sizeof(size_t)); |
| 205 | uint8_t *dec_out = (uint8_t*)malloc(raw_bytes); |
| 206 | |
| 207 | if (!in_ring || !comp_ring || !comp_sizes || !dec_out) { |
| 208 | fprintf(stderr, "OOM allocating Layer 1 ring (%dx%d)\n", w, h); |
| 209 | exit(1); |
| 210 | } |
| 211 | for (int i = 0; i < ring_n; ++i) { |
| 212 | in_ring[i] = (uint8_t*)malloc(raw_bytes); |
| 213 | comp_ring[i] = (uint8_t*)malloc(max_comp); |
| 214 | if (!in_ring[i] || !comp_ring[i]) exit(1); |
| 215 | fill_uyvy_frame(in_ring[i], w, h, (uint32_t)(i + 1) * 97u); |
| 216 | } |
| 217 | |
| 218 | oavx_speedhq_set_quality((uint8_t)quality); |
| 219 | |
| 220 | /* Warmup */ |
| 221 | size_t warm_sz = 0; |
| 222 | avx_speedhq_encode_ex(OAVX_FOURCC_VIDEO_TYPE_SHQ2, w, h, in_ring[0], NULL, comp_ring[0], max_comp, &warm_sz); |
| 223 | bool has_alpha = false; |
| 224 | avx_speedhq_decode_ex(OAVX_FOURCC_VIDEO_TYPE_SHQ2, w, h, comp_ring[0], warm_sz, dec_out, NULL, &has_alpha); |
| 225 | |
| 226 | double enc_cpu0 = proc_cpu_sec(); |
| 227 | int64_t enc_t0 = mono_ns(); |
| 228 | size_t total_comp_bytes = 0; |
| 229 | for (int i = 0; i < total_iters; ++i) { |
| 230 | int idx = i % ring_n; |
| 231 | size_t out_sz = 0; |
| 232 | if (avx_speedhq_encode_ex(OAVX_FOURCC_VIDEO_TYPE_SHQ2, w, h, |
| 233 | in_ring[idx], NULL, comp_ring[idx], max_comp, &out_sz) != 0 || out_sz == 0) { |
| 234 | fprintf(stderr, "SpeedHQ encode failed at %dx%d\n", w, h); |
| 235 | exit(1); |
| 236 | } |
| 237 | comp_sizes[idx] = out_sz; |
| 238 | total_comp_bytes += out_sz; |
| 239 | } |
| 240 | double enc_wall = (double)(mono_ns() - enc_t0) / 1e9; |
| 241 | double enc_cpu = proc_cpu_sec() - enc_cpu0; |
| 242 | |
| 243 | double dec_cpu0 = proc_cpu_sec(); |
| 244 | int64_t dec_t0 = mono_ns(); |
| 245 | for (int i = 0; i < total_iters; ++i) { |
| 246 | int idx = i % ring_n; |
| 247 | if (avx_speedhq_decode_ex(OAVX_FOURCC_VIDEO_TYPE_SHQ2, w, h, |
| 248 | comp_ring[idx], comp_sizes[idx], dec_out, NULL, &has_alpha) != 0) { |
| 249 | fprintf(stderr, "SpeedHQ decode failed at %dx%d\n", w, h); |
| 250 | exit(1); |
| 251 | } |
| 252 | } |
| 253 | double dec_wall = (double)(mono_ns() - dec_t0) / 1e9; |
| 254 | double dec_cpu = proc_cpu_sec() - dec_cpu0; |
| 255 | |
| 256 | double avg_kb = ((double)total_comp_bytes / (double)total_iters) / 1024.0; |
| 257 | double ratio = (double)raw_bytes / ((double)total_comp_bytes / (double)total_iters); |
| 258 | double mbps_at_60 = ((double)total_comp_bytes / (double)total_iters) * 8.0 * 60.0 / 1e6; |
| 259 | |
| 260 | printf(" %-11s q=%-2d | Enc: %6.1f FPS (%5.2f ms, %5.0f%% CPU) | Dec: %6.1f FPS (%5.2f ms, %5.0f%% CPU) | %6.1f KB (%4.1f:1, %6.1f Mbps@60)\n", |
| 261 | (w >= 3840) ? "4K UYVY" : "1080p UYVY", quality, |
| 262 | total_iters / enc_wall, (enc_wall / total_iters) * 1000.0, (enc_cpu / enc_wall) * 100.0, |
| 263 | total_iters / dec_wall, (dec_wall / total_iters) * 1000.0, (dec_cpu / dec_wall) * 100.0, |
| 264 | avg_kb, ratio, mbps_at_60); |
| 265 | |
| 266 | for (int i = 0; i < ring_n; ++i) { |
| 267 | free(in_ring[i]); |
| 268 | free(comp_ring[i]); |
| 269 | } |
| 270 | free(in_ring); |
| 271 | free(comp_ring); |
| 272 | free(comp_sizes); |
| 273 | free(dec_out); |
| 274 | } |
| 275 | |
| 276 | static void run_layer1_codec(bool quick) { |
| 277 | printf("\n=====================================================================================================================\n"); |
| 278 | printf(" LAYER 1: Full-Frame SpeedHQ-2 Codec Benchmark (Cold Multi-Frame Buffer Ring > L3 Cache)\n"); |
| 279 | printf("=====================================================================================================================\n"); |
| 280 | avx_speedhq_init(); |
| 281 | int n1080 = quick ? 30 : 80; |
| 282 | int n4k = quick ? 10 : 24; |
| 283 | bench_codec_case(1920, 1080, 96, BENCH_RING_1080P, n1080); |
| 284 | bench_codec_case(1920, 1080, 50, BENCH_RING_1080P, n1080); |
| 285 | bench_codec_case(3840, 2160, 96, BENCH_RING_4K, n4k); |
| 286 | bench_codec_case(3840, 2160, 50, BENCH_RING_4K, n4k); |
| 287 | oavx_speedhq_set_quality(96); |
| 288 | } |
| 289 | |
| 290 | /* ========================================================================= */ |
| 291 | /* LAYERS 2, 3, 4: Real Socket Pipeline Stream Benchmark (TCP & RUDP) */ |
| 292 | /* ========================================================================= */ |
| 293 | |
| 294 | static void run_stream_scenario(const char *label, |
| 295 | int w, int h, |
| 296 | oavx_fourcc_video_type_e tx_fourcc, |
| 297 | oavx_color_format_e rx_fmt, |
| 298 | oavx_transport_e transport, |
| 299 | int target_fps, |
| 300 | int num_frames, |
| 301 | int num_clients, |
| 302 | bool stall_last_client) { |
| 303 | oavx_context_t *ctx = oavx_context_create(); |
| 304 | if (!ctx) { |
| 305 | fprintf(stderr, "oavx_context_create failed\n"); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | oavx_stream_config_t tx_cfg = { |
| 310 | .name = label, |
| 311 | .groups = "bench", |
| 312 | .direction = OAVX_DIR_TRANSMIT, |
| 313 | .clock_sync = (target_fps > 0), |
| 314 | .max_connections = (uint32_t)(num_clients + 4), |
| 315 | .bind_ip = "127.0.0.1", |
| 316 | .transport = transport |
| 317 | }; |
| 318 | oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg); |
| 319 | if (!tx) { |
| 320 | fprintf(stderr, "Failed to open TX for %s\n", label); |
| 321 | oavx_context_destroy(ctx); |
| 322 | return; |
| 323 | } |
| 324 | const oavx_endpoint_t *ep = oavx_stream_get_endpoint(tx); |
| 325 | |
| 326 | oavx_stream_t *rxs[BENCH_MAX_CLIENTS] = {0}; |
| 327 | rx_worker_t workers[BENCH_MAX_CLIENTS] = {0}; |
| 328 | avx_thread_t threads[BENCH_MAX_CLIENTS] = {0}; |
| 329 | |
| 330 | int64_t bench_start = mono_ns(); |
| 331 | for (int c = 0; c < num_clients; ++c) { |
| 332 | char cname[64]; |
| 333 | snprintf(cname, sizeof(cname), "BENCH_RX_%d", c); |
| 334 | oavx_stream_config_t rx_cfg = { |
| 335 | .name = cname, |
| 336 | .endpoint = ep->address, |
| 337 | .direction = OAVX_DIR_RECEIVE, |
| 338 | .color_format = rx_fmt, |
| 339 | .bandwidth = OAVX_BANDWIDTH_HIGHEST, |
| 340 | .clock_sync = (target_fps > 0), |
| 341 | .transport = transport |
| 342 | }; |
| 343 | rxs[c] = oavx_stream_open(ctx, &rx_cfg); |
| 344 | workers[c].rx = rxs[c]; |
| 345 | workers[c].stop = false; |
| 346 | workers[c].stalled = (stall_last_client && c == num_clients - 1); |
| 347 | workers[c].bench_start_ns = bench_start; |
| 348 | workers[c].lat_cap = (size_t)(num_frames + 32); |
| 349 | workers[c].lat_ms = (double*)calloc(workers[c].lat_cap, sizeof(double)); |
| 350 | } |
| 351 | |
| 352 | for (int c = 0; c < num_clients; ++c) { |
| 353 | if (rxs[c]) avx_thread_create(&threads[c], rx_worker_main, &workers[c]); |
| 354 | } |
| 355 | |
| 356 | /* Wait for all clients to finish TCP handshake (including SYN retry on lossy WAN) */ |
| 357 | int connected_clients = 0; |
| 358 | for (int wait = 0; wait < 120; ++wait) { |
| 359 | connected_clients = oavx_stream_get_connection_count(tx, 25); |
| 360 | if (connected_clients >= num_clients) break; |
| 361 | } |
| 362 | if (connected_clients < num_clients) { |
| 363 | fprintf(stderr, " [WARN] %s: only %d/%d clients connected before handshake deadline\n", |
| 364 | label, connected_clients, num_clients); |
| 365 | } |
| 366 | struct timespec hs_ts = { .tv_sec = 0, .tv_nsec = 120000000L }; |
| 367 | nanosleep(&hs_ts, NULL); |
| 368 | |
| 369 | /* Prepare distinct UYVY frames + 48 kHz stereo audio chunk */ |
| 370 | int ring_n = 4; |
| 371 | size_t raw_bytes = (size_t)w * (size_t)h * 2; |
| 372 | uint8_t *ring[4] = {0}; |
| 373 | for (int i = 0; i < ring_n; ++i) { |
| 374 | ring[i] = (uint8_t*)malloc(raw_bytes); |
| 375 | fill_uyvy_frame(ring[i], w, h, (uint32_t)(i + 1) * 31u); |
| 376 | } |
| 377 | |
| 378 | float audio_l[800], audio_r[800]; |
| 379 | for (int i = 0; i < 800; ++i) { |
| 380 | audio_l[i] = 0.25f; |
| 381 | audio_r[i] = -0.25f; |
| 382 | } |
| 383 | |
| 384 | oavx_video_frame_t vf = {0}; |
| 385 | vf.width = (uint32_t)w; |
| 386 | vf.height = (uint32_t)h; |
| 387 | vf.fourcc = tx_fourcc; |
| 388 | vf.scan_type = OAVX_SCAN_PROGRESSIVE; |
| 389 | vf.strides[0] = (size_t)w * 2; |
| 390 | vf.framerate.num = (uint32_t)(target_fps > 0 ? target_fps * 1000 : 60000); |
| 391 | vf.framerate.den = 1000; |
| 392 | vf.aspect_ratio.num = 16; |
| 393 | vf.aspect_ratio.den = 9; |
| 394 | |
| 395 | oavx_audio_frame_t af = {0}; |
| 396 | af.sample_rate = 48000; |
| 397 | af.channels = 2; |
| 398 | af.frames = 800; /* 16.67 ms at 48 kHz */ |
| 399 | af.format = OAVX_AUDIO_FMT_FLT_PLANAR; |
| 400 | af.stride_bytes = sizeof(audio_l); |
| 401 | af.planes[0] = audio_l; |
| 402 | af.planes[1] = audio_r; |
| 403 | |
| 404 | double cpu0 = proc_cpu_sec(); |
| 405 | int64_t t0 = mono_ns(); |
| 406 | int64_t period_ns = (target_fps > 0) ? (1000000000LL / target_fps) : 0; |
| 407 | |
| 408 | for (int f = 0; f < num_frames; ++f) { |
| 409 | int64_t now = mono_ns(); |
| 410 | vf.planes[0] = ring[f % ring_n]; |
| 411 | vf.pts_ns = now; |
| 412 | oavx_stream_send_video(tx, &vf); |
| 413 | |
| 414 | af.pts_ns = now; |
| 415 | oavx_stream_send_audio(tx, &af); |
| 416 | |
| 417 | if (period_ns > 0) { |
| 418 | int64_t next_tick = t0 + (int64_t)(f + 1) * period_ns; |
| 419 | if (mono_ns() < next_tick) sleep_until_ns(next_tick); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /* Allow trailing packets / WAN NACKs in flight to land cleanly */ |
| 424 | long tail_ns = (transport == OAVX_TRANSPORT_RUDP) ? 350000000L : 150000000L; |
| 425 | struct timespec tail_ts = { .tv_sec = 0, .tv_nsec = tail_ns }; |
| 426 | nanosleep(&tail_ts, NULL); |
| 427 | int64_t t_end = mono_ns(); |
| 428 | double cpu_used = proc_cpu_sec() - cpu0; |
| 429 | |
| 430 | for (int c = 0; c < num_clients; ++c) workers[c].stop = true; |
| 431 | for (int c = 0; c < num_clients; ++c) { |
| 432 | if (rxs[c]) avx_thread_join(threads[c]); |
| 433 | } |
| 434 | |
| 435 | int64_t active_end_ns = t_end - (int64_t)tail_ns; |
| 436 | int active_clients = stall_last_client ? (num_clients - 1) : num_clients; |
| 437 | if (active_clients < 1) active_clients = 1; |
| 438 | int64_t max_rx_ns = 0; |
| 439 | for (int c = 0; c < active_clients; ++c) { |
| 440 | if (workers[c].last_rx_ns > max_rx_ns) max_rx_ns = workers[c].last_rx_ns; |
| 441 | } |
| 442 | if (max_rx_ns > t0) active_end_ns = max_rx_ns; |
| 443 | double wall_sec = (double)(active_end_ns - t0) / 1e9; |
| 444 | if (wall_sec <= 0.001) wall_sec = 0.001; |
| 445 | |
| 446 | oavx_telemetry_t tx_telem = {0}; |
| 447 | oavx_stream_get_telemetry(tx, &tx_telem); |
| 448 | |
| 449 | uint64_t total_rx_frames = 0; |
| 450 | uint64_t total_rx_wire = 0; |
| 451 | double sum_jitter = 0.0; |
| 452 | float max_rtt = 0.0f; |
| 453 | uint32_t max_retrans = 0; |
| 454 | size_t combined_lat_n = 0; |
| 455 | |
| 456 | for (int c = 0; c < active_clients; ++c) { |
| 457 | oavx_telemetry_t rx_telem = {0}; |
| 458 | if (rxs[c]) oavx_stream_get_telemetry(rxs[c], &rx_telem); |
| 459 | total_rx_frames += (uint64_t)workers[c].lat_count; |
| 460 | total_rx_wire += rx_telem.total_wire_bytes; |
| 461 | sum_jitter += rx_telem.inter_frame_jitter_ms; |
| 462 | if (rx_telem.kernel_rtt_ms > max_rtt) max_rtt = rx_telem.kernel_rtt_ms; |
| 463 | if (rx_telem.tcp_retransmissions > max_retrans) max_retrans = rx_telem.tcp_retransmissions; |
| 464 | combined_lat_n += workers[c].lat_count; |
| 465 | } |
| 466 | |
| 467 | double *all_lat = combined_lat_n ? (double*)malloc(combined_lat_n * sizeof(double)) : NULL; |
| 468 | size_t pos = 0; |
| 469 | for (int c = 0; c < active_clients; ++c) { |
| 470 | if (all_lat && workers[c].lat_count > 0) { |
| 471 | memcpy(all_lat + pos, workers[c].lat_ms, workers[c].lat_count * sizeof(double)); |
| 472 | pos += workers[c].lat_count; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | double p50 = 0, p95 = 0, p99 = 0, max_l = 0; |
| 477 | summarize_latencies(all_lat, pos, &p50, &p95, &p99, &max_l); |
| 478 | free(all_lat); |
| 479 | |
| 480 | double avg_rx_fps = ((double)total_rx_frames / (double)active_clients) / wall_sec; |
| 481 | double wire_mbps = ((double)tx_telem.total_wire_bytes * 8.0) / (wall_sec * 1e6); |
| 482 | if (wire_mbps == 0.0 && total_rx_wire > 0) { |
| 483 | wire_mbps = ((double)total_rx_wire * 8.0) / (wall_sec * 1e6); |
| 484 | } |
| 485 | double avg_jitter = sum_jitter / (double)active_clients; |
| 486 | |
| 487 | printf(" %-34s | RX:%5.1f/%-3d (%3llu/%-3d) | %7.1f Mbps | Drop:%4lld | CPU:%5.0f%% | Jit:%4.2fms | p50:%5.2f p95:%5.2f p99:%5.2f max:%6.2f ms | rtt:%.2f retx:%u cg:%u\n", |
| 488 | label, |
| 489 | avg_rx_fps, target_fps > 0 ? target_fps : 0, |
| 490 | (unsigned long long)(total_rx_frames / (uint64_t)active_clients), num_frames, |
| 491 | wire_mbps, |
| 492 | (long long)tx_telem.dropped_frames_total, |
| 493 | (cpu_used / wall_sec) * 100.0, |
| 494 | avg_jitter, |
| 495 | p50, p95, p99, max_l, |
| 496 | (double)max_rtt > 0 ? max_rtt : tx_telem.kernel_rtt_ms, |
| 497 | max_retrans > 0 ? max_retrans : tx_telem.tcp_retransmissions, |
| 498 | tx_telem.congestion_level); |
| 499 | |
| 500 | for (int c = 0; c < num_clients; ++c) { |
| 501 | free(workers[c].lat_ms); |
| 502 | if (rxs[c]) oavx_stream_close(rxs[c]); |
| 503 | } |
| 504 | for (int i = 0; i < ring_n; ++i) free(ring[i]); |
| 505 | oavx_stream_close(tx); |
| 506 | oavx_context_destroy(ctx); |
| 507 | } |
| 508 | |
| 509 | static void run_layer2_single(bool quick) { |
| 510 | int n1080 = quick ? 90 : 180; |
| 511 | int n4k = quick ? 45 : 120; |
| 512 | printf("\n=====================================================================================================================\n"); |
| 513 | printf(" LAYER 2: Single-Stream End-to-End Pipeline Benchmark (127.0.0.1, TX -> Wire -> RX + 48kHz Audio)\n"); |
| 514 | printf("=====================================================================================================================\n"); |
| 515 | run_stream_scenario("1080p60 UYVY -> FASTEST (raw)", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_UYVY, OAVX_COLOR_FORMAT_FASTEST, OAVX_TRANSPORT_TCP, 60, n1080, 1, false); |
| 516 | run_stream_scenario("1080p60 UYVY -> BGRA (SIMD)", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_UYVY, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_TRANSPORT_TCP, 60, n1080, 1, false); |
| 517 | run_stream_scenario("1080p60 SHQ2 -> BGRA (Codec)", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_SHQ2, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_TRANSPORT_TCP, 60, n1080, 1, false); |
| 518 | run_stream_scenario("4K60 UYVY -> FASTEST (raw)", 3840, 2160, OAVX_FOURCC_VIDEO_TYPE_UYVY, OAVX_COLOR_FORMAT_FASTEST, OAVX_TRANSPORT_TCP, 60, n4k, 1, false); |
| 519 | run_stream_scenario("4K60 SHQ2 -> BGRA (Codec)", 3840, 2160, OAVX_FOURCC_VIDEO_TYPE_SHQ2, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_TRANSPORT_TCP, 60, n4k, 1, false); |
| 520 | run_stream_scenario("1080p UYVY -> FASTEST (burst)", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_UYVY, OAVX_COLOR_FORMAT_FASTEST, OAVX_TRANSPORT_TCP, 0, n1080, 1, false); |
| 521 | run_stream_scenario("1080p SHQ2 -> BGRA (burst)", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_SHQ2, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_TRANSPORT_TCP, 0, n1080, 1, false); |
| 522 | } |
| 523 | |
| 524 | static void run_layer3_transport(bool quick, bool wan_active) { |
| 525 | int n = quick ? 90 : 240; |
| 526 | printf("\n=====================================================================================================================\n"); |
| 527 | printf(" LAYER 3: Transport Comparison — TCP vs RUDP (%s, 1080p60 SHQ2 -> BGRA)\n", |
| 528 | wan_active ? "WAN tc netem mode" : "Clean Loopback Baseline"); |
| 529 | printf("=====================================================================================================================\n"); |
| 530 | run_stream_scenario("TCP 1080p60 SHQ2 -> BGRA", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_SHQ2, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_TRANSPORT_TCP, 60, n, 1, false); |
| 531 | run_stream_scenario("RUDP 1080p60 SHQ2 -> BGRA", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_SHQ2, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_TRANSPORT_RUDP, 60, n, 1, false); |
| 532 | } |
| 533 | |
| 534 | static void run_layer4_fanout(bool quick) { |
| 535 | int n = quick ? 60 : 120; |
| 536 | printf("\n=====================================================================================================================\n"); |
| 537 | printf(" LAYER 4: Multi-Viewer Fanout Scaling & Slow-Client Isolation (1080p60 SHQ2 -> BGRA)\n"); |
| 538 | printf("=====================================================================================================================\n"); |
| 539 | run_stream_scenario("Fanout 1 client (SHQ2 1080p60)", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_SHQ2, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_TRANSPORT_TCP, 60, n, 1, false); |
| 540 | run_stream_scenario("Fanout 2 clients (SHQ2 1080p60)", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_SHQ2, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_TRANSPORT_TCP, 60, n, 2, false); |
| 541 | run_stream_scenario("Fanout 4 clients (SHQ2 1080p60)", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_SHQ2, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_TRANSPORT_TCP, 60, n, 4, false); |
| 542 | run_stream_scenario("Fanout 8 clients (SHQ2 1080p60)", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_SHQ2, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_TRANSPORT_TCP, 60, n, 8, false); |
| 543 | run_stream_scenario("Isolation: 3 fast + 1 stalled rx", 1920, 1080, OAVX_FOURCC_VIDEO_TYPE_SHQ2, OAVX_COLOR_FORMAT_UYVY_BGRA, OAVX_TRANSPORT_TCP, 60, n, 4, true); |
| 544 | } |
| 545 | |
| 546 | static void print_usage(const char *prog) { |
| 547 | printf("Usage: %s [--all | --layer <1|2|3|4>] [--wan] [--quick]\n", prog); |
| 548 | } |
| 549 | |
| 550 | int main(int argc, char **argv) { |
| 551 | int layer = 0; |
| 552 | bool wan_active = false; |
| 553 | bool quick = false; |
| 554 | |
| 555 | for (int i = 1; i < argc; ++i) { |
| 556 | if (strcmp(argv[i], "--all") == 0) { |
| 557 | layer = 0; |
| 558 | } else if (strcmp(argv[i], "--wan") == 0) { |
| 559 | wan_active = true; |
| 560 | if (layer == 0) layer = 3; |
| 561 | } else if (strcmp(argv[i], "--quick") == 0) { |
| 562 | quick = true; |
| 563 | } else if (strcmp(argv[i], "--layer") == 0 && i + 1 < argc) { |
| 564 | layer = atoi(argv[++i]); |
| 565 | } else { |
| 566 | print_usage(argv[0]); |
| 567 | return 1; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | oavx_set_log_level(OAVX_LOG_ERROR); |
| 572 | |
| 573 | printf("=== OpenAVX End-to-End Full Stream Benchmark (%s) ===\n", oavx_version()); |
| 574 | printf("[*] Mode: %s%s\n", |
| 575 | quick ? "Quick" : "Standard", |
| 576 | wan_active ? " | WAN netem profile expected on lo" : ""); |
| 577 | |
| 578 | if (layer == 0 || layer == 1) run_layer1_codec(quick); |
| 579 | if (layer == 0 || layer == 2) run_layer2_single(quick); |
| 580 | if (layer == 0 || layer == 3) run_layer3_transport(quick, wan_active); |
| 581 | if (layer == 0 || layer == 4) run_layer4_fanout(quick); |
| 582 | |
| 583 | printf("\n[DONE] openavx_stream_bench\n"); |
| 584 | return 0; |
| 585 | } |