spatial-db/basic_functionality_test.go

96 lines
2.0 KiB
Go
Raw Normal View History

package main
import (
2023-11-12 12:33:39 -08:00
"errors"
"io/fs"
"math/rand"
"os"
"time"
2023-11-12 12:33:39 -08:00
log "github.com/sirupsen/logrus"
"git.nicholasnovak.io/nnovak/spatial-db/server"
"git.nicholasnovak.io/nnovak/spatial-db/world"
)
2023-11-12 12:33:39 -08:00
func populateStorageDir(
dirName string,
maxSpread float64,
numPoints int,
cleanup bool,
) {
log.Debug("Generating new storage directory")
// Make sure that another directory is not already at that location
if _, err := os.Stat(dirName); err != nil {
if errors.Is(err, fs.ErrNotExist) {
log.Debugf("Making new directory at %s", dirName)
if err := os.Mkdir(dirName, 0755); err != nil {
panic(err)
}
} else {
panic(err)
}
} else {
log.Debug("Directory already exists, skipping generation")
return
}
var server server.SimpleServer
2023-11-12 12:33:39 -08:00
server.StorageDir = dirName
if cleanup {
defer os.RemoveAll(server.StorageDir)
}
points := make([]world.BlockPos, numPoints)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < numPoints; i++ {
points[i] = world.BlockPos{
X: int(r.NormFloat64() * maxSpread),
Y: uint(r.NormFloat64() * maxSpread),
Z: int(r.NormFloat64() * maxSpread),
}
}
for _, point := range points {
if err := server.ChangeBlock(point, world.Generic); err != nil {
panic(err)
}
}
2023-11-12 12:33:39 -08:00
log.Info("Done generating")
}
// 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))
// }