Debuging test_speed()

This commit is contained in:
Aoran Zeng 2023-09-02 21:36:54 +08:00
parent 4a6f24a258
commit 2c62dedd5c
2 changed files with 52 additions and 17 deletions

53
chsrc.c
View File

@ -55,6 +55,7 @@ to_human_readable_speed (double speed)
} }
char* buf = xy_malloc0(64); char* buf = xy_malloc0(64);
sprintf(buf, "%.2f %s", speed, scale[i]); sprintf(buf, "%.2f %s", speed, scale[i]);
return buf;
} }
@ -62,37 +63,49 @@ to_human_readable_speed (double speed)
* https://github.com/mirrorz-org/oh-my-mirrorz/blob/master/oh-my-mirrorz.py * https://github.com/mirrorz-org/oh-my-mirrorz/blob/master/oh-my-mirrorz.py
* C语言 * C语言
* *
* @return 0 * @return -1
*/ */
double double
test_speed (char* url) test_speed (char* url)
{ {
char* curl_cmd, *devnull = NULL; char* curl_cmd = xy_strjoin(4, "curl -qs -o ", xy_os_devnull, " -w \"%{http_code} %{speed_download}\" -m8 -A chsrc/" Chsrc_Version
if (xy_on_windows) " ", url);
devnull = "nul";
else
devnull = "/dev/null";
curl_cmd = xy_strjoin(4, "curl -qs -o ", devnull, "-w '%{http_code} %{speed_download}' -m8 -A chsrc/" Chsrc_Version // xy_info (xy_2strjoin("chsrc: 测速 ", url));
"(+https://gitee.com/RubyMetric/chsrc)", url); puts(curl_cmd);
system(curl_cmd);
/*
FILE* fp = popen(curl_cmd, "r"); FILE* fp = popen(curl_cmd, "r");
char buf[64] = {0}; char buf[64] = {0};
fgets(buf, 64, fp); while(NULL!=fgets(buf, 64, fp));
fclose(fp);
puts("hello?");
puts(buf);
// 如果尾部有换行,删除
char* last_lf = strrchr(buf, '\n');
if (last_lf) *last_lf = '\0';
char* split = strchr(buf, ' '); char* split = strchr(buf, ' ');
*split = '\0'; printf("diff = %d\n", split-buf);
if (split) *split = '\0';
puts(buf);
puts(split+1);
int http_code = atoi(buf); int http_code = atoi(buf);
double speed = atof(split+1); double speed = atof(split+1);
printf("http_code = %d, speed = %f\n", http_code, speed);
char* speedstr = to_human_readable_speed(speed); char* speedstr = to_human_readable_speed(speed);
puts(url);
if (200!=http_code) { if (200!=http_code) {
xy_warn (xy_2strjoin("HTTP码 = ", buf)); xy_warn (xy_2strjoin("HTTP码 = ", buf));
} }
puts(xy_2strjoin("速度 = ", speedstr)); puts(xy_2strjoin("速度 = ", speedstr));
return speed;
*/
return 0;
} }
@ -317,8 +330,6 @@ dblary_maxidx(double* array, int size)
void void
pl_ruby_cesu (char* option) pl_ruby_cesu (char* option)
{ {
char* url = "";
size_t size = pl_ruby_sources_n; size_t size = pl_ruby_sources_n;
source_info* sources = pl_ruby_sources; source_info* sources = pl_ruby_sources;
double speeds[size]; double speeds[size];
@ -327,6 +338,7 @@ pl_ruby_cesu (char* option)
source_info src = sources[i]; source_info src = sources[i];
const char* baseurl = src.url; const char* baseurl = src.url;
char* testurl = xy_2strjoin(baseurl, "gems/nokogiri-1.15.0-java.gem"); char* testurl = xy_2strjoin(baseurl, "gems/nokogiri-1.15.0-java.gem");
puts(testurl);
double speed = test_speed (testurl); double speed = test_speed (testurl);
speeds[i] = speed; speeds[i] = speed;
} }
@ -924,8 +936,15 @@ get_target (const char* input, int code)
print_supported_sources_for_target (target->sources); print_supported_sources_for_target (target->sources);
} }
else if (Target_Cesu_Source==code) { else if (Target_Cesu_Source==code) {
if (target->cesufn) target->cesufn(""); if (!target->cesufn)
else xy_error (xy_strjoin(3, "chsrc: 暂未对", input, "实现cesu功能欢迎贡献")); xy_error (xy_strjoin(3, "chsrc: 暂未对", input, "实现cesu功能欢迎贡献"));
else {
char* check_cmd = xy_str_to_quietcmd("curl --version");
bool exist_b = does_the_program_exist (check_cmd, "curl");
if (!exist_b) xy_error ("chsrc: 没有curl命令无法测速");
else target->cesufn("");
return true;
}
} }
return true; return true;
} }

View File

@ -26,6 +26,8 @@
static bool xy_on_macos = false; static bool xy_on_macos = false;
static bool xy_on_bsds = false; static bool xy_on_bsds = false;
static char* xy_os_devnull = "nul";
#include <windows.h> #include <windows.h>
#define xy_useutf8() SetConsoleOutputCP(65001) #define xy_useutf8() SetConsoleOutputCP(65001)
@ -35,6 +37,8 @@
static bool xy_on_macos = false; static bool xy_on_macos = false;
static bool xy_on_bsds = false; static bool xy_on_bsds = false;
static char* xy_os_devnull = "/dev/null"
#define xy_useutf8() #define xy_useutf8()
#endif #endif
@ -212,4 +216,16 @@ xy_streql(const char* str1, const char* str2) {
return strcmp(str1, str2) == 0 ? true : false; return strcmp(str1, str2) == 0 ? true : false;
} }
char*
xy_str_to_quietcmd (const char* cmd)
{
char* ret = NULL;
#ifdef _WIN32
ret = xy_2strjoin (cmd, " >nul 2>nul");
#else
ret = xy_2strjoin (cmd, " 1>/dev/null 2>&1");
#endif
}
#endif #endif