1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#![forbid(unsafe_code)]
use std::{
fmt::Display,
ops::{Add, AddAssign},
sync::atomic::{AtomicBool, AtomicU64, Ordering},
time::Duration,
};
use serde::Serialize;
mod env;
mod metrics;
mod result;
mod runner;
pub use result::ShumaiResult;
pub use runner::run;
pub use shumai_config_impl::{config, ShumaiConfig};
pub mod __dep {
pub use colored;
pub use once_cell;
pub use regex;
pub use serde;
pub use serde_json;
pub use toml;
}
pub struct Context<'a, C: BenchConfig> {
pub thread_id: usize,
pub thread_cnt: usize,
pub config: &'a C,
ready_thread: &'a AtomicU64,
running: &'a AtomicBool,
}
impl<C: BenchConfig> Context<'_, C> {
pub fn wait_for_start(&self) {
self.ready_thread.fetch_add(1, Ordering::Relaxed);
while !self.is_running() {
std::hint::spin_loop();
}
}
pub fn is_running(&self) -> bool {
self.running.load(Ordering::Relaxed)
}
}
pub trait BenchResult:
serde::Serialize + Default + AddAssign + Add<Output = Self> + Clone + Send + Sync + Display
{
fn short_value(&self) -> usize;
#[must_use]
fn normalize_time(self, dur: &Duration) -> Self;
}
impl BenchResult for usize {
fn short_value(&self) -> usize {
*self
}
fn normalize_time(self, dur: &Duration) -> usize {
((self as f64) / dur.as_secs_f64()) as usize
}
}
pub trait BenchConfig: Clone + Serialize + Send + Sync {
fn name(&self) -> &String;
fn thread(&self) -> &[usize];
fn bench_sec(&self) -> usize;
}
pub trait ShumaiBench: Send + Sync {
type Result: BenchResult;
type Config: BenchConfig;
fn load(&mut self) -> Option<serde_json::Value>;
fn run(&self, context: Context<Self::Config>) -> Self::Result;
fn on_iteration_finished(&mut self, _cur_iter: usize) {}
fn on_thread_finished(&mut self, _cur_thread: usize) {}
fn cleanup(&mut self) -> Option<serde_json::Value>;
}