diff --git a/include/chsrc.h b/include/chsrc.h index e8729c4..cf114bf 100644 --- a/include/chsrc.h +++ b/include/chsrc.h @@ -27,6 +27,7 @@ bool CliOpt_Locally = false; bool CliOpt_InEnglish = false; bool CliOpt_DryRun = false; bool CliOpt_NoColor = false; +bool CliOpt_Parallel = false; /** * -local 的含义是启用 *项目级* 换源 @@ -408,9 +409,14 @@ to_human_readable_speed (double speed) * 该函数实际原型为 char * (*)(const char*) */ void * -measure_speed (void *url) +measure_speed_for_url (void *url) { - char *time_sec = "9"; + char *time_sec = NULL; + + if (CliOpt_Parallel) + time_sec = "9"; + else + time_sec = "6"; /* 现在我们切换至跳转后的链接来测速,不再使用下述判断 if (xy_str_start_with(url, "https://registry.npmmirror")) @@ -533,47 +539,62 @@ measure_speed_in_group (int size, SourceInfo whole_sources[], double whole_speed const char *msg = CliOpt_InEnglish ? src.mirror->abbr : src.mirror->name; measure_msgs[i] = xy_strjoin (3, " - ", msg, " ... "); printf ("%s", measure_msgs[i]); - say (""); - // fflush (stdout); + + if (CliOpt_Parallel) + say (""); /* 并行时直接显示下一测速状态行 */ + else + fflush (stdout); char *url_ = xy_strdup (url); - // say (url); - int ret = pthread_create (&threads[i], NULL, measure_speed, url_); - if (ret!=0) + + if (CliOpt_Parallel) { - chsrc_error ("Unable to measure speed\n"); - exit (Exit_UserCause); + int ret = pthread_create (&threads[i], NULL, measure_speed_for_url, url_); + if (ret!=0) + { + chsrc_error ("Unable to measure speed\n"); + exit (Exit_UserCause); + } + else + { + get_measured[i] = true; + get_measured_n += 1; + } } else { - get_measured[i] = true; - get_measured_n += 1; + char *curl_result = measure_speed_for_url (url_); + double speed = parse_and_say_curl_result (curl_result); + whole_speeds[i+grp_cursor_in_whole] = speed; } } } - /* 一组汇总 */ - char **curl_results = xy_malloc0 (sizeof(char *) * size); - for (int i=0; i cpu_cores ? cpu_cores : (size-1); - int ngroup = (size-1) / group_size; - int rest = (size-1) % group_size; - // 跳过 upstream speed_records[0] = 0.0; int grp_cursor = 1; - for (int i=0; i 0) - measure_speed_in_group (rest, sources, speed_records, grp_cursor); + if (CliOpt_Parallel) + { + // 跳过 upstream + int cpu_cores = chsrc_get_cpucore () ; + int group_size = (size-1) > cpu_cores ? cpu_cores : (size-1); + int ngroup = (size-1) / group_size; + int rest = (size-1) % group_size; + + for (int i=0; i 0) + measure_speed_in_group (rest, sources, speed_records, grp_cursor); + } + else + { + measure_speed_in_group (size-1, sources, speed_records, grp_cursor); + } say (""); diff --git a/src/chsrc.c b/src/chsrc.c index 32e00a0..9fdb4c0 100644 --- a/src/chsrc.c +++ b/src/chsrc.c @@ -13,7 +13,7 @@ * | Terrasse * | * Created On : <2023-08-28> - * Last Modified : <2024-08-29> + * Last Modified : <2024-09-04> * * chsrc: Change Source —— 全平台通用命令行换源工具 * ------------------------------------------------------------*/ @@ -124,8 +124,9 @@ Chsrc_Usage[] = { "选项:", "-dry Dry Run,模拟换源过程,命令仅打印并不运行", - "-ipv6 使用IPv6测速", + "-para(llel) 并行测速 (默认的顺序测速更有参考意义)", "-local 仅对本项目而非全局换源 (通过ls 查看支持情况)", + "-ipv6 使用IPv6测速", "-en(glish) 使用英文输出", "-no-color 无颜色输出\n", @@ -156,8 +157,9 @@ Chsrc_Usage_English[] = { "Options:", "-dry Dry Run. Simulate the source changing process, command only prints, not run", - "-ipv6 Speed measurement using IPv6", + "-para(llel) Measure velocity in parallel", "-local Change source only for this project rather than globally (Via `ls `)", + "-ipv6 Speed measurement using IPv6", "-en(glish) Output in English", "-no-color Output without color\n", @@ -598,6 +600,12 @@ main (int argc, char const *argv[]) { CliOpt_DryRun = true; } + else if (xy_streql (argv[i], "-para") + || xy_streql (argv[i], "-parallel") + || xy_streql (argv[i], "-paralel")) + { + CliOpt_Parallel = true; + } else if (xy_streql (argv[i], "-no-color") || xy_streql (argv[i], "-no-colour")) { CliOpt_NoColor = true;