记录tklog压测结果


本次记录数据,主要测试tklog用户反馈的问题,查看tklog是否存在不打印日志时,出现消耗性能的情况,即打不打印前后性能一致。

文件1:main.rs

use actix_web::{web, App, HttpServer, Responder};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    inittk().await;
    initlog4rs();
    HttpServer::new(|| {
        App::new()
            .route("/tk", web::get().to(tkhandle))
            .route("/rs", web::get().to(rshandle))
            .route("/", web::get().to(index_handle))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

async fn index_handle() -> impl Responder {
    "Hello, world!"
}

async fn tkhandle() -> impl Responder {
    tklog::async_debug!(" tkhandle request to >>>>>>>>>>>>>");
    "Hello, tkhandle!"
}

async fn rshandle() -> impl Responder {
    log::debug!(" rshandle request to >>>>>>>>>>>>>");
    "Hello, rshandle!"
}

async fn inittk() {
    tklog::ASYNC_LOG
        .set_console(false)
        .set_formatter("{level}{time} {file} | {message}\n")
        .set_cutmode_by_size("actixlog.log", 100 << 20, 0, false)
        .await;
}

fn initlog4rs() {
    log4rs::init_file("log4rs.yaml", Default::default()).unwrap();
}

文件2:Cargo.toml

[package]
name = "logtest"
version = "0.1.0"
edition = "2021"

[dependencies]
tklog= "0.2.7"
log = "0.4.21"
actix-web = "4.0"
log4rs = "1.3.0"

文件3:log4rs.yaml

refresh_rate: 30 seconds

appenders:
  requests:
    kind: rolling_file
    path: "log4rs.log"
    encoder:
      pattern: "{l}{d(%Y-%m-%d %H:%M:%S)}{file}:{line}|{m}{n}"
    policy:
      kind: compound
      trigger:
        kind: size
        limit: 100 MB
      roller:
        kind: fixed_window
        base: 1
        count: 10
        pattern: "log4rs-{}.log"

root:
  level: debug
  appenders:
    - requests

压测环境:
  • 系统:Ubuntu 24.04.1 LTS
  • cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz


1. 启动,设置tklog的日志级别为debug

RUST_LOG=debug cargo run

压测结果:

http://127.0.0.1:8080/


http://127.0.0.1:8080/tk


http://127.0.0.1:8080/rs


结果:启动tklog日志打印,并打印日志,

压测没有打印日志的接口:http://127.0.0.1:8080/   (QPS)RPS为: 66782  

tklog打印接口:http://127.0.0.1:8080/tk    (QPS)RPS为: 41368

log4rs打印接口:http://127.0.0.1:8080/rs    (QPS)RPS为: 4111

可以看到,打印日志与不打印日志的接口,数据差距较大,同时,tklog在linux环境中,性能远远高于log4rs,相距10倍以上


2. 启动,设置tklog的日志级别为info,即tklog接口中debug宏应该不打印日志

RUST_LOG=info cargo run

压测结果:

http://127.0.0.1:8080/tk

结果

tklog打印接口:http://127.0.0.1:8080/tk    (QPS)RPS为: 66833

可以看到:当tklog的日志级别设置高于打印宏时,宏不打印日志,打印结果与   http://127.0.0.1:8080/ 接口 66782 基本一致,在正常的波动范围内,没有出现打不打印压测结果不变的情况