|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "image" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +type BenchmarkConfig struct { |
| 11 | + MaxIterValues []int |
| 12 | + ImgSizeValues []int |
| 13 | + loc Location |
| 14 | +} |
| 15 | + |
| 16 | +func getBenchmarkConfig() BenchmarkConfig { |
| 17 | + imgConf = ImageConfig{ |
| 18 | + Width: -1, |
| 19 | + Height: -1, |
| 20 | + Samples: 5, |
| 21 | + MaxIter: -1, |
| 22 | + Offset: 0.0, |
| 23 | + Mixing: true, |
| 24 | + InsideBlack: true, |
| 25 | + Grayscale: false, |
| 26 | + RndGlobal: uint64(time.Now().UnixNano()), |
| 27 | + } |
| 28 | + |
| 29 | + return BenchmarkConfig{ |
| 30 | + MaxIterValues: []int{10, 100, 1000, 10000}, |
| 31 | + ImgSizeValues: []int{100, 1000}, |
| 32 | + loc: Location{ |
| 33 | + XCenter: -0.5, |
| 34 | + YCenter: 0, |
| 35 | + Zoom: 1, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +func BenchmarkRenderImage(b *testing.B) { |
| 42 | + benchmarkConfig := getBenchmarkConfig() |
| 43 | + |
| 44 | + for _, size := range benchmarkConfig.ImgSizeValues { |
| 45 | + imgConf.Height = size |
| 46 | + imgConf.Width = size |
| 47 | + for _, maxIter := range benchmarkConfig.MaxIterValues { |
| 48 | + imgConf.MaxIter = maxIter |
| 49 | + testId := fmt.Sprintf("Size_%d_MaxIter_%d", size, maxIter) |
| 50 | + b.Run(testId, func(subB *testing.B) { |
| 51 | + img := image.NewRGBA(image.Rect(0, 0, imgConf.Width, imgConf.Height)) |
| 52 | + |
| 53 | + subB.ResetTimer() |
| 54 | + for i := 0; i < subB.N; i++ { |
| 55 | + renderImage(img, benchmarkConfig.loc) |
| 56 | + } |
| 57 | + }) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func BenchmarkRenderRow(b *testing.B) { |
| 63 | + benchmarkConfig := getBenchmarkConfig() |
| 64 | + |
| 65 | + for _, size := range benchmarkConfig.ImgSizeValues { |
| 66 | + imgConf.Height = size |
| 67 | + imgConf.Width = size |
| 68 | + for _, maxIter := range benchmarkConfig.MaxIterValues { |
| 69 | + imgConf.MaxIter = maxIter |
| 70 | + testId := fmt.Sprintf("Size_%d_MaxIter_%d", size, maxIter) |
| 71 | + b.Run(testId, func(subB *testing.B) { |
| 72 | + img := image.NewRGBA(image.Rect(0, 0, imgConf.Width, imgConf.Height)) |
| 73 | + subB.ResetTimer() |
| 74 | + |
| 75 | + rndLocal := RandUint64(&imgConf.RndGlobal) |
| 76 | + for i := 0; i < subB.N; i++ { |
| 77 | + renderRow(benchmarkConfig.loc, imgConf.Height/2, img, &rndLocal) |
| 78 | + } |
| 79 | + }) |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func BenchmarkGetColorForPixel(b *testing.B) { |
| 85 | + benchmarkConfig := getBenchmarkConfig() |
| 86 | + imgConf.Height = 1000 |
| 87 | + imgConf.Width = 1000 |
| 88 | + |
| 89 | + for _, maxIter := range benchmarkConfig.MaxIterValues { |
| 90 | + imgConf.MaxIter = maxIter |
| 91 | + testId := fmt.Sprintf("MaxIter_%d", maxIter) |
| 92 | + b.Run(testId, func(subB *testing.B) { |
| 93 | + subB.ResetTimer() |
| 94 | + |
| 95 | + for i := 0; i < subB.N; i++ { |
| 96 | + getColorForPixel(benchmarkConfig.loc, imgConf.Height/2, imgConf.Width/2, &imgConf.RndGlobal) |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func BenchmarkGetColorFromMandelbrotUnlimited(b *testing.B) { |
| 103 | + magnitude := 2.0 |
| 104 | + iterations := 100 |
| 105 | + b.ResetTimer() |
| 106 | + |
| 107 | + for i := 0; i < b.N; i++ { |
| 108 | + getColorFromMandelbrot(true, magnitude, iterations) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func BenchmarkRunMandelbrot(b *testing.B) { |
| 113 | + benchmarkConfig := getBenchmarkConfig() |
| 114 | + |
| 115 | + z0 := complex(-0.5, 0) // (-0.5, 0) is part of the mandelbrot set, i.e. zn is bounded for all zn |
| 116 | + for _, maxIter := range benchmarkConfig.MaxIterValues { |
| 117 | + testId := fmt.Sprintf("MaxIter_%d", maxIter) |
| 118 | + b.Run(testId, func(subB *testing.B) { |
| 119 | + imgConf.MaxIter = maxIter |
| 120 | + subB.ResetTimer() |
| 121 | + |
| 122 | + for i := 0; i < subB.N; i++ { |
| 123 | + runMandelbrot(z0) |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
0 commit comments