4 个版本 (2 个重大更新)

0.4.1 2024 年 7 月 12 日
0.4.0 2024 年 1 月 5 日
0.3.0 2024 年 1 月 5 日
0.1.0 2024 年 1 月 4 日

4#counting

Download history 7/week @ 2024-04-28 25/week @ 2024-05-05 23/week @ 2024-05-12 27/week @ 2024-05-19 29/week @ 2024-05-26 71/week @ 2024-06-02 32/week @ 2024-06-09 11/week @ 2024-06-16 65/week @ 2024-06-23 23/week @ 2024-06-30 145/week @ 2024-07-07 141/week @ 2024-07-14 56/week @ 2024-07-21 94/week @ 2024-07-28

每月 436 次下载

Apache-2.0

22KB
322

resourcetrack

另一个资源跟踪工具。

关于

一个精简、低依赖的计数工具。此项目不使用 unsafe 代码。它只依赖 std。

安全性、清晰性、灵活性和性能按降序优先。计数器是包含对 atomic usize 的 Arc 的普通结构体。这意味着跟踪会有内存成本。这里的目标不是零开销。

详情

Resourcetrack 支持使用您自己的名称的静态类别

use resourcetrack::new_registry;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum MyCategories {
    Miscellaneous,
    Specific,
}

let registry = new_registry::<MyCategories>();
let specific_category_tracker = registry.category(MyCategories::Specific);

Resourcetrack 不明确同步分类的资源计数器。

let _count_sentinel: Count = category_tracker.track(); // non-blocking, on both track and drop

计数器是普通结构体,您可以将其组合到您的昂贵的业务对象中,以实现自动计数管理。如果您这样做,可以考虑使用 lazy_static 来包装计数器,以便在构造函数内部。理想情况下,您可以将 lazy_static 扩展到类别追踪器!

struct ExpensiveResource {
    payload: String,
    _phantom_count: resourcetrack::tracked::Count,
}

当您需要获取计数时(用于记录、度量等),只需读取它们即可。

let registry = new_registry::<MyCategories>();
{
    let _counter_1 = registry.category(MyCategories::Specific).track();
    assert_eq!(vec![(MyCategories::Specific, 1)], registry.read_counts::<Vec<_>>(), "1 specific instance");
    let _counter_2 = registry.category(MyCategories::Specific).track();
    assert_eq!(vec![(MyCategories::Specific, 2)], registry.read_counts::<Vec<_>>(), "2 specific instances");
}

assert_eq!(vec![(MyCategories::Specific, 0)], registry.read_counts::<Vec<_>>(), "both dropped");

您可以跟踪大小资源,其中其大小会变化。为了保持理智,您可能需要限制自己在给定类别中使用 track() 或 track_sized()。不过,您可以在注册表中混合计数和大小,没问题!完整示例

use resourcetrack::tracked;

// Set up your statically knowable categories
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum MyCategories {
    ResourceCount,
    ResourceWeight,
}

// Here is an example of a tracked business object. Both size and weight are tracked.
struct TrackedVector {
    internal: Vec<String>,
    _count_sentinel: tracked::Count,
    weight: tracked::Size,
}
impl TrackedVector {
    pub fn push(&mut self, next: String) {
        self.weight.add(next.len());
    }
}

// Static setup - this should be in some shared lazy static scope.
let registry = resourcetrack::new_registry::<MyCategories>();
let resource_counts = registry.category(MyCategories::ResourceCount);
let resource_weights = registry.category(MyCategories::ResourceWeight);

let mut v = TrackedVector { // This should be wrapped into TrackedVector::new() in your application
    internal: Default::default(),
    _count_sentinel: resource_counts.track(),
    weight: resource_weights.track_size(0),
};
v.push("hello".to_string());
//!
let mut counts = registry.read_counts::<Vec<_>>();
counts.sort();
assert_eq!(
    vec![
        (MyCategories::ResourceCount, 1),
        (MyCategories::ResourceWeight, 5),
    ],
    counts,
)

无运行时依赖