aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/sync/bench/channel_bench_v.v
blob: 54dcfe974b833b2b77165935121110b92c64b29b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 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

fn do_rec(ch chan int, resch chan i64, n int) {
	mut sum := i64(0)
	for _ in 0 .. n {
		sum += <-ch
	}
	println(sum)
	resch <- sum
}

fn do_send(ch chan int, start int, end int) {
	for i in start .. end {
		ch <- i
	}
}

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()
	ch := chan int{cap: buflen}
	resch := chan i64{}
	mut no := nobj
	for i in 0 .. nrec {
		n := no / (nrec - i)
		go do_rec(ch, resch, n)
		no -= n
	}
	assert no == 0
	no = nobj
	for i in 0 .. nsend {
		n := no / (nsend - i)
		end := no
		no -= n
		go do_send(ch, no, end)
	}
	assert no == 0
	mut sum := i64(0)
	for _ in 0 .. nrec {
		sum += <-resch
	}
	elapsed := stopwatch.elapsed()
	rate := f64(nobj) / elapsed * time.microsecond
	println('$nobj objects in ${f64(elapsed) / time.second} s (${rate:.2f} objs/µs)')
	// use sum formula by Gauß to calculate the expected result
	expected_sum := i64(nobj) * (nobj - 1) / 2
	println('got: $sum, expected: $expected_sum')
	assert sum == expected_sum
}