From ec8e47f8315fdc092c3ca7d4f21bd3513730ff57 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Fri, 12 Sep 2025 14:08:46 +0800 Subject: [PATCH] tests/test_fast_buffer.c support all types --- src/tests/test_fast_buffer.c | 162 ++++++++++++++++++++--------------- src/tests/test_logger.c | 12 ++- 2 files changed, 103 insertions(+), 71 deletions(-) diff --git a/src/tests/test_fast_buffer.c b/src/tests/test_fast_buffer.c index 956fd7c..aba04e7 100644 --- a/src/tests/test_fast_buffer.c +++ b/src/tests/test_fast_buffer.c @@ -27,7 +27,8 @@ #include "fastcommon/sched_thread.h" typedef enum { - TEST_TYPE_ITOA = 1, + TEST_TYPE_NONE = 0, + TEST_TYPE_ITOA, TEST_TYPE_FTOA, TEST_TYPE_INT2HEX, TEST_TYPE_APPEND @@ -161,7 +162,7 @@ static inline int cache_binlog_filename_by_append( static void usage(const char *program) { - fprintf(stderr, "Usage: %s [-t {itoa | ftoa | int2hex | append}]\n", + fprintf(stderr, "Usage: %s [-t {itoa | ftoa | int2hex | append | all}]\n", program); } @@ -177,6 +178,8 @@ int main(int argc, char *argv[]) int result; TestType test_type = TEST_TYPE_ITOA; + TestType type_start; + TestType type_last; uint64_t id = 123456; double d = 123.456; int ch; @@ -191,7 +194,7 @@ int main(int argc, char *argv[]) char full_filename1[PATH_MAX]; char full_filename2[PATH_MAX]; char buff[32] = {0}; - char *caption = "itoa"; + char *caption; log_init(); g_current_time = time(NULL); @@ -201,6 +204,7 @@ int main(int argc, char *argv[]) return result; } + type_start = type_last = TEST_TYPE_ITOA; while ((ch=getopt(argc, argv, "ht:")) != -1) { switch (ch) { case 'h': @@ -208,17 +212,16 @@ int main(int argc, char *argv[]) return 0; case 't': if (strcasecmp(optarg, "itoa") == 0) { - test_type = TEST_TYPE_ITOA; - caption = "itoa"; + type_start = type_last = TEST_TYPE_ITOA; } else if (strcasecmp(optarg, "ftoa") == 0) { - test_type = TEST_TYPE_FTOA; - caption = "ftoa"; + type_start = type_last = TEST_TYPE_FTOA; } else if (strcasecmp(optarg, "int2hex") == 0) { - test_type = TEST_TYPE_INT2HEX; - caption = "int2hex"; + type_start = type_last = TEST_TYPE_INT2HEX; } else if (strcasecmp(optarg, "append") == 0) { - test_type = TEST_TYPE_APPEND; - caption = "append"; + type_start = type_last = TEST_TYPE_APPEND; + } else if (strcasecmp(optarg, "all") == 0) { + type_start = TEST_TYPE_ITOA; + type_last = TEST_TYPE_APPEND; } else { fprintf(stderr, "invalid type: %s\n", optarg); return EINVAL; @@ -230,79 +233,98 @@ int main(int argc, char *argv[]) } } - if (test_type == TEST_TYPE_APPEND) { - memset(&record, 0, sizeof(record)); - record.op_type = 'C'; - record.slice_type = DA_SLICE_TYPE_FILE; - record.storage.version = 1111; - record.oid = 9007211709265131LL; - record.fid = 0; - record.storage.trunk_id = 61; - record.storage.length = 62; - record.storage.offset = 12345; - record.storage.size = 64; - } + for (test_type=type_start; test_type<=type_last; test_type++) { + if (test_type == TEST_TYPE_APPEND) { + memset(&record, 0, sizeof(record)); + record.op_type = 'C'; + record.slice_type = DA_SLICE_TYPE_FILE; + record.storage.version = 1111; + record.oid = 9007211709265131LL; + record.fid = 0; + record.storage.trunk_id = 61; + record.storage.length = 62; + record.storage.offset = 12345; + record.storage.size = 64; + } + + start_time_us = get_current_time_us(); + for (i=0; i 0) { + ratio = (double)sprintf_time_us / (double)convert_time_us; + } else { + ratio = 1.0; + } - start_time_us = get_current_time_us(); - for (i=0; i 0) { - ratio = (double)sprintf_time_us / (double)convert_time_us; - } else { - ratio = 1.0; - } - - printf("sprintf time: %d ms, %s time: %d ms, " - "sprintf time / %s time: %d%%\n", - sprintf_time_us / 1000, caption, convert_time_us / 1000, - caption, (int)(ratio * 100.00)); fast_buffer_destroy(&buffer); return 0; diff --git a/src/tests/test_logger.c b/src/tests/test_logger.c index 7d33896..1dc5f98 100644 --- a/src/tests/test_logger.c +++ b/src/tests/test_logger.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "fastcommon/logger.h" int main(int argc, char *argv[]) @@ -35,6 +36,15 @@ int main(int argc, char *argv[]) log_take_over_stderr(); log_take_over_stdout(); log_set_compress_log_flags(LOG_COMPRESS_FLAGS_ENABLED | LOG_COMPRESS_FLAGS_NEW_THREAD); + + log_set_filename("/opt/fastcfs/fuse/test.log"); + + + if (flock(g_log_context.log_fd, LOCK_EX) != 0) { + logError("flock fail"); + } + + flock(g_log_context.log_fd, LOCK_UN); printf("sizeof(LogContext): %d, time_precision: %d, compress_log_flags: %d, " "use_file_write_lock: %d\n", (int)sizeof(LogContext), @@ -46,7 +56,7 @@ int main(int argc, char *argv[]) "by log_it_ex, timestamp: %d", (int)time(NULL)); len = sprintf(buff, "this is by log_it_ex1, " - "timestamp: %d", (int)time(NULL)); + "timestamp: %ld", (long)time(NULL)); log_it_ex1(&g_log_context, LOG_INFO, buff, len); return 0;