use nutype::nutype;
use std::time::Duration;
#[nutype(
validate(greater = 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)
)]
pub struct TaskBatchThreshold(usize);
#[nutype(
validate(greater = 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)
)]
pub struct MaxConcurrentTasks(usize);
#[nutype(
validate(less_or_equal = 100),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)
)]
pub struct ErrorThresholdPercent(u8);
#[nutype(
validate(greater = 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)
)]
pub struct TargetRps(u32);
#[nutype(
validate(predicate = |d| d.as_secs() > 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)
)]
pub struct TestDuration(Duration);
#[nutype(
validate(greater = 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)
)]
pub struct PayloadSize(usize);
#[nutype(
validate(greater = 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)
)]
pub struct ConcurrentUsers(usize);
#[nutype(
validate(greater = 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)
)]
pub struct MaxConnections(u32);
#[nutype(
validate(greater = 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)
)]
pub struct MinConnections(u32);
#[nutype(validate(finite), derive(Debug, Clone, Copy, PartialEq, PartialOrd))]
pub struct OpsPerMillisecond(f64);
#[nutype(
validate(greater_or_equal = 0.0, less_or_equal = 1.0),
derive(Debug, Clone, Copy, PartialEq, PartialOrd)
)]
pub struct RpsTolerance(f64);
#[nutype(
validate(predicate = |d| d.as_nanos() > 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)
)]
pub struct LatencyThreshold(Duration);
#[nutype(
validate(greater = 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)
)]
pub struct BenchmarkIterations(u32);
#[nutype(
validate(greater = 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)
)]
pub struct ThreadCount(usize);
#[nutype(
validate(greater = 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)
)]
pub struct OperationsPerThread(u32);
#[nutype(
validate(predicate = |d| d.as_millis() > 0),
derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)
)]
pub struct ConcurrencyTestTimeout(Duration);
#[nutype(
validate(predicate = |s: &str| !s.is_empty() && s.starts_with("postgres://")),
derive(Debug, Clone, PartialEq, Eq, Hash)
)]
pub struct DatabaseUrl(String);
impl PayloadSize {
pub fn one_kb() -> Self {
Self::try_new(1024).unwrap()
}
pub fn ten_kb() -> Self {
Self::try_new(10 * 1024).unwrap()
}
pub fn sixty_four_kb() -> Self {
Self::try_new(64 * 1024).unwrap()
}
pub fn one_twenty_eight_kb() -> Self {
Self::try_new(128 * 1024).unwrap()
}
}
impl TestDuration {
pub fn ten_seconds() -> Result<Self, TestDurationError> {
Self::try_new(Duration::from_secs(10))
}
pub fn twenty_seconds() -> Result<Self, TestDurationError> {
Self::try_new(Duration::from_secs(20))
}
pub fn thirty_seconds() -> Result<Self, TestDurationError> {
Self::try_new(Duration::from_secs(30))
}
}
impl LatencyThreshold {
pub fn one_ms() -> Result<Self, LatencyThresholdError> {
Self::try_new(Duration::from_millis(1))
}
pub fn five_ms() -> Result<Self, LatencyThresholdError> {
Self::try_new(Duration::from_millis(5))
}
}
impl RpsTolerance {
pub fn five_percent() -> Self {
Self::try_new(0.95).unwrap()
}
pub fn ten_percent() -> Self {
Self::try_new(0.90).unwrap()
}
}
impl From<TargetRps> for OpsPerMillisecond {
fn from(rps: TargetRps) -> Self {
Self::try_new(rps.into_inner() as f64 / 1000.0).unwrap()
}
}