change: Removed chunk storage dir from being a global variable, and added some more tests

This commit is contained in:
Nicholas Novak 2023-11-08 16:24:27 -08:00
parent 378d121566
commit 1766cd4f48
6 changed files with 80 additions and 38 deletions

View File

@ -1,56 +1,68 @@
package main package main
import ( import (
"fmt"
"math/rand" "math/rand"
"os" "os"
"testing"
"time" "time"
"git.nicholasnovak.io/nnovak/spatial-db/storage" "git.nicholasnovak.io/nnovak/spatial-db/storage"
"git.nicholasnovak.io/nnovak/spatial-db/world" "git.nicholasnovak.io/nnovak/spatial-db/world"
) )
func setupStorageDir() string { func populateStorageDir(dir string, maxSpread float64, numPoints int) 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) {
var server storage.SimpleServer var server storage.SimpleServer
stdDev := 65536 server.StorageDir = dir
defer os.RemoveAll(server.StorageDir)
storage.ChunkFileDirectory = setupStorageDir() points := make([]world.BlockPos, numPoints)
defer os.RemoveAll(storage.ChunkFileDirectory)
points := make([]world.BlockPos, b.N)
r := rand.New(rand.NewSource(time.Now().UnixNano())) 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{ points[i] = world.BlockPos{
X: int(r.NormFloat64() * float64(stdDev)), X: int(r.NormFloat64() * maxSpread),
Y: uint(r.NormFloat64() * float64(stdDev)), Y: uint(r.NormFloat64() * maxSpread),
Z: int(r.NormFloat64() * float64(stdDev)), Z: int(r.NormFloat64() * maxSpread),
} }
} }
b.ResetTimer()
for _, point := range points { for _, point := range points {
if err := server.ChangeBlock(point, world.Generic); err != nil { 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))
// }

26
scaling_test.go Normal file
View File

@ -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)
}
}
}

View File

@ -7,10 +7,6 @@ import (
"git.nicholasnovak.io/nnovak/spatial-db/world" "git.nicholasnovak.io/nnovak/spatial-db/world"
) )
var (
ChunkFileDirectory = "./persistence"
)
func ReadChunkFromFile(chunkFile *os.File) (world.ChunkData, error) { func ReadChunkFromFile(chunkFile *os.File) (world.ChunkData, error) {
var chunkData world.ChunkData var chunkData world.ChunkData

View File

@ -17,12 +17,13 @@ var (
) )
type SimpleServer struct { type SimpleServer struct {
StorageDir string
} }
// Filesystem operations // Filesystem operations
func (s *SimpleServer) FetchOrCreateChunk(pos world.ChunkPos) (world.ChunkData, error) { 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 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) { 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 var chunkData world.ChunkData

View File

@ -141,11 +141,9 @@ var VisualizeChunkCommand = &cobra.Command{
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { 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 // Create a new server to read from those files
var chunkServer storage.SimpleServer var chunkServer storage.SimpleServer
chunkServer.StorageDir = args[0]
prog := tea.NewProgram(initChunkViewer(&chunkServer), tea.WithAltScreen()) prog := tea.NewProgram(initChunkViewer(&chunkServer), tea.WithAltScreen())

View File

@ -2,6 +2,7 @@ package world
import ( import (
"fmt" "fmt"
"math/rand"
) )
const ( const (
@ -15,6 +16,14 @@ type BlockPos struct {
Z int `json:"z"` 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 { func (b BlockPos) ToChunkPos() ChunkPos {
return ChunkPos{ return ChunkPos{
X: b.X / 16, X: b.X / 16,