change: Removed chunk storage dir from being a global variable, and added some more tests
This commit is contained in:
		@@ -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))
 | 
			
		||||
// }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										26
									
								
								scaling_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								scaling_test.go
									
									
									
									
									
										Normal 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)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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())
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user