From 1766cd4f481af46bfcb8e63232350de4d127ac48 Mon Sep 17 00:00:00 2001 From: Nicholas Novak <34256932+NickyBoy89@users.noreply.github.com> Date: Wed, 8 Nov 2023 16:24:27 -0800 Subject: [PATCH] change: Removed chunk storage dir from being a global variable, and added some more tests --- basic_functionality_test.go | 70 +++++++++++++++++++------------- scaling_test.go | 26 ++++++++++++ storage/file_operations.go | 4 -- storage/simple_server.go | 5 ++- visualization/visualize_chunk.go | 4 +- world/data_format.go | 9 ++++ 6 files changed, 80 insertions(+), 38 deletions(-) create mode 100644 scaling_test.go diff --git a/basic_functionality_test.go b/basic_functionality_test.go index 4e84796..3719ca3 100644 --- a/basic_functionality_test.go +++ b/basic_functionality_test.go @@ -1,56 +1,68 @@ package main import ( - "fmt" "math/rand" "os" - "testing" "time" "git.nicholasnovak.io/nnovak/spatial-db/storage" "git.nicholasnovak.io/nnovak/spatial-db/world" ) -func setupStorageDir() string { - dir, err := os.MkdirTemp("", "spatial-db-persistence") - if err != nil { - panic(err) - } - - fmt.Printf("Temporary directory is at %s\n", dir) - - storage.ChunkFileDirectory = dir - - return dir -} - -func BenchmarkInsertSomePoints(b *testing.B) { +func populateStorageDir(dir string, maxSpread float64, numPoints int) string { var server storage.SimpleServer - stdDev := 65536 + server.StorageDir = dir + defer os.RemoveAll(server.StorageDir) - storage.ChunkFileDirectory = setupStorageDir() - defer os.RemoveAll(storage.ChunkFileDirectory) - - points := make([]world.BlockPos, b.N) + points := make([]world.BlockPos, numPoints) r := rand.New(rand.NewSource(time.Now().UnixNano())) - for i := 0; i < b.N; i++ { + for i := 0; i < numPoints; i++ { points[i] = world.BlockPos{ - X: int(r.NormFloat64() * float64(stdDev)), - Y: uint(r.NormFloat64() * float64(stdDev)), - Z: int(r.NormFloat64() * float64(stdDev)), + X: int(r.NormFloat64() * maxSpread), + Y: uint(r.NormFloat64() * maxSpread), + Z: int(r.NormFloat64() * maxSpread), } } - b.ResetTimer() - for _, point := range points { if err := server.ChangeBlock(point, world.Generic); err != nil { - b.Error(err) + panic(err) } } - fmt.Println(os.ReadDir(storage.ChunkFileDirectory)) + return server.StorageDir } + +// func BenchmarkInsertSomePoints(b *testing.B) { +// var server storage.SimpleServer +// +// stdDev := 65536 +// +// storage.ChunkFileDirectory = setupStorageDir() +// defer os.RemoveAll(storage.ChunkFileDirectory) +// +// points := make([]world.BlockPos, b.N) +// +// r := rand.New(rand.NewSource(time.Now().UnixNano())) +// +// for i := 0; i < b.N; i++ { +// points[i] = world.BlockPos{ +// X: int(r.NormFloat64() * float64(stdDev)), +// Y: uint(r.NormFloat64() * float64(stdDev)), +// Z: int(r.NormFloat64() * float64(stdDev)), +// } +// } +// +// b.ResetTimer() +// +// for _, point := range points { +// if err := server.ChangeBlock(point, world.Generic); err != nil { +// b.Error(err) +// } +// } +// +// fmt.Println(os.ReadDir(storage.ChunkFileDirectory)) +// } diff --git a/scaling_test.go b/scaling_test.go new file mode 100644 index 0000000..e7f5a01 --- /dev/null +++ b/scaling_test.go @@ -0,0 +1,26 @@ +package main + +import ( + "testing" + + "git.nicholasnovak.io/nnovak/spatial-db/storage" + "git.nicholasnovak.io/nnovak/spatial-db/world" +) + +func BenchmarkInsertSparsePoints(b *testing.B) { + var server storage.SimpleServer + + tempDir := "./data" + + server.StorageDir = populateStorageDir(tempDir, 2048, 1_000) + b.ResetTimer() + + b.Log("Finished generating directory") + + for i := 0; i < b.N; i++ { + pos := world.RandomBlockPosWithRange(2048) + if err := server.ChangeBlock(pos, world.Generic); err != nil { + b.Error(err) + } + } +} diff --git a/storage/file_operations.go b/storage/file_operations.go index 77d5108..c7a191c 100644 --- a/storage/file_operations.go +++ b/storage/file_operations.go @@ -7,10 +7,6 @@ import ( "git.nicholasnovak.io/nnovak/spatial-db/world" ) -var ( - ChunkFileDirectory = "./persistence" -) - func ReadChunkFromFile(chunkFile *os.File) (world.ChunkData, error) { var chunkData world.ChunkData diff --git a/storage/simple_server.go b/storage/simple_server.go index 699c428..9301ac3 100644 --- a/storage/simple_server.go +++ b/storage/simple_server.go @@ -17,12 +17,13 @@ var ( ) type SimpleServer struct { + StorageDir string } // Filesystem operations func (s *SimpleServer) FetchOrCreateChunk(pos world.ChunkPos) (world.ChunkData, error) { - chunkFileName := filepath.Join(ChunkFileDirectory, pos.ToFileName()) + chunkFileName := filepath.Join(s.StorageDir, pos.ToFileName()) var chunkData world.ChunkData @@ -53,7 +54,7 @@ func (s *SimpleServer) FetchOrCreateChunk(pos world.ChunkPos) (world.ChunkData, } func (s *SimpleServer) FetchChunk(pos world.ChunkPos) (world.ChunkData, error) { - chunkFileName := filepath.Join(ChunkFileDirectory, pos.ToFileName()) + chunkFileName := filepath.Join(s.StorageDir, pos.ToFileName()) var chunkData world.ChunkData diff --git a/visualization/visualize_chunk.go b/visualization/visualize_chunk.go index 0f9bd01..afc41d3 100644 --- a/visualization/visualize_chunk.go +++ b/visualization/visualize_chunk.go @@ -141,11 +141,9 @@ var VisualizeChunkCommand = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - // Initialize the server in the specified directory - storage.ChunkFileDirectory = args[0] - // Create a new server to read from those files var chunkServer storage.SimpleServer + chunkServer.StorageDir = args[0] prog := tea.NewProgram(initChunkViewer(&chunkServer), tea.WithAltScreen()) diff --git a/world/data_format.go b/world/data_format.go index c508438..ef3fd40 100644 --- a/world/data_format.go +++ b/world/data_format.go @@ -2,6 +2,7 @@ package world import ( "fmt" + "math/rand" ) const ( @@ -15,6 +16,14 @@ type BlockPos struct { Z int `json:"z"` } +func RandomBlockPosWithRange(maxRange float64) BlockPos { + return BlockPos{ + X: int(rand.NormFloat64() * maxRange), + Y: uint(rand.NormFloat64() * maxRange), + Z: int(rand.NormFloat64() * maxRange), + } +} + func (b BlockPos) ToChunkPos() ChunkPos { return ChunkPos{ X: b.X / 16,