8 个版本
0.2.1 | 2020 年 12 月 20 日 |
---|---|
0.2.0 | 2020 年 12 月 12 日 |
0.1.5 | 2020 年 10 月 18 日 |
0.1.2 | 2020 年 9 月 6 日 |
0.1.1 | 2020 年 5 月 14 日 |
#846 在 Rust 模式
每月 386 次下载
在 cron_clock 中使用
10KB
219 行
Rust timed
宏,快速分析您的程序
✅ 支持 async
✅ 支持 main
✅ 自定义打印机,如 println!
,info!
或您自己的函数。
✅ 使用 chrome 追踪分析程序,构建火焰图。
✅ 新功能!显示函数之间的计时统计信息
用法 duration
[dependencies]
timed = "0.1.5"
log = "0.4"
use timed::timed;
#[macro_use] extern crate log;
#[timed]
fn add(x: i32, y: i32) -> i32 { x + y }
#[timed]
fn mul(x: i32, y: i32) -> i32 { x * y }
#[timed(duration(printer = "println!"))]
fn mul_println(x: i32, y: i32) -> i32 { x * y}
#[timed(duration(printer = "info!"))]
fn mul_info(x: i32, y: i32) -> i32 { x * y }
#[test]
fn timing() {
assert_eq!(add(1, 2), 3);
assert_eq!(mul(1, 2), 2);
assert_eq!(mul_println(1, 2), 2);
assert_eq!(mul_info(1, 2), 2);
}
输出
$ cargo test -- --nocapture
running 1 test
function=add duration=36ns
function=mul duration=36ns
function=mul_println duration=31ns
INFO demo_duration > function=mul_info duration=326ns
test timing ... ok
也支持 main 和 tokio
#[tokio::main]
#[timed]
async fn main() {
println!("Running main");
reqwest::get("https://google.com").await;
}
输出
Running main
Calling https://type.fit/api/quotes
Quote of the day:
Genius is one percent inspiration and ninety-nine percent perspiration. - Thomas Edison
function=get_random_quote duration=455.291753ms
function=main duration=456.452412ms
用法 chrome::tracing
#[timed::timed(tracing(enabled = true), duration(disabled = true))]
fn foo() {
bar();
baz();
}
#[timed::timed(tracing(enabled = true), duration(disabled = true))]
fn baz() {
println!("Hello")
}
#[timed::timed(tracing(enabled = true), duration(disabled = true))]
fn bar() {
baz();
}
#[timed::timed(tracing(enabled = true), duration(disabled = true))]
fn main() {
let trace = timed::TraceOptions::new()
.with_chrome_trace(|x: &str| println!("{}", x))
.build_named("Main");
foo();
trace.finish();
}
Hello
Hello
[
{ "pid": 0, "ts": 1603026625248670, "ph": "B", "name": "foo" },
{ "pid": 0, "ts": 1603026625248676, "ph": "B", "name": "bar" },
{ "pid": 0, "ts": 1603026625248677, "ph": "B", "name": "baz" },
{ "pid": 0, "ts": 1603026625248721, "ph": "E", "name": "baz" },
{ "pid": 0, "ts": 1603026625248725, "ph": "E", "name": "bar" },
{ "pid": 0, "ts": 1603026625248727, "ph": "B", "name": "baz" },
{ "pid": 0, "ts": 1603026625248732, "ph": "E", "name": "baz" },
{ "pid": 0, "ts": 1603026625248735, "ph": "E", "name": "foo" }
]
将 [
和 ]
之间的 json 压缩存保存到文件 tracing.json
,然后在 Chrome 中打开 chrome://tracing
并拖动文件:
用法统计
#[timed::timed(tracing(enabled = true), duration(disabled = true))]
fn main() {
let trace = timed::Trace::new("Main");
foo();
std::thread::sleep(std::time::Duration::from_millis(10));
println!("{}", trace.statistics());
}
+----------------------------------------+-------+--------------+-----------+-----------+-----------+-----------+-----------+
| function name | calls | overall time | avg time | max time | p90 time | p50 time | p10 time |
+----------------------------------------+-------+--------------+-----------+-----------+-----------+-----------+-----------+
| Main | 1 | 10.955ms | 10.955ms | 10.955ms | 10.955ms | 10.955ms | 10.955ms |
+----------------------------------------+-------+--------------+-----------+-----------+-----------+-----------+-----------+
| demo_statistics::foo | 1 | 112.184µs | 112.184µs | 112.184µs | 112.184µs | 112.184µs | 112.184µs |
+----------------------------------------+-------+--------------+-----------+-----------+-----------+-----------+-----------+
| demo_statistics::bar | 1 | 99.452µs | 99.452µs | 99.452µs | 99.452µs | 99.452µs | 99.452µs |
+----------------------------------------+-------+--------------+-----------+-----------+-----------+-----------+-----------+
| demo_statistics::baz | 11 | 85.069µs | 7.733µs | 8.403µs | 5.738µs | 5.895µs | 19.525µs |
+----------------------------------------+-------+--------------+-----------+-----------+-----------+-----------+-----------+
| demo_statistics::one::two::three::deep | 33 | 691ns | 20ns | 25ns | 23ns | 17ns | 23ns |
+----------------------------------------+-------+--------------+-----------+-----------+-----------+-----------+-----------+
贡献
欢迎贡献。请提交 PR。查看 TODO
运行 / 构建
要构建特定的包/示例/二进制文件,请遵循常规工作流程。要构建所有内容,请运行以下命令
$ cd tests && cargo make all
$ cd timed && cargo make all
$ cd timed_proc_macros && cargo make all
依赖项
~4.5MB
~75K SLoC