Add -para option

This commit is contained in:
Aoran Zeng 2024-09-04 01:03:53 +08:00
parent ff8f6127fc
commit f62962b3cc
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
2 changed files with 86 additions and 43 deletions

View File

@ -27,6 +27,7 @@ bool CliOpt_Locally = false;
bool CliOpt_InEnglish = false; bool CliOpt_InEnglish = false;
bool CliOpt_DryRun = false; bool CliOpt_DryRun = false;
bool CliOpt_NoColor = false; bool CliOpt_NoColor = false;
bool CliOpt_Parallel = false;
/** /**
* -local ** * -local **
@ -408,9 +409,14 @@ to_human_readable_speed (double speed)
* char * (*)(const char*) * char * (*)(const char*)
*/ */
void * 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")) 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; const char *msg = CliOpt_InEnglish ? src.mirror->abbr : src.mirror->name;
measure_msgs[i] = xy_strjoin (3, " - ", msg, " ... "); measure_msgs[i] = xy_strjoin (3, " - ", msg, " ... ");
printf ("%s", measure_msgs[i]); printf ("%s", measure_msgs[i]);
say ("");
// fflush (stdout); if (CliOpt_Parallel)
say (""); /* 并行时直接显示下一测速状态行 */
else
fflush (stdout);
char *url_ = xy_strdup (url); char *url_ = xy_strdup (url);
// say (url);
int ret = pthread_create (&threads[i], NULL, measure_speed, url_); if (CliOpt_Parallel)
if (ret!=0)
{ {
chsrc_error ("Unable to measure speed\n"); int ret = pthread_create (&threads[i], NULL, measure_speed_for_url, url_);
exit (Exit_UserCause); if (ret!=0)
{
chsrc_error ("Unable to measure speed\n");
exit (Exit_UserCause);
}
else
{
get_measured[i] = true;
get_measured_n += 1;
}
} }
else else
{ {
get_measured[i] = true; char *curl_result = measure_speed_for_url (url_);
get_measured_n += 1; double speed = parse_and_say_curl_result (curl_result);
whole_speeds[i+grp_cursor_in_whole] = speed;
} }
} }
} }
/* 一组汇总 */ if (CliOpt_Parallel)
char **curl_results = xy_malloc0 (sizeof(char *) * size);
for (int i=0; i<size; i++)
{ {
if (get_measured[i]==true) /* 一组汇总 */
pthread_join (threads[i], (void *)&curl_results[i]); char **curl_results = xy_malloc0 (sizeof(char *) * size);
} for (int i=0; i<size; i++)
for (int i=0; i<get_measured_n; i++)
printf("\033[A\033[2K");
for (int i=0; i<size; i++)
{
if (get_measured[i]==true)
{ {
printf ("%s", measure_msgs[i]); if (get_measured[i]==true)
double speed = parse_and_say_curl_result (curl_results[i]); pthread_join (threads[i], (void *)&curl_results[i]);
whole_speeds[i+grp_cursor_in_whole] = speed;
} }
}
/* 汇总结束 */ for (int i=0; i<get_measured_n; i++)
printf("\033[A\033[2K");
for (int i=0; i<size; i++)
{
if (get_measured[i]==true)
{
printf ("%s", measure_msgs[i]);
double speed = parse_and_say_curl_result (curl_results[i]);
whole_speeds[i+grp_cursor_in_whole] = speed;
}
}
/* 汇总结束 */
} /* End of if Parallel*/
} }
@ -588,7 +609,14 @@ int
auto_select_ (SourceInfo *sources, size_t size, const char *target_name) auto_select_ (SourceInfo *sources, size_t size, const char *target_name)
{ {
{ {
char *msg = CliOpt_InEnglish ? "Auto speed measuring" : "自动测速中"; char *msg = NULL;
if (CliOpt_Parallel)
msg = CliOpt_InEnglish ? "Measuring speed in parallel. It is recommended to use the default sequential measurement for more referential results"
: "即将并行测速,建议使用默认的顺序测速以获得更具参考意义的结果";
else
msg = CliOpt_InEnglish ? "Measuring speed in sequence" : "顺序测速中";
xy_log_brkt (App_Name, bdpurple (CliOpt_InEnglish ? "MEASURE" : "测速"), msg); xy_log_brkt (App_Name, bdpurple (CliOpt_InEnglish ? "MEASURE" : "测速"), msg);
say (""); say ("");
} }
@ -622,20 +650,27 @@ auto_select_ (SourceInfo *sources, size_t size, const char *target_name)
/* 总测速记录值 */ /* 总测速记录值 */
double speed_records[size]; double speed_records[size];
// 跳过 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;
// 跳过 upstream // 跳过 upstream
speed_records[0] = 0.0; speed_records[0] = 0.0;
int grp_cursor = 1; int grp_cursor = 1;
for (int i=0; i<ngroup; i++, grp_cursor+=group_size) if (CliOpt_Parallel)
measure_speed_in_group (group_size, sources, speed_records, grp_cursor); {
if (rest > 0) // 跳过 upstream
measure_speed_in_group (rest, sources, speed_records, grp_cursor); 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<ngroup; i++, grp_cursor+=group_size)
measure_speed_in_group (group_size, sources, speed_records, grp_cursor);
if (rest > 0)
measure_speed_in_group (rest, sources, speed_records, grp_cursor);
}
else
{
measure_speed_in_group (size-1, sources, speed_records, grp_cursor);
}
say (""); say ("");

View File

@ -13,7 +13,7 @@
* | Terrasse <terrasse@qq.com> * | Terrasse <terrasse@qq.com>
* | * |
* Created On : <2023-08-28> * Created On : <2023-08-28>
* Last Modified : <2024-08-29> * Last Modified : <2024-09-04>
* *
* chsrc: Change Source * chsrc: Change Source
* ------------------------------------------------------------*/ * ------------------------------------------------------------*/
@ -124,8 +124,9 @@ Chsrc_Usage[] = {
"选项:", "选项:",
"-dry Dry Run模拟换源过程命令仅打印并不运行", "-dry Dry Run模拟换源过程命令仅打印并不运行",
"-ipv6 使用IPv6测速", "-para(llel) 并行测速 (默认的顺序测速更有参考意义)",
"-local 仅对本项目而非全局换源 (通过ls <target>查看支持情况)", "-local 仅对本项目而非全局换源 (通过ls <target>查看支持情况)",
"-ipv6 使用IPv6测速",
"-en(glish) 使用英文输出", "-en(glish) 使用英文输出",
"-no-color 无颜色输出\n", "-no-color 无颜色输出\n",
@ -156,8 +157,9 @@ Chsrc_Usage_English[] = {
"Options:", "Options:",
"-dry Dry Run. Simulate the source changing process, command only prints, not run", "-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 <target>`)", "-local Change source only for this project rather than globally (Via `ls <target>`)",
"-ipv6 Speed measurement using IPv6",
"-en(glish) Output in English", "-en(glish) Output in English",
"-no-color Output without color\n", "-no-color Output without color\n",
@ -598,6 +600,12 @@ main (int argc, char const *argv[])
{ {
CliOpt_DryRun = true; 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")) else if (xy_streql (argv[i], "-no-color") || xy_streql (argv[i], "-no-colour"))
{ {
CliOpt_NoColor = true; CliOpt_NoColor = true;