Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Jan 31, 2024
1 parent 984f3ff commit bb8929e
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 146 deletions.
8 changes: 4 additions & 4 deletions src/perf_event/counting/group/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
mod hardware;
mod software;

use crate::config::{Cpu, Process};
use crate::counting::{Config, CounterGroup};
use crate::{Builder, Event, EventScope};
use crate::{Event, EventScope};

/// rate = ev_1 / ev_2
pub fn test_group<F>(ev_1: &Event, ev_2: &Event, workload: &mut F)
Expand All @@ -16,13 +17,12 @@ where
}

fn gen_group() -> CounterGroup {
let builder = Builder::new().calling_process().any_cpu();
builder.build_counting_group().unwrap()
CounterGroup::new(&Process::Calling, &Cpu::Any).unwrap()
}

fn gen_cfg(ev: &Event) -> Config {
let scopes = [EventScope::User, EventScope::Host];
Config::new(ev, &scopes, &Default::default())
Config::new(ev, &scopes)
}

fn test_stat<F>(ev_1: &Event, ev_2: &Event, workload: &mut F)
Expand Down
17 changes: 8 additions & 9 deletions src/perf_event/counting/single/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
mod hardware;
mod software;

use crate::config::{Cpu, Process};
use crate::counting::{Config, Counter};
use crate::{Builder, Event, EventScope};
use crate::{Event, EventScope};

pub fn test_single<F>(ev: &Event, workload: &mut F)
where
Expand All @@ -13,20 +14,18 @@ where
test_reset_count(ev, workload);
}

