欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > tklog0.2.4—Rust高性能日志库,性能显著提高

tklog0.2.4—Rust高性能日志库,性能显著提高

2024/10/25 15:39:04 来源:https://blog.csdn.net/wxd_001/article/details/143142344  浏览:    关键词:tklog0.2.4—Rust高性能日志库,性能显著提高
tklog是rust高性能结构化日志库,支持同步日志,异步日志,支持自定义日志的输出格式,支持按时间,按文件大小分割日志文件,支持日志文件压缩备份,支持官方日志库标准API,支持mod独立参数设置,支持日志level独立参数设置
  1. 简介
  2. Github地址
  3. 仓库地址
  4. 《rust日志库性能压测 — log4rs + tracing + tklog》

v0.2.4 版本更新

该版本主要更新为:

  • 在v0.2.3更新内容的基础上,新增支持日志内容的自定义格式化。通过  set_body_fmt 自定义输出日志内容。
  • 优化性能,性能提高30%-50%。
通过 set_body_fmt 函数设置日志标识与时间格式
示例: 通过set_body_fmt 设置不同日志级别输出不用颜色的日志
fn testlog() {LOG.set_level(LEVEL::Trace).set_attr_format(|fmt| {fmt.set_body_fmt(|level, body| {//处理body的末尾换行符let trimmed_body = if body.ends_with('\n') {format!("{}{}", body.as_str()[..body.len() - 1].to_string(), "\x1b[0m\n")} else {format!("{}{}", body, "\x1b[0m\n")};match level {LEVEL::Trace => format!("{}{}", "\x1b[34m", trimmed_body), //蓝色LEVEL::Debug => format!("{}{}", "\x1b[36m", trimmed_body), //青色LEVEL::Info => format!("{}{}", "\x1b[32m", trimmed_body),  //绿色LEVEL::Warn => format!("{}{}", "\x1b[33m", trimmed_body),  //黄色LEVEL::Error => format!("{}{}", "\x1b[31m", trimmed_body), //红色LEVEL::Fatal => format!("{}{}", "\x1b[41m", trimmed_body), //背景红LEVEL::Off => "".to_string(),}});});trace!("trace!", "this is sync log");debug!("debug!", "this is sync log");info!("info!", "this is sync log");warn!("warn!", "this is sync log");error!("error!", "this is sync log");fatal!("fata!", "this is sync log");thread::sleep(Duration::from_secs(1))
}
执行结果:


性能压测数据

log_benchmark
测试编号最小时间 (µs)最大时间 (µs)平均时间 (µs)变化百分比 (%)p 值
12.39492.49412.4428-0.5586%0.14
22.39922.46322.4307-12.388%0.00
32.45252.56322.5059-10.548%0.00
42.56502.67752.6194-3.5311%0.79
mod_benchmark
测试编号最小时间 (µs)最大时间 (µs)平均时间 (µs)变化百分比 (%)p 值
12.19462.27182.2325-2.5723%0.96
22.21262.29202.2508-11.895%0.00
32.26032.36932.3113-12.539%0.00
42.49082.64402.5655-1.3617%0.29
2. 总结统计
  • log_benchmark
    • 最小时间: 2.3949 µs
    • 最大时间: 2.6775 µs
    • 平均时间: 2.5160 µs
    • 变化幅度: 从 -0.5586% 到 -12.388%
    • p 值: 大部分测试显著性强(p < 0.05)。
  • mod_benchmark
    • 最小时间: 2.1946 µs
    • 最大时间: 2.6440 µs
    • 平均时间: 2.3430 µs
    • 变化幅度: 从 -2.5723% 到 -12.539%
    • p 值: 大部分测试显著性强(p < 0.05)。
性能统计数据(每次响应时间)
  1. 最小时间: 2.1946 µs
  2. 最大时间: 2.6775 µs
  3. 平均时间: 2.3946 µs


性能优化过程中的火焰图变化

可以看出,tklog的性能在优化过程中有效提高。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com