-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: enhance WireGuard performance (#51)
- Loading branch information
Showing
10 changed files
with
206 additions
and
132 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::sync::atomic::{AtomicU64, Ordering}; | ||
use std::time::{Duration, Instant}; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct IdGenerator(AtomicU64); | ||
|
||
impl IdGenerator { | ||
pub fn get(&self) -> u64 { | ||
self.0.fetch_add(1, Ordering::Relaxed) | ||
} | ||
} | ||
|
||
#[allow(unused)] | ||
pub struct TputMeasurement { | ||
inner: std::sync::Mutex<TputMeasurementInner>, | ||
} | ||
|
||
struct TputMeasurementInner { | ||
pub accum_bytes: u64, | ||
pub last_time: Instant, | ||
pub interval: Duration, | ||
} | ||
|
||
impl TputMeasurement { | ||
pub fn new(interval: Duration) -> Self { | ||
Self { | ||
inner: std::sync::Mutex::new(TputMeasurementInner { | ||
accum_bytes: 0, | ||
last_time: Instant::now(), | ||
interval, | ||
}), | ||
} | ||
} | ||
|
||
pub fn update(&self, bytes: usize) -> Option<f64> { | ||
let mut inner = self.inner.lock().unwrap(); | ||
inner.accum_bytes += bytes as u64; | ||
let now = Instant::now(); | ||
let elapsed = now - inner.last_time; | ||
if elapsed >= inner.interval { | ||
let tput = inner.accum_bytes as f64 / elapsed.as_secs_f64(); | ||
inner.accum_bytes = 0; | ||
inner.last_time = now; | ||
Some(tput) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn update_to_mbps(&self, bytes: usize) -> Option<f64> { | ||
self.update(bytes).map(|tput| tput * 8.0 / 1_000_000.0) | ||
} | ||
} |
Oops, something went wrong.