edition 2024 (#4702)
* feat: update Cargo.toml for 2024 edition and optimize release profiles
* feat: refactor environment variable settings for Linux and improve code organization
* Refactor conditional statements to use `&&` for improved readability
- Updated multiple files to combine nested `if let` statements using `&&` for better clarity and conciseness.
- This change enhances the readability of the code by reducing indentation levels and making the conditions more straightforward.
- Affected files include: media_unlock_checker.rs, profile.rs, clash.rs, profiles.rs, async_proxy_query.rs, core.rs, handle.rs, hotkey.rs, service.rs, timer.rs, tray/mod.rs, merge.rs, seq.rs, config.rs, proxy.rs, window.rs, general.rs, dirs.rs, i18n.rs, init.rs, network.rs, and window.rs in the resolve module.
* refactor: streamline conditional checks using `&&` for improved readability
* fix: update release profile settings for panic behavior and optimization
* fix: adjust optimization level in Cargo.toml and reorder imports in lightweight.rs
2025-09-10 09:49:06 +08:00
|
|
|
|
use criterion::{Criterion, criterion_group, criterion_main};
|
2025-07-04 22:43:23 +08:00
|
|
|
|
use std::hint::black_box;
|
2025-10-16 09:01:16 +08:00
|
|
|
|
use std::process;
|
2025-10-15 08:32:30 +08:00
|
|
|
|
use tokio::runtime::Runtime;
|
2025-07-04 22:43:23 +08:00
|
|
|
|
|
2025-10-15 08:32:30 +08:00
|
|
|
|
// 引入业务模型 & Draft 实现
|
2025-07-04 22:43:23 +08:00
|
|
|
|
use app_lib::config::IVerge;
|
2025-10-15 08:32:30 +08:00
|
|
|
|
use app_lib::utils::Draft as DraftNew;
|
|
|
|
|
|
|
|
|
|
|
|
/// 创建测试数据
|
|
|
|
|
|
fn make_draft() -> DraftNew<Box<IVerge>> {
|
|
|
|
|
|
let verge = Box::new(IVerge {
|
|
|
|
|
|
enable_auto_launch: Some(true),
|
|
|
|
|
|
enable_tun_mode: Some(false),
|
|
|
|
|
|
..Default::default()
|
|
|
|
|
|
});
|
|
|
|
|
|
DraftNew::from(verge)
|
|
|
|
|
|
}
|
2025-07-04 22:43:23 +08:00
|
|
|
|
|
2025-10-15 08:32:30 +08:00
|
|
|
|
/// 基准:只读 data_ref(正式数据)
|
|
|
|
|
|
fn bench_data_ref(c: &mut Criterion) {
|
|
|
|
|
|
c.bench_function("draft_data_ref", |b| {
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
|
let draft = make_draft();
|
|
|
|
|
|
let data = draft.data_ref();
|
|
|
|
|
|
black_box(data.enable_auto_launch);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-07-04 22:43:23 +08:00
|
|
|
|
|
2025-10-15 08:32:30 +08:00
|
|
|
|
/// 基准:可写 data_mut(正式数据)
|
|
|
|
|
|
fn bench_data_mut(c: &mut Criterion) {
|
|
|
|
|
|
c.bench_function("draft_data_mut", |b| {
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
|
let draft = make_draft();
|
|
|
|
|
|
let mut data = draft.data_mut();
|
|
|
|
|
|
data.enable_tun_mode = Some(true);
|
|
|
|
|
|
black_box(data.enable_tun_mode);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-07-04 22:43:23 +08:00
|
|
|
|
|
2025-10-15 08:32:30 +08:00
|
|
|
|
/// 基准:首次创建草稿(会触发 clone)
|
|
|
|
|
|
fn bench_draft_mut_first(c: &mut Criterion) {
|
|
|
|
|
|
c.bench_function("draft_draft_mut_first", |b| {
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
|
let draft = make_draft();
|
|
|
|
|
|
let mut d = draft.draft_mut();
|
|
|
|
|
|
d.enable_auto_launch = Some(false);
|
|
|
|
|
|
black_box(d.enable_auto_launch);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-07-04 22:43:23 +08:00
|
|
|
|
|
2025-10-15 08:32:30 +08:00
|
|
|
|
/// 基准:重复 draft_mut(已存在草稿,不再 clone)
|
|
|
|
|
|
fn bench_draft_mut_existing(c: &mut Criterion) {
|
|
|
|
|
|
c.bench_function("draft_draft_mut_existing", |b| {
|
2025-07-04 22:43:23 +08:00
|
|
|
|
b.iter(|| {
|
2025-10-15 08:32:30 +08:00
|
|
|
|
let draft = make_draft();
|
|
|
|
|
|
{
|
|
|
|
|
|
let mut first = draft.draft_mut();
|
|
|
|
|
|
first.enable_tun_mode = Some(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
let mut second = draft.draft_mut();
|
|
|
|
|
|
second.enable_tun_mode = Some(false);
|
|
|
|
|
|
black_box(second.enable_tun_mode);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-07-04 22:43:23 +08:00
|
|
|
|
|
2025-10-15 08:32:30 +08:00
|
|
|
|
/// 基准:零拷贝读取最新视图(latest_ref)
|
|
|
|
|
|
fn bench_latest_ref(c: &mut Criterion) {
|
|
|
|
|
|
c.bench_function("draft_latest_ref", |b| {
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
|
let draft = make_draft();
|
|
|
|
|
|
let latest = draft.latest_ref();
|
|
|
|
|
|
black_box(latest.enable_auto_launch);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-07-04 22:43:23 +08:00
|
|
|
|
|
2025-10-15 08:32:30 +08:00
|
|
|
|
/// 基准:apply(提交草稿)
|
|
|
|
|
|
fn bench_apply(c: &mut Criterion) {
|
|
|
|
|
|
c.bench_function("draft_apply", |b| {
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
|
let draft = make_draft();
|
2025-07-04 22:43:23 +08:00
|
|
|
|
{
|
|
|
|
|
|
let mut d = draft.draft_mut();
|
|
|
|
|
|
d.enable_auto_launch = Some(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
let _ = draft.apply();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 08:32:30 +08:00
|
|
|
|
/// 基准:discard(丢弃草稿)
|
|
|
|
|
|
fn bench_discard(c: &mut Criterion) {
|
|
|
|
|
|
c.bench_function("draft_discard", |b| {
|
2025-07-04 22:43:23 +08:00
|
|
|
|
b.iter(|| {
|
2025-10-15 08:32:30 +08:00
|
|
|
|
let draft = make_draft();
|
2025-07-04 22:43:23 +08:00
|
|
|
|
{
|
|
|
|
|
|
let mut d = draft.draft_mut();
|
|
|
|
|
|
d.enable_auto_launch = Some(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
let _ = draft.discard();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 08:32:30 +08:00
|
|
|
|
/// 基准:异步 with_data_modify
|
|
|
|
|
|
fn bench_with_data_modify(c: &mut Criterion) {
|
2025-10-16 09:01:16 +08:00
|
|
|
|
let rt = Runtime::new().unwrap_or_else(|error| {
|
|
|
|
|
|
eprintln!("draft benchmarks require a Tokio runtime: {error}");
|
|
|
|
|
|
process::exit(1);
|
|
|
|
|
|
});
|
2025-10-15 08:32:30 +08:00
|
|
|
|
|
|
|
|
|
|
c.bench_function("draft_with_data_modify", |b| {
|
|
|
|
|
|
b.to_async(&rt).iter(|| async {
|
|
|
|
|
|
let draft = make_draft();
|
|
|
|
|
|
let _res: Result<(), anyhow::Error> = draft
|
|
|
|
|
|
.with_data_modify(|mut box_data| async move {
|
|
|
|
|
|
box_data.enable_auto_launch =
|
|
|
|
|
|
Some(!box_data.enable_auto_launch.unwrap_or(false));
|
|
|
|
|
|
Ok((box_data, ()))
|
|
|
|
|
|
})
|
|
|
|
|
|
.await;
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-04 22:43:23 +08:00
|
|
|
|
criterion_group!(
|
|
|
|
|
|
benches,
|
2025-10-15 08:32:30 +08:00
|
|
|
|
bench_data_ref,
|
|
|
|
|
|
bench_data_mut,
|
|
|
|
|
|
bench_draft_mut_first,
|
|
|
|
|
|
bench_draft_mut_existing,
|
|
|
|
|
|
bench_latest_ref,
|
|
|
|
|
|
bench_apply,
|
|
|
|
|
|
bench_discard,
|
|
|
|
|
|
bench_with_data_modify
|
2025-07-04 22:43:23 +08:00
|
|
|
|
);
|
|
|
|
|
|
criterion_main!(benches);
|