1 | package main |
---|
2 | |
---|
3 | import ( |
---|
4 | "context" |
---|
5 | "flag" |
---|
6 | "fmt" |
---|
7 | "math/rand" |
---|
8 | "sync" |
---|
9 | "sync/atomic" |
---|
10 | "time" |
---|
11 | "golang.org/x/sync/semaphore" |
---|
12 | "golang.org/x/text/language" |
---|
13 | "golang.org/x/text/message" |
---|
14 | ) |
---|
15 | |
---|
16 | func churner(result chan uint64, start *sync.WaitGroup, skip bool, spots [] * semaphore.Weighted) { |
---|
17 | ctx := context.TODO() |
---|
18 | s := rand.NewSource(time.Now().UnixNano()) |
---|
19 | rng := rand.New(s) |
---|
20 | |
---|
21 | count := uint64(0) |
---|
22 | start.Wait() |
---|
23 | for true { |
---|
24 | |
---|
25 | sem := spots[ rng.Intn(100) % len(spots) ]; |
---|
26 | if !skip { sem.Release(1); }; |
---|
27 | sem.Acquire(ctx,1); |
---|
28 | skip = false; |
---|
29 | |
---|
30 | count += 1 |
---|
31 | if clock_mode && atomic.LoadInt32(&stop) == 1 { break } |
---|
32 | if !clock_mode && count >= stop_count { break } |
---|
33 | } |
---|
34 | |
---|
35 | atomic.AddInt64(&threads_left, -1); |
---|
36 | result <- count |
---|
37 | } |
---|
38 | |
---|
39 | func main() { |
---|
40 | var spot_cnt int |
---|
41 | |
---|
42 | spot_cntOpt := flag.Int("s", 1, "Number of spots in the system") |
---|
43 | |
---|
44 | bench_init() |
---|
45 | |
---|
46 | spot_cnt = *spot_cntOpt |
---|
47 | |
---|
48 | threads_left = int64(nthreads) |
---|
49 | |
---|
50 | result := make(chan uint64) |
---|
51 | var wg sync.WaitGroup |
---|
52 | wg.Add(1) |
---|
53 | |
---|
54 | spots := make([] * semaphore.Weighted, spot_cnt) |
---|
55 | for i := range spots { |
---|
56 | ctx := context.TODO() |
---|
57 | spots[i] = semaphore.NewWeighted(20000) |
---|
58 | spots[i].Acquire(ctx, 20000) |
---|
59 | } |
---|
60 | |
---|
61 | for i := 0; i < nthreads; i++ { |
---|
62 | go churner(result, &wg, i < len(spots), spots) |
---|
63 | } |
---|
64 | fmt.Printf("Starting\n"); |
---|
65 | atomic.StoreInt32(&stop, 0) |
---|
66 | start := time.Now() |
---|
67 | wg.Done(); |
---|
68 | wait(start, true); |
---|
69 | |
---|
70 | atomic.StoreInt32(&stop, 1) |
---|
71 | end := time.Now() |
---|
72 | duration := end.Sub(start) |
---|
73 | |
---|
74 | fmt.Printf("\nDone\n") |
---|
75 | |
---|
76 | for i := range spots { |
---|
77 | spots[i].Release(10000) |
---|
78 | } |
---|
79 | |
---|
80 | global_counter := uint64(0) |
---|
81 | for i := 0; i < nthreads; i++ { |
---|
82 | global_counter += <- result |
---|
83 | } |
---|
84 | |
---|
85 | p := message.NewPrinter(language.English) |
---|
86 | p.Printf("Duration (ms) : %d\n", duration.Milliseconds()) |
---|
87 | p.Printf("Number of processors : %d\n", nprocs); |
---|
88 | p.Printf("Number of threads : %d\n", nthreads); |
---|
89 | p.Printf("Number of spots : %d\n", spot_cnt); |
---|
90 | p.Printf("Total Operations(ops): %15d\n", global_counter); |
---|
91 | // p.Printf("Total blocks : %15d\n", global_blocks); |
---|
92 | p.Printf("Ops per second : %18.2f\n", float64(global_counter) / duration.Seconds()); |
---|
93 | p.Printf("ns per ops : %18.2f\n", float64(duration.Nanoseconds()) / float64(global_counter)) |
---|
94 | p.Printf("Ops per threads : %15d\n", global_counter / uint64(nthreads)) |
---|
95 | p.Printf("Ops per procs : %15d\n", global_counter / uint64(nprocs)) |
---|
96 | p.Printf("Ops/sec/procs : %18.2f\n", (float64(global_counter) / float64(nprocs)) / duration.Seconds()) |
---|
97 | p.Printf("ns per ops/procs : %18.2f\n", float64(duration.Nanoseconds()) / (float64(global_counter) / float64(nprocs))) |
---|
98 | } |
---|