Skip to main content

quantities/
datathroughput.rs

1// ---------------------------------------------------------------------------
2// Copyright:   (c) 2022 ff. Michael Amrhein (michael@adrhinum.de)
3// License:     This program is part of a larger application. For license
4//              details please read the file LICENSE.TXT provided together
5//              with the application.
6// ---------------------------------------------------------------------------
7// $Source: src/datathroughput.rs $
8// $Revision: 2022-04-16T08:18:48+02:00 $
9
10//! Definition of derived quantity `DataThroughput`.
11
12use crate::{datavolume::DataVolume, duration::Duration, prelude::*};
13
14#[quantity(DataVolume / Duration)]
15#[ref_unit(
16    Byte_per_Second,
17    "B/s",
18    NONE,
19    "Reference unit of quantity `DataThroughput`"
20)]
21#[unit(Bit_per_Second, "b/s", 0.125, "b/s")]
22#[unit(Kilobit_per_Second, "kb/s", 125, "1000·b/s")]
23#[unit(Kibibit_per_Second, "Kib/s", 128, "1024·b/s")]
24#[unit(Kilobyte_per_Second, "kB/s", KILO, 1000, "1000·B/s")]
25#[unit(Kibibyte_per_Second, "KiB/s", 1024, "1024·B/s")]
26#[unit(Megabit_per_Second, "Mb/s", 125000, "1000000·b/s")]
27#[unit(Mebibit_per_Second, "Mib/s", 131072, "1048576·b/s")]
28#[unit(Megabyte_per_Second, "MB/s", MEGA, 1000000, "1000000·B/s")]
29#[unit(Mebibyte_per_Second, "MiB/s", 1048576, "1048576·B/s")]
30#[unit(Gigabit_per_Second, "Gb/s", 125000000, "1000000000·b/s")]
31#[unit(Gibibit_per_Second, "Gib/s", 134217728, "1073741824·b/s")]
32#[unit(Gigabyte_per_Second, "GB/s", GIGA, 1000000000, "1000000000·B/s")]
33#[unit(Gibibyte_per_Second, "GiB/s", 1073741824, "1073741824·B/s")]
34#[unit(Terabit_per_Second, "Tb/s", 125000000000., "1000000000000·b/s")]
35#[unit(Tebibit_per_Second, "Tib/s", 137438953472., "1099511627776·b/s")]
36#[unit(Terabyte_per_Second, "TB/s", TERA, 1000000000000., "1000000000000·B/s")]
37#[unit(Tebibyte_per_Second, "TiB/s", 1099511627776., "1099511627776·B/s")]
38/// Volume of data transferred per unit of time
39///
40/// Definition: DataVolume/Duration
41///
42/// Reference unit: Byte per Second ('B/s')
43///
44/// Predefined units:
45///
46/// | Symbol | Name                  | Definition        | Equivalent in 'B/s' |
47/// |--------|-----------------------|-------------------|---------------------|
48/// | b/s    | Bit per Second        | b/s               | 0.125               |
49/// | kb/s   | Kilobit per Second    | 1000·b/s          | 125                 |
50/// | Kib/s  | Kibibit per Second    | 1024·b/s          | 128                 |
51/// | kB/s   | Kilobyte per Second   | 1000·B/s          | 1000                |
52/// | KiB/s  | Kibibyte per Second   | 1024·B/s          | 1024                |
53/// | Mb/s   | Megabit per Second    | 1000000·b/s       | 125000              |
54/// | Mib/s  | Mebibit per Second    | 1048576·b/s       | 131072              |
55/// | MB/s   | Megabyte per Second   | 1000000·B/s       | 1000000             |
56/// | MiB/s  | Mebibyte per Second   | 1048576·B/s       | 1048576             |
57/// | Gb/s   | Gigabit per Second    | 1000000000·b/s    | 125000000           |
58/// | Gib/s  | Gibibit per Second    | 1073741824·b/s    | 134217728           |
59/// | GB/s   | Gigabyte per Second   | 1000000000·B/s    | 1000000000          |
60/// | GiB/s  | Gibibyte per Second   | 1073741824·B/s    | 1073741824          |
61/// | Tb/s   | Terabit per Second    | 1000000000000·b/s | 125000000000        |
62/// | Tib/s  | Tebibit per Second    | 1099511627776·b/s | 137438953472        |
63/// | TB/s   | Terabyte per Second   | 1000000000000·B/s | 1000000000000       |
64/// | TiB/s  | Tebibyte per Second   | 1099511627776·B/s | 1099511627776       |
65pub struct DataThroughput {}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70    use crate::{
71        assert_almost_eq, datavolume::MEBIBYTE, duration::MILLISECOND,
72    };
73
74    #[test]
75    fn test_datavolume_div_duration() {
76        let ad: AmountT = Amnt!(837.5);
77        let d = ad * MEBIBYTE;
78        let at = Amnt!(2.5);
79        let t = at * MILLISECOND;
80        let r = d / t;
81        assert_almost_eq!(r.amount(), ad / at * Amnt!(1.048576));
82        assert_eq!(r.unit(), GIGABYTE_PER_SECOND);
83    }
84}