2023-10-28 21:40:29 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-11-12 12:33:39 -08:00
|
|
|
"errors"
|
|
|
|
"io/fs"
|
2023-10-28 21:40:29 -07:00
|
|
|
"math/rand"
|
2023-10-29 01:23:38 -07:00
|
|
|
"os"
|
2023-10-28 21:40:29 -07:00
|
|
|
"time"
|
|
|
|
|
2023-11-12 12:33:39 -08:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-12-10 21:46:15 -08:00
|
|
|
"git.nicholasnovak.io/nnovak/spatial-db/server"
|
2023-10-28 21:40:29 -07:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2023-12-10 21:46:15 -08:00
|
|
|
var server server.SimpleServer
|
2023-10-28 21:40:29 -07:00
|
|
|
|
2023-11-12 12:33:39 -08:00
|
|
|
server.StorageDir = dirName
|
|
|
|
if cleanup {
|
|
|
|
defer os.RemoveAll(server.StorageDir)
|
|
|
|
}
|
2023-10-29 01:23:38 -07:00
|
|
|
|
2023-11-08 16:24:27 -08:00
|
|
|
points := make([]world.BlockPos, numPoints)
|
2023-10-28 21:40:29 -07:00
|
|
|
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
2023-11-08 16:24:27 -08:00
|
|
|
for i := 0; i < numPoints; i++ {
|
2023-10-28 21:40:29 -07:00
|
|
|
points[i] = world.BlockPos{
|
2023-11-08 16:24:27 -08:00
|
|
|
X: int(r.NormFloat64() * maxSpread),
|
|
|
|
Y: uint(r.NormFloat64() * maxSpread),
|
|
|
|
Z: int(r.NormFloat64() * maxSpread),
|
2023-10-28 21:40:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, point := range points {
|
|
|
|
if err := server.ChangeBlock(point, world.Generic); err != nil {
|
2023-11-08 16:24:27 -08:00
|
|
|
panic(err)
|
2023-10-28 21:40:29 -07:00
|
|
|
}
|
|
|
|
}
|
2023-11-12 12:33:39 -08:00
|
|
|
log.Info("Done generating")
|
2023-10-28 21:40:29 -07:00
|
|
|
}
|
2023-11-08 16:24:27 -08:00
|
|
|
|
|
|
|
// 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))
|
|
|
|
// }
|