aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/sync/bench/channel_bench_go.go
blob: a0afbbcee114fa7053fc3f7ab32e3633112ffedd (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
65
66
67
68
package main

import "fmt"
import "log"
import "os"
import "time"
import "strconv"

func assert_eq(a, b int64) {
	if a != b {
		log.Fatalf("assertion failed\nleft: %d, right: %d\n", a, b)
	}
}

func do_rec(ch chan int32, resch chan int64, n int32) {
	var sum int64
	var i int32
	for i = 0; i < n; i++ {
		sum += int64(<- ch)
	}
	fmt.Println(sum)
	resch <- sum
}

func do_send(ch chan int32, start, end int32) {
	for i := start; i < end; i++ {
		ch <- i
	}
}

func main() {
	if len(os.Args) != 5 {
		log.Fatalf("usage:\n\t%s <nsend> <nrec> <buflen> <nobj>\n", os.Args[0])
	}
	nsend, _ := strconv.Atoi(os.Args[1])
	nrec, _ := strconv.Atoi(os.Args[2])
	buflen, _ := strconv.Atoi(os.Args[3])
	nobj, _ := strconv.Atoi(os.Args[4])
	stopwatch := time.Now()
	ch := make(chan int32, buflen)
	resch := make(chan int64, 0)
	no := nobj
	for i := 0; i < nrec; i++ {
		n := no / (nrec - i)
		go do_rec(ch, resch, int32(n))
		no -= n
	}
	assert_eq(int64(no), 0)	
	no = nobj
	for i := 0; i < nsend; i++ {
		n := no / (nsend - i)
		end := no
		no -= n
		go do_send(ch, int32(no), int32(end))
	}
	assert_eq(int64(no), 0)	
	var sum int64
	for i := 0; i < nrec; i++ {
		sum += <-resch
	}
	elapsed := time.Now().Sub(stopwatch)
	rate := float64(nobj)/float64(elapsed.Nanoseconds())*1000.0
	duration := 1.0e-09 * float64(elapsed.Nanoseconds())
	fmt.Printf("%d objects in %g s (%.2f objs/µs)\n", nobj, duration, rate)
	expected_sum := int64(nobj)*int64(nobj-1)/2
	fmt.Printf("got: %d, expected: %d\n", sum, expected_sum)
	assert_eq(sum, expected_sum)
}