Skip to content

Commit

Permalink
spdlog同步异步测试,但是还是有点问题,异步比同步慢
Browse files Browse the repository at this point in the history
  • Loading branch information
home committed May 5, 2024
1 parent 88cbcbf commit d58f1b0
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 37 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ file(GLOB FILES
)
message("test: ${FILES}")
add_executable(ZRTC main.cpp ${FILES})
find_package(glog CONFIG REQUIRED)
target_link_libraries(ZRTC PRIVATE glog::glog)

add_subdirectory(test)

Expand Down
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
1.异步日志 读写测试(使用gtest
1.异步日志 读写测试(使用spdlog
2.编写单元测试
3.cicd
8 changes: 8 additions & 0 deletions logs/basic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[2024-05-05 17:15:07.428] [sbasic_logger] [info] Hello world
[2024-05-05 17:21:01.574] [sbasic_logger] [info] Hello world
[2024-05-05 17:22:24.249] [sbasic_logger] [info] Hello world
[2024-05-05 17:22:52.744] [sbasic_logger] [info] Hello world
[2024-05-05 17:26:44.928] [sbasic_logger] [info] Hello world
[2024-05-05 17:27:45.337] [sbasic_logger] [info] Hello world
[2024-05-05 17:28:27.521] [sbasic_logger] [info] Hello world
[2024-05-05 17:30:23.957] [sbasic_logger] [info] Hello world
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//

#include <iostream>
#include <glog/logging.h>
// #include <glog/logging.h>
int main(int argc, char** argv) {
// printf("asd");
// google::InitGoogleLogging(argv[0]);
Expand Down
81 changes: 51 additions & 30 deletions test/AsyncLog.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include <glog/logging.h>
#include <gtest/gtest.h>

#include <chrono>
#include <filesystem>
#include <random>

#include "spdlog/async.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/spdlog.h"

TEST(HelloWorldTest, BasicTest) {
// Test code goes here
// You can use ASSERT_* or EXPECT_* macros to perform assertions
Expand All @@ -13,35 +17,52 @@ TEST(HelloWorldTest, BasicTest) {

// 创建要写入的文件夹并指定要写入的日志文件名称
TEST(WriteFile, BasicTest) {
std::string log_path = "../../log";
FLAGS_log_dir = log_path;
printf("test\n");
google::SetLogSymlink(google::GLOG_INFO, "Info");
google::SetLogSymlink(google::GLOG_WARNING, "Warning");
google::SetLogSymlink(google::GLOG_ERROR, "Error");
google::InitGoogleLogging("WriteLog");
if (!std::filesystem::exists(log_path)) {
if (std::filesystem::create_directories(log_path)) {
LOG(INFO) << "创建成功";
}
} else {
LOG(WARNING) << "文件夹" + log_path + "已经存在,不用创建";
spdlog::info("Test: {}", 1);

try {
// 在logs/basic.txt中写下Hello world
auto my_logger =
spdlog::basic_logger_mt("sbasic_logger", "./logs/basic.txt");
my_logger->info("Hello {}", "world");
} catch (const spdlog::spdlog_ex& ex) {
std::cout << "Log initialization failed: " << ex.what() << std::endl;
}
LOG(INFO) << "file";
// Most flags work immediately after updating values.
FLAGS_logtostderr = 1;
LOG(INFO) << "stderr";
FLAGS_logtostderr = 0;
// This won’t change the log destination. If you want to set this
// value, you should do this before google::InitGoogleLogging .
// FLAGS_log_dir = "/some/log/directory";
LOG(INFO) << "the same file";

for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
LOG(INFO) << "Test Write INFO" << i;
} else {
LOG(ERROR) << "Test Write ERROR" << i;
}
}

// 异步写入文件
TEST(AsyncWrite, BasicTest) {
auto start = std::chrono::system_clock::now();
spdlog::init_thread_pool(10000, 10);
auto file_logger = spdlog::rotating_logger_mt<spdlog::async_factory>(
"Async", "./logs/AsyncLogs", 1024 * 1024 * 5, 100);
int i = 0;
file_logger->set_level(spdlog::level::debug);
while (i < 1000000) {
file_logger->debug("Async message #{}", i);
i++;
}
auto end = std::chrono::system_clock::now();
auto diff = end - start;
file_logger->info("Async Duration: {}", diff.count());
spdlog::drop_all();
}

// 同步写入文件
TEST(SyncWrite, BasicTest) {
auto start = std::chrono::system_clock::now();

auto file_logger =
spdlog::rotating_logger_mt("Sync", "SyncLogs", 1024 * 1024 * 5, 100);
file_logger->set_level(spdlog::level::debug);
int i = 0;
while (i < 1000000) {
file_logger->debug("Async message #{}", i);
i++;
}

auto end = std::chrono::system_clock::now();
auto diff = end - start;
file_logger->info("Sync Duration: {}", diff.count());
// spdlog::info("Sync Duration: {}", diff.count());
spdlog::drop_all();
}
4 changes: 2 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
add_executable(AsyncLogTest AsyncLog.cpp)
find_package(glog CONFIG REQUIRED)
target_link_libraries(AsyncLogTest PRIVATE glog::glog)
find_package(spdlog CONFIG REQUIRED)
target_link_libraries(AsyncLogTest PRIVATE spdlog::spdlog_header_only)

enable_testing()
find_package(GTest CONFIG REQUIRED)
Expand Down
2 changes: 1 addition & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"version": "0.15.2",
"dependencies": [
"gtest",
"glog"
"spdlog"
]
}

0 comments on commit d58f1b0

Please sign in to comment.