invocation-counter 0.1.0

Datastructure to answer to: how many times a function has been called in the last X minutes?
Documentation
  • Coverage
  • 83.33%
    5 out of 6 items documented1 out of 5 items with examples
  • Size
  • Source code size: 34.65 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.65 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • oramasearch/invocation-counter
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • allevo micheleriva

Invocation counter

Rust

This data structure responses the following question: how many times a function has been called in the last X minutes?

Installation

cargo add invocation-counter

Example

use invocation_counter::Counter;

fn main() {
    // 4 is the group_shift_factor
    // 16 is the number of buckets
    let counter = Counter::<16, 4>::new(4);

    let mut now = 0; // Instant::now().elapsed().as_secs();
    counter.increment_by_one(now);

    now += 1; // Simulate a second passing
    counter.increment_by_one(now);

    assert_eq!(counter.get_count_till(now), 2);

    now += 2_u64.pow(4); // Simulate 16 seconds passing
    counter.increment_by_one(now);

    now += 1; // Simulate a second passing
    counter.increment_by_one(now);

    assert_eq!(counter.get_count_till(now), 4);

    now += 2_u64.pow(4) * 16; // Move foward for a while...
    counter.increment_by_one(now);
    assert_eq!(counter.get_count_till(now), 1); // The counter should have reset
}