package mapblockrenderer import ( "io/ioutil" "mapserver/db/sqlite" "mapserver/mapblockaccessor" "mapserver/testutils" "mapserver/types" "os" "testing" "time" "github.com/minetest-go/colormapping" "github.com/sirupsen/logrus" ) type callback func() func createRenderer(b *testing.B) (*MapBlockRenderer, callback) { logrus.SetLevel(logrus.PanicLevel) tmpfile, err := ioutil.TempFile("", "TestMigrate.*.sqlite") if err != nil { panic(err) } cleanup := func() { os.Remove(tmpfile.Name()) } testutils.CreateTestDatabase(tmpfile.Name()) a, err := sqlite.New(tmpfile.Name()) if err != nil { panic(err) } err = a.Migrate() if err != nil { panic(err) } cache := mapblockaccessor.NewMapBlockAccessor(a, 500*time.Millisecond, 1000*time.Millisecond, 1000) c := colormapping.NewColorMapping() err = c.LoadDefaults() if err != nil { b.Fatal(err) } r := NewMapBlockRenderer(cache, c) b.ResetTimer() return r, cleanup } func BenchmarkRenderEmptySingle(b *testing.B) { r, cleanup := createRenderer(b) defer cleanup() for n := 0; n < b.N; n++ { pos1 := types.NewMapBlockCoords(10, 0, 10) pos2 := types.NewMapBlockCoords(10, 0, 10) _, err := r.Render(pos1, pos2) if err != nil { panic(err) } } } func BenchmarkRenderSingle(b *testing.B) { r, cleanup := createRenderer(b) defer cleanup() for n := 0; n < b.N; n++ { pos1 := types.NewMapBlockCoords(0, 0, 0) pos2 := types.NewMapBlockCoords(0, 0, 0) _, err := r.Render(pos1, pos2) if err != nil { panic(err) } } } func BenchmarkRenderStride(b *testing.B) { r, cleanup := createRenderer(b) defer cleanup() for n := 0; n < b.N; n++ { pos1 := types.NewMapBlockCoords(0, 10, 0) pos2 := types.NewMapBlockCoords(0, -1, 0) _, err := r.Render(pos1, pos2) if err != nil { panic(err) } } } func BenchmarkRenderBigStride(b *testing.B) { r, cleanup := createRenderer(b) defer cleanup() for n := 0; n < b.N; n++ { pos1 := types.NewMapBlockCoords(0, 1000, 0) pos2 := types.NewMapBlockCoords(0, -1000, 0) _, err := r.Render(pos1, pos2) if err != nil { panic(err) } } }