fn gen_counting(ev: &Event) -> Counter {
let builder = Builder::new().calling_process().any_cpu();

fn gen_counter(ev: &Event) -> Counter {
let scopes = [EventScope::User, EventScope::Host];
let cfg = Config::new(ev, &scopes, &Default::default());
let cfg = Config::new(ev, &scopes);

builder.build_counting(&cfg).unwrap()
Counter::new(&Process::Calling, &Cpu::Any, &cfg).unwrap()
}

fn test_stat<F>(ev: &Event, workload: &mut F)
where
F: FnMut(),
{
let mut counter = gen_counting(ev);
let mut counter = gen_counter(ev);

let before = counter.stat().unwrap().event_count;
dbg!(before);
Expand All @@ -45,7 +44,7 @@ fn test_enable_disable<F>(ev: &Event, workload: &mut F)
where
F: FnMut(),
{
let mut counter = gen_counting(ev);
let mut counter = gen_counter(ev);

counter.enable().unwrap();
workload();
Expand All @@ -63,7 +62,7 @@ fn test_reset_count<F>(ev: &Event, workload: &mut F)
where
F: FnMut(),
{
let mut counter = gen_counting(ev);
let mut counter = gen_counter(ev);

counter.enable().unwrap();
workload();
Expand Down
34 changes: 13 additions & 21 deletions src/perf_event/sampling/group/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
mod hardware;
mod software;

use crate::config::{Cpu, Process};
use crate::sampling::record::{Record, RecordBody};
use crate::sampling::{Config, ExtraConfig, FixedSamplerGroup, OverflowBy, SamplerGuard};
use crate::{Builder, Event, EventScope};
use crate::sampling::{Config, FixedSamplerGroup, OverflowBy, SamplerGroup, SamplerGuard};
use crate::{Event, EventScope};

pub fn test_group<F>(ev_1: &Event, ev_2: &Event, workload: &mut F)
where
Expand All @@ -17,26 +18,22 @@ where
test_guard_stat(ev_1, ev_2, workload);
}

fn gen_builder() -> Builder {
Builder::new()
.calling_process()
.any_cpu()
.ring_buffer_pages(1 + 512)
fn gen_group() -> SamplerGroup {
let mmap_pages = 1 + 512;
SamplerGroup::new(&Process::Calling, &Cpu::Any, mmap_pages).unwrap()
}

fn gen_cfg(ev: &Event) -> Config {
let scopes = [EventScope::User, EventScope::Host];
let overflow_by = OverflowBy::Period(1000);
let extra_config = ExtraConfig::default();
Config::new(&ev, &scopes, &overflow_by, &extra_config)
Config::new(&ev, &scopes, &overflow_by)
}

fn test_next_record<F>(ev_1: &Event, ev_2: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder();
let mut group = builder.build_sampling_group().unwrap();
let mut group = gen_group();
let ev_1_guard = group.add_member(&gen_cfg(ev_1)).unwrap();
let ev_2_guard = group.add_member(&gen_cfg(ev_2)).unwrap();

Expand Down Expand Up @@ -72,8 +69,7 @@ fn test_enable_disable<F>(ev_1: &Event, ev_2: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder();
let mut group = builder.build_sampling_group().unwrap();
let mut group = gen_group();
let ev_1_guard = group.add_member(&gen_cfg(ev_1)).unwrap();
let ev_2_guard = group.add_member(&gen_cfg(ev_2)).unwrap();

Expand Down Expand Up @@ -109,8 +105,7 @@ fn test_guard_basic<F>(ev_1: &Event, ev_2: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder();
let mut group = builder.build_sampling_group().unwrap();
let mut group = gen_group();
let mut ev_1_guard = group.add_member(&gen_cfg(ev_1)).unwrap();
let mut ev_2_guard = group.add_member(&gen_cfg(ev_2)).unwrap();

Expand Down Expand Up @@ -142,8 +137,7 @@ fn test_guard_enable_disable<F>(ev_1: &Event, ev_2: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder();
let mut group = builder.build_sampling_group().unwrap();
let mut group = gen_group();
let mut ev_1_guard = group.add_member(&gen_cfg(ev_1)).unwrap();
let mut ev_2_guard = group.add_member(&gen_cfg(ev_2)).unwrap();

Expand Down Expand Up @@ -179,8 +173,7 @@ fn test_stat<F>(ev_1: &Event, ev_2: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder();
let mut group = builder.build_sampling_group().unwrap();
let mut group = gen_group();
let ev_1_guard = group.add_member(&gen_cfg(ev_1)).unwrap();
let ev_2_guard = group.add_member(&gen_cfg(ev_2)).unwrap();

Expand Down Expand Up @@ -209,8 +202,7 @@ fn test_guard_stat<F>(ev_1: &Event, ev_2: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder();
let mut group = builder.build_sampling_group().unwrap();
let mut group = gen_group();
let mut ev_1_guard = group.add_member(&gen_cfg(ev_1)).unwrap();
let mut ev_2_guard = group.add_member(&gen_cfg(ev_2)).unwrap();

Expand Down
33 changes: 13 additions & 20 deletions src/perf_event/sampling/single/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ mod sample_record_fields;
mod software;

use crate::sampling::record::{Record, RecordBody};
use crate::sampling::{Config, ExtraConfig, OverflowBy};
use crate::{Builder, Event, EventScope};
use crate::sampling::{Config, ExtraConfig, OverflowBy, Sampler};
use crate::{Event, EventScope};
use crate::config::{Cpu, Process};

pub fn test_single<F>(ev: &Event, workload: &mut F)
where
Expand All @@ -19,27 +20,24 @@ where
test_stat(ev, workload);
}

fn gen_builder(mmap_pages: usize) -> Builder {
Builder::new()
.calling_process()
.any_cpu()
.ring_buffer_pages(mmap_pages)
fn gen_sampler(cfg: &Config) -> Sampler {
let mmap_pages = 1 + 512;
Sampler::new(&Process::Calling, &Cpu::Any, mmap_pages, cfg).unwrap()
}

fn gen_cfg(ev: &Event) -> Config {
let scopes = [EventScope::User, EventScope::Host];
let overflow_by = OverflowBy::Period(1000);
let mut extra_config = ExtraConfig::default();
extra_config.sample_record_fields.time = true;
Config::new(&ev, &scopes, &overflow_by, &extra_config)
Config::extra_new(&ev, &scopes, &overflow_by, &extra_config)
}

fn test_next_record<F>(ev: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder(1 + (1 << 16));
let mut sampler = builder.build_sampling(&gen_cfg(ev)).unwrap();
let mut sampler = gen_sampler(&gen_cfg(ev));

sampler.enable().unwrap();
workload();
Expand All @@ -61,8 +59,7 @@ fn test_all_records<F>(ev: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder(1 + (1 << 16));
let mut sampler = builder.build_sampling(&gen_cfg(ev)).unwrap();
let mut sampler = gen_sampler(&gen_cfg(ev));

sampler.enable().unwrap();
workload();
Expand All @@ -84,8 +81,7 @@ fn test_enable_disable<F>(ev: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder(1 + (1 << 16));
let mut sampler = builder.build_sampling(&gen_cfg(ev)).unwrap();
let mut sampler = gen_sampler(&gen_cfg(ev));

assert!(sampler.next_record().is_none());
sampler.enable().unwrap();
Expand Down Expand Up @@ -113,8 +109,7 @@ fn test_pause_resume<F>(ev: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder(1 + (1 << 16));
let mut sampler = builder.build_sampling(&gen_cfg(ev)).unwrap();
let mut sampler = gen_sampler(&gen_cfg(ev));

assert!(sampler.next_record().is_none());
sampler.enable().unwrap();
Expand All @@ -141,8 +136,7 @@ fn test_ring_buffer<F>(ev: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder(1 + 512);
let mut sampler = builder.build_sampling(&gen_cfg(ev)).unwrap();
let mut sampler = gen_sampler(&gen_cfg(ev));

sampler.enable().unwrap();
workload();
Expand All @@ -161,8 +155,7 @@ fn test_stat<F>(ev: &Event, workload: &mut F)
where
F: FnMut(),
{
let builder = gen_builder(1 + 512);
let mut sampler = builder.build_sampling(&gen_cfg(ev)).unwrap();
let mut sampler = gen_sampler(&gen_cfg(ev));

sampler.enable().unwrap();
workload();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use crate::config::{Cpu, Process};
use crate::sampling::record::{Record, RecordBody};
use crate::sampling::{Config, ExtraConfig, OverflowBy};
use crate::sampling::{Config, ExtraConfig, OverflowBy, Sampler};
use crate::test::cpu_workload;
use crate::{Builder, Event, EventScope, HardwareEvent};
use crate::{Event, EventScope, HardwareEvent};

fn gen_builder() -> Builder {
fn gen_sampler(cfg: &Config) -> Sampler {
let mmap_pages = 1 + 512;
Builder::new()
.calling_process()
.any_cpu()
.ring_buffer_pages(mmap_pages)
Sampler::new(&Process::Calling, &Cpu::Any, mmap_pages, cfg).unwrap()
}

fn gen_cfg(sample_regs_intr: u64) -> Config {
Expand All @@ -18,15 +16,14 @@ fn gen_cfg(sample_regs_intr: u64) -> Config {
let event = HardwareEvent::CpuCycles;
let scopes = [EventScope::User, EventScope::Host];
let overflow_by = OverflowBy::Period(1000);
Config::new(&Event::from(event), &scopes, &overflow_by, &extra_config)
Config::extra_new(&Event::from(event), &scopes, &overflow_by, &extra_config)
}

#[test]
fn test() {
let builder = gen_builder();
for i in 1..7 {
let cfg = gen_cfg(i);
let mut sampler = builder.build_sampling(&cfg).unwrap();
let mut sampler = gen_sampler(&cfg);

sampler.enable().unwrap();
cpu_workload();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use crate::sampling::record::{Record, RecordBody};
use crate::sampling::{Config, ExtraConfig, OverflowBy};
use crate::sampling::{Config, ExtraConfig, OverflowBy, Sampler};
use crate::test::cpu_workload;
use crate::{Builder, Event, EventScope, HardwareEvent};
use crate::{Event, EventScope, HardwareEvent};
use crate::config::{Cpu, Process};

fn gen_builder() -> Builder {
fn gen_sampler(cfg: &Config) -> Sampler {
let mmap_pages = 1 + 512;
Builder::new()
.calling_process()
.any_cpu()
.ring_buffer_pages(mmap_pages)
Sampler::new(&Process::Calling, &Cpu::Any, mmap_pages, cfg).unwrap()
}

fn gen_cfg(sample_regs_user: u64) -> Config {
Expand All @@ -18,15 +16,14 @@ fn gen_cfg(sample_regs_user: u64) -> Config {
let event = HardwareEvent::CpuCycles;
let scopes = [EventScope::User, EventScope::Host];
let overflow_by = OverflowBy::Period(1000);
Config::new(&Event::from(event), &scopes, &overflow_by, &extra_config)
Config::extra_new(&Event::from(event), &scopes, &overflow_by, &extra_config)
}

#[test]
fn test() {
let builder = gen_builder();
for i in 1..7 {
let cfg = gen_cfg(i);
let mut sampler = builder.build_sampling(&cfg).unwrap();
let mut sampler = gen_sampler(&cfg);

sampler.enable().unwrap();
cpu_workload();
Expand Down
17 changes: 7 additions & 10 deletions src/perf_event/sampling/single/tests/sample_record_fields/all.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use crate::sampling::record::sample::WeightRepr;
use crate::sampling::record::{Record, RecordBody};
use crate::sampling::{Config, ExtraConfig, OverflowBy, SampleRecordFields};
use crate::sampling::{Config, ExtraConfig, OverflowBy, Sampler, SampleRecordFields};
use crate::test::cpu_workload;
use crate::{Builder, Event, EventScope, HardwareEvent};
use crate::{Event, EventScope, HardwareEvent};
use crate::config::{Cpu, Process};

fn gen_builder() -> Builder {
fn gen_sampler(cfg: &Config) -> Sampler {
let mmap_pages = 1 + 512;
Builder::new()
.calling_process()
.any_cpu()
.ring_buffer_pages(mmap_pages)
Sampler::new(&Process::Calling, &Cpu::Any, mmap_pages, cfg).unwrap()
}

fn gen_cfg(extra_config: ExtraConfig) -> Config {
let event = HardwareEvent::CpuCycles;
let scopes = [EventScope::User, EventScope::Host];
let overflow_by = OverflowBy::Period(1000);
Config::new(&Event::from(event), &scopes, &overflow_by, &extra_config)
Config::extra_new(&Event::from(event), &scopes, &overflow_by, &extra_config)
}

#[test]
Expand Down Expand Up @@ -53,10 +51,9 @@ fn test() {
#[cfg(feature = "linux-5.11")]
code_page_size: true,
};
let builder = gen_builder();
let cfg = gen_cfg(extra_config);
let mut sampler = gen_sampler(&cfg);

let mut sampler = builder.build_sampling(&cfg).unwrap();
sampler.enable().unwrap();
cpu_workload();
sampler.disable().unwrap();
Expand Down
Loading

0 comments on commit bb8929e

Please sign in to comment.