Browse Source

Add random benchmarking code

master
Chris Smith 4 years ago
parent
commit
7217ec0cd8
Signed by: Chris Smith <chris@chameth.com> GPG Key ID: 3A2D4BBDC4A3C9A9
3 changed files with 138 additions and 0 deletions
  1. 84
    0
      02/main_test.go
  2. 17
    0
      07/main_test.go
  3. 37
    0
      09/main_test.go

+ 84
- 0
02/main_test.go View File

@@ -0,0 +1,84 @@
1
+package main
2
+
3
+import (
4
+	"github.com/csmith/aoc-2019/common"
5
+	"github.com/csmith/aoc-2019/intcode"
6
+	"runtime/debug"
7
+	"sync"
8
+	"testing"
9
+)
10
+
11
+func findOutputMany(pool *sync.Pool, input []int, target int) int {
12
+	res := make(chan int)
13
+
14
+	for n := 0; n < 100; n++ {
15
+		for v := 0; v < 100; v++ {
16
+			go func(n, v int) {
17
+				if run(pool, input, n, v) == target {
18
+					res <- 100*n + v
19
+				}
20
+			}(n, v)
21
+		}
22
+
23
+	}
24
+
25
+	return <-res
26
+}
27
+
28
+func findOutputLinear(pool *sync.Pool, input []int, target int) int {
29
+	for n := 0; n < 100; n++ {
30
+		for v := 0; v < 100; v++ {
31
+			if run(pool, input, n, v) == target {
32
+				return 100*n + v
33
+			}
34
+		}
35
+	}
36
+	panic("Should've returned")
37
+}
38
+
39
+func Benchmark_goroutine_per_pair(b *testing.B) {
40
+	debug.SetGCPercent(-1)
41
+	for i := 0; i < b.N; i++ {
42
+		input := common.ReadCsvAsInts("input.txt")
43
+
44
+		pool := &sync.Pool{
45
+			New: func() interface{} {
46
+				return intcode.NewVirtualMachine(make([]int, len(input)))
47
+			},
48
+		}
49
+
50
+		_ = run(pool, input, 12, 2)
51
+		_ = findOutputMany(pool, input, 19690720)
52
+	}
53
+}
54
+
55
+func Benchmark_goroutine_per_noun(b *testing.B) {
56
+	debug.SetGCPercent(-1)
57
+	for i := 0; i < b.N; i++ {
58
+		input := common.ReadCsvAsInts("input.txt")
59
+
60
+		pool := &sync.Pool{
61
+			New: func() interface{} {
62
+				return intcode.NewVirtualMachine(make([]int, len(input)))
63
+			},
64
+		}
65
+
66
+		_ = run(pool, input, 12, 2)
67
+		_ = findOutput(pool, input, 19690720)
68
+	}
69
+}
70
+
71
+func Benchmark_no_goroutines(b *testing.B) {
72
+	for i := 0; i < b.N; i++ {
73
+		input := common.ReadCsvAsInts("input.txt")
74
+
75
+		pool := &sync.Pool{
76
+			New: func() interface{} {
77
+				return intcode.NewVirtualMachine(make([]int, len(input)))
78
+			},
79
+		}
80
+
81
+		_ = run(pool, input, 12, 2)
82
+		_ = findOutputLinear(pool, input, 19690720)
83
+	}
84
+}

+ 17
- 0
07/main_test.go View File

@@ -0,0 +1,17 @@
1
+package main
2
+
3
+import (
4
+	"github.com/csmith/aoc-2019/common"
5
+	"runtime/debug"
6
+	"testing"
7
+)
8
+
9
+func Benchmark(b *testing.B) {
10
+	debug.SetGCPercent(-1)
11
+	for i := 0; i < b.N; i++ {
12
+		input := common.ReadCsvAsInts("input.txt")
13
+		memoryBanks := make([]int, len(input)*5)
14
+		_ = maxOutput(memoryBanks, input, []int{0, 1, 2, 3, 4}, false)
15
+		_ = maxOutput(memoryBanks, input, []int{5, 6, 7, 8, 9}, true)
16
+	}
17
+}

+ 37
- 0
09/main_test.go View File

@@ -0,0 +1,37 @@
1
+package main
2
+
3
+import (
4
+	"github.com/csmith/aoc-2019/common"
5
+	"github.com/csmith/aoc-2019/intcode"
6
+	"testing"
7
+)
8
+
9
+func Benchmark_Part1(b *testing.B) {
10
+	for i := 0; i < b.N; i++ {
11
+		input := common.ReadCsvAsInts("input.txt")
12
+		memory := make([]int, len(input))
13
+		copy(memory, input)
14
+
15
+		vm := intcode.NewVirtualMachine(memory)
16
+		vm.Input = make(chan int, 1)
17
+		vm.Output = make(chan int, 1)
18
+		vm.Input <- 1
19
+		go vm.Run()
20
+		_ = last(vm.Output)
21
+	}
22
+}
23
+
24
+func Benchmark_Part2(b *testing.B) {
25
+	for i := 0; i < b.N; i++ {
26
+		input := common.ReadCsvAsInts("input.txt")
27
+		memory := make([]int, len(input))
28
+		copy(memory, input)
29
+
30
+		vm := intcode.NewVirtualMachine(memory)
31
+		vm.Input = make(chan int, 1)
32
+		vm.Output = make(chan int, 1)
33
+		vm.Input <- 2
34
+		go vm.Run()
35
+		_ = last(vm.Output)
36
+	}
37
+}

Loading…
Cancel
Save