test_fast_buffer.c support int2hex

use_iouring
YuQing 2025-09-11 12:07:45 +08:00
parent 2f75958a4a
commit d9d6255621
2 changed files with 29 additions and 15 deletions

View File

@ -29,7 +29,8 @@
typedef enum { typedef enum {
TEST_TYPE_ITOA = 1, TEST_TYPE_ITOA = 1,
TEST_TYPE_FTOA, TEST_TYPE_FTOA,
TEST_TYPE_MIXED TEST_TYPE_INT2HEX,
TEST_TYPE_APPEND
} TestType; } TestType;
typedef enum { typedef enum {
@ -160,7 +161,7 @@ static inline int cache_binlog_filename_by_append(
static void usage(const char *program) static void usage(const char *program)
{ {
fprintf(stderr, "Usage: %s [-t {itoa | ftoa | mixed}]\n", fprintf(stderr, "Usage: %s [-t {itoa | ftoa | int2hex | append}]\n",
program); program);
} }
@ -180,8 +181,9 @@ int main(int argc, char *argv[])
double d = 123.456; double d = 123.456;
int ch; int ch;
int i; int i;
int len;
int64_t start_time_us; int64_t start_time_us;
int append_time_us; int convert_time_us;
int sprintf_time_us; int sprintf_time_us;
double ratio; double ratio;
FastBuffer buffer; FastBuffer buffer;
@ -211,8 +213,11 @@ int main(int argc, char *argv[])
} else if (strcasecmp(optarg, "ftoa") == 0) { } else if (strcasecmp(optarg, "ftoa") == 0) {
test_type = TEST_TYPE_FTOA; test_type = TEST_TYPE_FTOA;
caption = "ftoa"; caption = "ftoa";
} else if (strcasecmp(optarg, "mixed") == 0) { } else if (strcasecmp(optarg, "int2hex") == 0) {
test_type = TEST_TYPE_MIXED; test_type = TEST_TYPE_INT2HEX;
caption = "int2hex";
} else if (strcasecmp(optarg, "append") == 0) {
test_type = TEST_TYPE_APPEND;
caption = "append"; caption = "append";
} else { } else {
fprintf(stderr, "invalid type: %s\n", optarg); fprintf(stderr, "invalid type: %s\n", optarg);
@ -225,7 +230,7 @@ int main(int argc, char *argv[])
} }
} }
if (test_type == TEST_TYPE_MIXED) { if (test_type == TEST_TYPE_APPEND) {
memset(&record, 0, sizeof(record)); memset(&record, 0, sizeof(record));
record.op_type = 'C'; record.op_type = 'C';
record.slice_type = DA_SLICE_TYPE_FILE; record.slice_type = DA_SLICE_TYPE_FILE;
@ -241,7 +246,7 @@ int main(int argc, char *argv[])
start_time_us = get_current_time_us(); start_time_us = get_current_time_us();
for (i=0; i<LOOP; i++) { for (i=0; i<LOOP; i++) {
switch (test_type) { switch (test_type) {
case TEST_TYPE_MIXED: case TEST_TYPE_APPEND:
cache_binlog_filename_by_sprintf(data_path, subdir_name, cache_binlog_filename_by_sprintf(data_path, subdir_name,
subdirs, ++id, full_filename1, sizeof(full_filename1)); subdirs, ++id, full_filename1, sizeof(full_filename1));
fast_buffer_reset(&buffer); fast_buffer_reset(&buffer);
@ -253,6 +258,9 @@ int main(int argc, char *argv[])
case TEST_TYPE_FTOA: case TEST_TYPE_FTOA:
sprintf(buff, "%.2f", d); sprintf(buff, "%.2f", d);
break; break;
case TEST_TYPE_INT2HEX:
sprintf(buff, "%x", (int)id);
break;
default: default:
break; break;
} }
@ -262,33 +270,38 @@ int main(int argc, char *argv[])
start_time_us = get_current_time_us(); start_time_us = get_current_time_us();
for (i=0; i<LOOP; i++) { for (i=0; i<LOOP; i++) {
switch (test_type) { switch (test_type) {
case TEST_TYPE_MIXED: case TEST_TYPE_APPEND:
cache_binlog_filename_by_append(data_path, subdir_name, cache_binlog_filename_by_append(data_path, subdir_name,
subdirs, ++id, full_filename2, sizeof(full_filename2)); subdirs, ++id, full_filename2, sizeof(full_filename2));
fast_buffer_reset(&buffer); fast_buffer_reset(&buffer);
log_pack_by_append(&record, &buffer, have_extra_field); log_pack_by_append(&record, &buffer, have_extra_field);
break; break;
case TEST_TYPE_ITOA: case TEST_TYPE_ITOA:
fc_itoa(id, buff); len = fc_itoa(id, buff);
*(buff + len) = '\0';
break; break;
case TEST_TYPE_FTOA: case TEST_TYPE_FTOA:
fc_ftoa(d, 2, buff); len = fc_ftoa(d, 2, buff);
*(buff + len) = '\0';
break;
case TEST_TYPE_INT2HEX:
int2hex(id, buff, 0);
break; break;
default: default:
break; break;
} }
} }
append_time_us = (get_current_time_us() - start_time_us); convert_time_us = (get_current_time_us() - start_time_us);
if (append_time_us > 0) { if (convert_time_us > 0) {
ratio = (double)sprintf_time_us / (double)append_time_us; ratio = (double)sprintf_time_us / (double)convert_time_us;
} else { } else {
ratio = 1.0; ratio = 1.0;
} }
printf("sprintf time: %d ms, %s time: %d ms, " printf("sprintf time: %d ms, %s time: %d ms, "
"sprintf time / %s time: %d%%\n", "sprintf time / %s time: %d%%\n",
sprintf_time_us / 1000, caption, append_time_us / 1000, sprintf_time_us / 1000, caption, convert_time_us / 1000,
caption, (int)(ratio * 100.00)); caption, (int)(ratio * 100.00));
fast_buffer_destroy(&buffer); fast_buffer_destroy(&buffer);

View File

@ -32,6 +32,7 @@ int main(int argc, char *argv[])
int fd; int fd;
int result; int result;
int n; int n;
int i;
char buf[1024]; char buf[1024];
if (argc < 2) { if (argc < 2) {
@ -69,7 +70,7 @@ int main(int argc, char *argv[])
return errno != 0 ? errno : EIO; return errno != 0 ? errno : EIO;
} }
for (int i=0; i<5; i++) { for (i=0; i<5; i++) {
if (lseek(fd, 0, SEEK_SET) < 0) { if (lseek(fd, 0, SEEK_SET) < 0) {
logError("file: "__FILE__", line: %d, " \ logError("file: "__FILE__", line: %d, " \
"lseek file %s fail, " \ "lseek file %s fail, " \