Skip to content

Instantly share code, notes, and snippets.

@tsenart
Last active October 31, 2018 03:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsenart/e90f11a47b84d8073b5254176f23e7f1 to your computer and use it in GitHub Desktop.
Save tsenart/e90f11a47b84d8073b5254176f23e7f1 to your computer and use it in GitHub Desktop.
Quantum effects
10/s: min: 454ns max: 5.341µs 0.50: 711ns 0.90: 1.042µs 0.95: 1.223µs 0.99: 4.173µs
50/s: min: 278ns max: 76.968µs 0.50: 768ns 0.90: 1.306µs 0.95: 2.956µs 0.99: 5.596µs
100/s: min: 193ns max: 41.241µs 0.50: 712ns 0.90: 1.136µs 0.95: 1.614µs 0.99: 4.393µs
500/s: min: 148ns max: 21.488µs 0.50: 721ns 0.90: 1.003µs 0.95: 1.086µs 0.99: 3.061µs
1000/s: min: 137ns max: 44.32µs 0.50: 735ns 0.90: 1.009µs 0.95: 1.069µs 0.99: 2.682µs
5000/s: min: 109ns max: 11.757µs 0.50: 348ns 0.90: 374ns 0.95: 398ns 0.99: 491ns
10000/s: min: 109ns max: 22.745µs 0.50: 348ns 0.90: 371ns 0.95: 384ns 0.99: 478ns
package main
import (
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"time"
)
func main() {
for _, rate := range []int{10, 50, 100, 500, 1000, 5000, 10000} { // per second
duration := 10 * time.Second
interval := time.Duration(1e9 / rate)
latencies := make([]time.Duration, 0, duration/interval)
deadline := time.Now().Add(duration)
var began time.Time
for began.Before(deadline) {
time.Sleep(interval)
began = time.Now()
noop()
took := time.Since(began)
latencies = append(latencies, took)
}
sort.Slice(latencies, func(i, j int) bool {
return latencies[i] < latencies[j]
})
min, max := latencies[0], latencies[len(latencies)-1]
quantiles := []float64{0.50, 0.90, 0.95, 0.99}
args := []interface{}{rate, min, max}
for _, quantile := range quantiles {
nth := latencies[int(quantile*(float64(len(latencies)-1)))]
args = append(args, quantile, nth)
}
tw := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', tabwriter.StripEscape)
fmtstr := "%6d/s:\tmin: %8s\tmax: %8s" + strings.Repeat("\t%0.2f: %8s", len(quantiles))
fmt.Fprintf(tw, fmtstr+"\n", args...)
tw.Flush()
}
}
//go:noinline
func noop() {
return
}
samples: 10000 elapsed: 1032.400288857s rate: 10/s interval: 100ms min: 15ns max: 9.856µs 0.5: 117ns 0.9: 117ns 0.95: 117ns 0.99: 130ns
samples: 10000 elapsed: 117.414649751s rate: 100/s interval: 10ms min: 15ns max: 18.402µs 0.5: 117ns 0.9: 117ns 0.95: 117ns 0.99: 117ns
samples: 10000 elapsed: 12.660496068s rate: 1000/s interval: 1ms min: 17ns max: 233ns 0.5: 117ns 0.9: 117ns 0.95: 117ns 0.99: 117ns
samples: 10000 elapsed: 1.406416756s rate: 10000/s interval: 100µs min: 13ns max: 294ns 0.5: 21ns 0.9: 50ns 0.95: 51ns 0.99: 51ns
samples: 10000 elapsed: 243.163628ms rate: 100000/s interval: 10µs min: 11ns max: 9.561µs 0.5: 18ns 0.9: 50ns 0.95: 50ns 0.99: 51ns
samples: 10000 elapsed: 107.475997ms rate: 1000000/s interval: 1µs min: 11ns max: 124ns 0.5: 17ns 0.9: 18ns 0.95: 23ns 0.99: 50ns
#![feature(duration_as_u128)]
use std::thread;
use std::time::{Duration, Instant};
fn main() {
let rates = &[10, 100, 1000, 10_000, 100_000, 1_000_000]; // per second
let mut samples = [Duration::new(0, 0); 10_000];
for rate in rates {
let interval = Duration::new(1, 0) / *rate;
let began = Instant::now();
for sample in samples.iter_mut() {
thread::sleep(interval);
let start = Instant::now();
noop();
*sample = Instant::now() - start
}
let elapsed = Instant::now() - began;
samples.sort();
let min = samples.first().unwrap();
let max = samples.last().unwrap();
print!(
"samples: {}\telapsed: {:5?}\trate: {:8}/s\tinterval: {:5?}\tmin: {:5?}\tmax: {:8?}",
samples.len(),
elapsed,
rate,
interval,
min,
max
);
let len = (samples.len() - 1) as f64;
for quantile in &[0.50, 0.90, 0.95, 0.99] {
let nth = samples[(quantile * len) as usize];
print!("\t{}: {:5?}", quantile, nth);
}
println!()
}
}
#[inline(never)]
fn noop() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment