2020-08-06 15:28:19 +02:00
|
|
|
// Channel Benchmark
|
|
|
|
//
|
|
|
|
// `nobj` integers are sent thru a channel with queue length`buflen`
|
|
|
|
// using `nsend` sender threads and `nrec` receiver threads.
|
|
|
|
//
|
|
|
|
// The receive threads add all received numbers and send them to the
|
|
|
|
// main thread where the total sum is compare to the expected value.
|
|
|
|
import time
|
|
|
|
import os
|
2020-10-21 11:23:03 +02:00
|
|
|
import sync
|
2020-08-06 15:28:19 +02:00
|
|
|
|
2020-08-31 10:44:39 +02:00
|
|
|
fn do_rec(ch chan int, resch chan i64, n int) {
|
2020-08-06 15:28:19 +02:00
|
|
|
mut sum := i64(0)
|
|
|
|
for _ in 0 .. n {
|
2020-08-31 10:44:39 +02:00
|
|
|
sum += <-ch
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
println(sum)
|
2020-08-31 10:44:39 +02:00
|
|
|
resch <- sum
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 11:23:03 +02:00
|
|
|
fn do_send(ch chan int, start int, end int) {
|
2020-08-06 15:28:19 +02:00
|
|
|
for i in start .. end {
|
2020-08-31 10:44:39 +02:00
|
|
|
ch <- i
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
if os.args.len != 5 {
|
|
|
|
eprintln('usage:\n\t${os.args[0]} <nsend> <nrec> <buflen> <nobj>')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
nsend := os.args[1].int()
|
|
|
|
nrec := os.args[2].int()
|
|
|
|
buflen := os.args[3].int()
|
|
|
|
nobj := os.args[4].int()
|
|
|
|
stopwatch := time.new_stopwatch({})
|
2020-08-31 10:44:39 +02:00
|
|
|
ch := chan int{cap: buflen}
|
|
|
|
resch := chan i64{}
|
2020-08-06 15:28:19 +02:00
|
|
|
mut no := nobj
|
|
|
|
for i in 0 .. nrec {
|
|
|
|
n := no / (nrec - i)
|
2020-08-31 10:44:39 +02:00
|
|
|
go do_rec(ch, resch, n)
|
2020-08-06 15:28:19 +02:00
|
|
|
no -= n
|
|
|
|
}
|
|
|
|
assert no == 0
|
|
|
|
no = nobj
|
|
|
|
for i in 0 .. nsend {
|
|
|
|
n := no / (nsend - i)
|
|
|
|
end := no
|
|
|
|
no -= n
|
2020-08-31 10:44:39 +02:00
|
|
|
go do_send(ch, no, end)
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
assert no == 0
|
|
|
|
mut sum := i64(0)
|
|
|
|
for _ in 0 .. nrec {
|
2020-08-31 10:44:39 +02:00
|
|
|
sum += <-resch
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|
|
|
|
elapsed := stopwatch.elapsed()
|
2020-10-21 11:23:03 +02:00
|
|
|
rate := f64(nobj) / elapsed * time.microsecond
|
|
|
|
println('$nobj objects in ${f64(elapsed) / time.second} s (${rate:.2f} objs/µs)')
|
2020-08-06 15:28:19 +02:00
|
|
|
// use sum formula by Gauß to calculate the expected result
|
2020-10-21 11:23:03 +02:00
|
|
|
expected_sum := i64(nobj) * (nobj - 1) / 2
|
2020-08-06 15:28:19 +02:00
|
|
|
println('got: $sum, expected: $expected_sum')
|
|
|
|
assert sum == expected_sum
|
|
|
|
}
|