diff --git a/tests/test_socks_http_proxy.c b/tests/test_socks_http_proxy.c index e9352eac..39c659f0 100644 --- a/tests/test_socks_http_proxy.c +++ b/tests/test_socks_http_proxy.c @@ -45,6 +45,7 @@ static struct UASYNC* g_ua = NULL; static int g_ok = 0, g_done = 0, g_phase = 0; static pid_t g_http_pid = 0, g_https_pid = 0; static pid_t g_workers[WORKERS]; +static pid_t g_workers_orig[WORKERS]; static int g_http_port = 0, g_https_port = 0, g_cli_port = 0, g_srv_port = 0; static int g_socks_port = 0, g_http_proxy_port = 0; static char g_tmpdir[256]; @@ -223,6 +224,13 @@ static int worker_main(int wtype, const char* socks_proxy, const char* http_prox const char* type_names[] = { "socks_ipv4", "socks_domain", "http_proxy", "https_connect" }; const char* tname = type_names[wtype]; + char prof_path[512]; snprintf(prof_path, sizeof(prof_path), "%s/.prof_%d", tmpdir, getpid()); + FILE* prof = fopen(prof_path, "w"); + if (prof) fprintf(prof, "%s\n", tname); + + struct timespec t1, t2; + double times_ms[num_files]; + for (int fi = 0; fi < num_files; fi++) { char expected[512], out[512], url[512], cmd[2048]; snprintf(expected, sizeof(expected), "%s/%s", tmpdir, file_names[fi]); @@ -250,8 +258,10 @@ static int worker_main(int wtype, const char* socks_proxy, const char* http_prox break; } + clock_gettime(CLOCK_MONOTONIC, &t1); int rc = system(cmd); - if (rc != 0) { fprintf(stderr, "[FAIL] %s %s curl exit=%d\n", tname, file_names[fi], WEXITSTATUS(rc)); return 1; } + clock_gettime(CLOCK_MONOTONIC, &t2); + if (rc != 0) { fprintf(stderr, "[FAIL] %s %s curl exit=%d\n", tname, file_names[fi], WEXITSTATUS(rc)); if (prof) fclose(prof); return 1; } char cmp_cmd[1024]; snprintf(cmp_cmd, sizeof(cmp_cmd), "cmp -s %s %s", out, expected); @@ -261,9 +271,21 @@ static int worker_main(int wtype, const char* socks_proxy, const char* http_prox char sizecmd[1024]; snprintf(sizecmd, sizeof(sizecmd), "wc -c <%s", out); FILE* pf = popen(sizecmd, "r"); if (pf) { int sz = 0; fscanf(pf, "%d", &sz); pclose(pf); fprintf(stderr, " downloaded: %d bytes, expected: %d bytes\n", sz, file_sizes[fi]); } - unlink(out); return 1; + unlink(out); if (prof) fclose(prof); return 1; } unlink(out); + + double elapsed_ms = (t2.tv_sec - t1.tv_sec) * 1000.0 + (t2.tv_nsec - t1.tv_nsec) / 1e6; + double mbps = (file_sizes[fi] / (1024.0 * 1024.0)) / (elapsed_ms / 1000.0); + times_ms[fi] = elapsed_ms; + if (prof) fprintf(prof, "%s %d %.0f %.1f\n", file_names[fi], file_sizes[fi], elapsed_ms, mbps); + } + + { + double tot_ms = 0; int tot_bytes = 0; + for (int fi = 0; fi < num_files; fi++) { tot_ms += times_ms[fi]; tot_bytes += file_sizes[fi]; } + double avg_mbps = (tot_bytes / (1024.0 * 1024.0)) / (tot_ms / 1000.0); + if (prof) { fprintf(prof, "TOTAL %d %.0f %.1f\n", tot_bytes, tot_ms, avg_mbps); fclose(prof); prof = NULL; } } if (wtype == W_HTTP_PROXY) { @@ -339,6 +361,7 @@ static void monitor(void* arg) { _exit(rc); } g_workers[i] = pid; + g_workers_orig[i] = pid; } } } @@ -355,6 +378,33 @@ static void monitor(void* arg) { if (done_count >= WORKERS) { if (fail) { printf("[FAIL] %d/%d workers failed\n", fail, WORKERS); g_done = -1; } else { g_ok = 1; g_done = 1; } + + printf("\n=== PROFILING ===\n"); + printf("%-14s | %-9s %-9s %-9s %-9s | %-11s\n", + "Worker", "1KB", "100KB", "1MB", "10MB", "Avg MB/s"); + printf("%s\n", "-----------------------------------------------------------------"); + for (int i = 0; i < WORKERS; i++) { + if (g_workers[i] > 0) continue; + char pp[512]; snprintf(pp, sizeof(pp), "%s/.prof_%d", g_tmpdir, (int)((long)g_workers_orig[i])); + FILE* prf = fopen(pp, "r"); + if (!prf) continue; + char tname[64] = "?"; { char buf[128]; if (fgets(buf, sizeof(buf), prf)) sscanf(buf, "%63s", tname); } + double speeds[4] = {0}; char fn[64]; + for (int fi = 0; fi < num_files; fi++) { + double mbps; char buf[128]; + if (fgets(buf, sizeof(buf), prf)) { + int sz2; double em2; int n = sscanf(buf, "%63s %d %lf %lf", fn, &sz2, &em2, &mbps); + if (n >= 4) speeds[fi] = mbps; + } + } + double total_mbps = 0; { char buf[128]; while (fgets(buf, sizeof(buf), prf)) { + int tb; double tm, tm2; if (sscanf(buf, "TOTAL %d %lf %lf", &tb, &tm, &tm2) == 3) { total_mbps = tm2; break; } + }} + fclose(prf); + printf("%-14s | %5.1f MB/s %5.1f MB/s %5.1f MB/s %5.1f MB/s | %5.1f MB/s\n", + tname, speeds[0], speeds[1], speeds[2], speeds[3], total_mbps); + } + printf("\n"); fflush(stdout); } }