From 47058e2fb0b5dc91d95d2422f7eb06f3ff6a6bde Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Sun, 1 Sep 2024 23:27:49 +0800 Subject: [PATCH] Measure fastly --- include/chsrc.h | 53 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/include/chsrc.h b/include/chsrc.h index 3a61860..8b538af 100644 --- a/include/chsrc.h +++ b/include/chsrc.h @@ -7,13 +7,14 @@ * Contributors : Peng Gao * | * Created on : <2023-08-29> - * Last modified : <2024-08-30> + * Last modified : <2024-09-01> * * chsrc 头文件 * ------------------------------------------------------------*/ #include "xy.h" #include "source.h" +#include #define App_Name "chsrc" @@ -359,9 +360,11 @@ to_human_readable_speed (double speed) * 功劳和版权属于原作者,由 @ccmywish 修改为C语言,并做了额外调整 * * @return 返回测得的速度,若出错,返回-1 + * + * 该函数实际原型为 double * (*)(const char*) */ -double -measure_speed (const char *url) +void * +measure_speed (void *url) { char *time_sec = "6"; @@ -404,14 +407,18 @@ measure_speed (const char *url) if (200!=http_code) { - char* httpcodestr = yellow (xy_2strjoin ("HTTP码 ", buf)); + char *httpcodestr = yellow (xy_2strjoin ("HTTP码 ", buf)); puts (xy_strjoin (3, speedstr, " | ", httpcodestr)); } else { puts (speedstr); } - return speed; + + double *speed_exported = xy_malloc0 (sizeof(double)); + *speed_exported = speed; + + return speed_exported; } @@ -467,15 +474,20 @@ auto_select_ (SourceInfo *sources, size_t size, const char *target_name) } double speeds[size]; + bool measured[size]; // 是否测速了 double speed = 0.0; + + pthread_t *threads = xy_malloc0 (sizeof(pthread_t) * size); + for (int i=0; i__bigfile_url; + const char *url = src.mirror->__bigfile_url; if (NULL==url) { if (xy_streql ("upstream", src.mirror->code)) { + speed = 0; continue; /* 上游默认源不测速 */ } else @@ -485,6 +497,8 @@ auto_select_ (SourceInfo *sources, size_t size, const char *target_name) chsrc_warn (xy_strjoin (3, msg1, src.mirror->code, msg2)); speed = 0; } + speeds[i] = speed; + measured[i] = false; } else { @@ -492,9 +506,32 @@ auto_select_ (SourceInfo *sources, size_t size, const char *target_name) printf ("%s", xy_strjoin (3, msg, src.mirror->site , " ... ")); fflush (stdout); - speed = measure_speed (url); + + char *url_ = xy_strdup (url); + int ret = pthread_create (&threads[i], NULL, measure_speed, url_); + if (ret!=0) + { + chsrc_error ("Unable to measure speed\n"); + exit (Exit_UserCause); + } + else + { + measured[i] = true; + } + // speed = measure_speed (url); + } + } + + /* 汇总 */ + for (int i=0; i