Skip to content
Draft
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
10 changes: 8 additions & 2 deletions crates/codspeed/src/codspeed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::measurement;
use crate::{instrument_hooks::InstrumentHooks, measurement};
use colored::Colorize;
use std::ffi::CString;

Expand All @@ -19,7 +19,8 @@ pub struct CodSpeed {

impl CodSpeed {
pub fn new() -> Self {
let is_instrumented = measurement::is_instrumented();
let ih = InstrumentHooks::instance();
let is_instrumented = ih.is_instrumented();
if !is_instrumented {
println!(
"{} codspeed is enabled, but no performance measurement will be made since it's running in an unknown environment.",
Expand All @@ -46,12 +47,17 @@ impl CodSpeed {
#[inline(always)]
pub fn start_benchmark(&mut self, name: &str) {
self.current_benchmark = CString::new(name).expect("CString::new failed");
let _ = InstrumentHooks::instance().start_benchmark();
measurement::start();
}

#[inline(always)]
pub fn end_benchmark(&mut self) {
measurement::stop(&self.current_benchmark);
let _ = InstrumentHooks::instance().stop_benchmark();
let _ = InstrumentHooks::instance().set_executed_benchmark(
&self.current_benchmark.to_string_lossy()
);
self.benchmarked
.push(self.current_benchmark.to_str().unwrap().to_string());
let action_str = if self.is_instrumented {
Expand Down
4 changes: 4 additions & 0 deletions crates/divan_compat/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ harness = false
[[bench]]
name = "counters"
harness = false

[[bench]]
name = "alloc"
harness = false
35 changes: 35 additions & 0 deletions crates/divan_compat/examples/benches/alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::{alloc::Layout, collections::{HashMap, HashSet}};

#[divan::bench]
fn allocate() {
println!("Hello, world!");

let vec = vec![1, 2, 3];
println!("{:?}", vec);

let mut map = HashMap::new();
map.insert("key", "value");
println!("{:?}", map);

let mut set = HashSet::new();
set.insert("apple");
set.insert("banana");
println!("{:?}", set);

std::thread::sleep(std::time::Duration::from_secs(1));

let mut bytes_vec = vec![0u8; 0x100];
println!("{:?}", bytes_vec.len());

bytes_vec.extend(&vec![0u8; 0x1000]);

// Alloc 256 bytes of memory
for _ in 0..100 {
let memory = unsafe { std::alloc::alloc(Layout::new::<[u8; 42]>()) };
core::hint::black_box(memory);
}
}

fn main() {
divan::main();
}
Loading