Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Base branch] Check allocation count #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/encoding/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ where

#[cfg(test)]
mod tests {
use std::alloc::{GlobalAlloc, Layout, System};
use super::*;
use crate::metrics::counter::Counter;
use crate::metrics::exemplar::{CounterWithExemplar, HistogramWithExemplars};
Expand All @@ -469,10 +470,31 @@ mod tests {
use crate::metrics::info::Info;
use crate::registry::Unit;
use std::borrow::Cow;
use std::sync::atomic::AtomicI64;
use std::sync::atomic::{AtomicI64, AtomicUsize};
use std::sync::atomic::Ordering::SeqCst;

struct CheckAlloc;

static ALLOCATED: AtomicUsize = AtomicUsize::new(0);

unsafe impl GlobalAlloc for CheckAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOCATED.fetch_add(1, SeqCst);
System.alloc(layout)
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
System.dealloc(ptr, layout);
}
}

#[global_allocator]
static A: CheckAlloc = CheckAlloc;

#[test]
fn encode_counter_int() {
let before = ALLOCATED.load(SeqCst);

let counter: Counter = Counter::default();
let mut registry = Registry::default();
registry.register("my_counter", "My counter", counter.clone());
Expand All @@ -498,6 +520,8 @@ mod tests {
}
_ => assert!(false, "wrong value type"),
}

println!("allocation: {}", ALLOCATED.load(SeqCst) - before);
}

#[test]
Expand Down Expand Up @@ -661,6 +685,8 @@ mod tests {

#[test]
fn encode_counter_family_with_prefix_with_label() {
let before = ALLOCATED.load(SeqCst);

let mut registry = Registry::default();
let sub_registry = registry.sub_registry_with_prefix("my_prefix");
let sub_sub_registry = sub_registry
Expand Down Expand Up @@ -704,6 +730,7 @@ mod tests {
}
_ => assert!(false, "wrong value type"),
}
println!("allocation: {}", ALLOCATED.load(SeqCst) - before);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![forbid(unsafe_code)]
// #![forbid(unsafe_code)]
#![deny(unused)]
#![deny(dead_code)]
#![warn(missing_debug_implementations)]
Expand Down