sync: a page is stored in one transaction with parallel verification; a sync is cut off by idleness, not a fixed 2 minutes
20,000 listings to a newcomer: 120 s (unfinished) -> 24 s. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
||||
)
|
||||
|
||||
// PutMany gives the same outcomes and the same database as Put one by one,
|
||||
// refused listings included, and a refused one does not stop the rest.
|
||||
func TestPutManyDoesWhatPutDoes(t *testing.T) {
|
||||
e := newQuotaEnv(t, 3)
|
||||
old := e.listing(t, nil)
|
||||
e.put(t, old)
|
||||
|
||||
upd := e.listing(t, func(l *listing.Listing) { l.ID, l.Seq, l.Title = old.ID, 2, "Neu" })
|
||||
fresh := e.listing(t, nil)
|
||||
bad := e.listing(t, nil)
|
||||
bad.Title = "changed after signing" // the signature no longer fits
|
||||
late := e.listing(t, nil) // the quota is 3: old, fresh, and one more fit
|
||||
over := e.listing(t, nil)
|
||||
|
||||
res, err := e.s.PutMany(ctx, []*listing.Listing{upd, fresh, bad, late, over, old})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []struct {
|
||||
out Outcome
|
||||
err error
|
||||
}{
|
||||
{Replaced, nil}, {Inserted, nil}, {Ignored, ErrInvalid}, {Inserted, nil}, {Ignored, ErrQuota}, {Ignored, nil}, // old (seq 1) loses to upd
|
||||
}
|
||||
for i, w := range want {
|
||||
if res[i].Outcome != w.out || !errors.Is(res[i].Err, w.err) && (w.err != nil || res[i].Err != nil) {
|
||||
t.Errorf("listing %d: got (%v, %v), want (%v, %v)", i, res[i].Outcome, res[i].Err, w.out, w.err)
|
||||
}
|
||||
}
|
||||
if n, _ := e.s.Count(ctx); n != 3 {
|
||||
t.Fatalf("the store holds %d listings, want 3", n)
|
||||
}
|
||||
got, err := e.s.Get(ctx, old.Author, old.ID)
|
||||
if err != nil || got.Title != "Neu" {
|
||||
t.Fatalf("the update did not win: %v %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPutManyOfNothingIsFine(t *testing.T) {
|
||||
e := newQuotaEnv(t, 3)
|
||||
if res, err := e.s.PutMany(ctx, nil); err != nil || len(res) != 0 {
|
||||
t.Fatalf("got %v, %v", res, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Logs how long 2000 listings take on a database file, one transaction each
|
||||
// and in pages of 100 (go test -v -run PutManyIsFaster ./internal/store).
|
||||
func TestPutManyIsFasterThanPut(t *testing.T) {
|
||||
e := newQuotaEnv(t, -1)
|
||||
ls := make([]*listing.Listing, 2000)
|
||||
for i := range ls {
|
||||
ls[i] = e.listing(t, nil)
|
||||
}
|
||||
took := map[string]time.Duration{}
|
||||
for _, mode := range []string{"Put", "PutMany"} {
|
||||
s, err := Open(t.TempDir()+"/"+mode+".db", Options{Difficulty: difficulty, MaxPerAuthor: -1, Clock: func() time.Time { return e.now }})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
start := time.Now()
|
||||
if mode == "Put" {
|
||||
for _, l := range ls {
|
||||
if _, err := s.Put(ctx, l); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for i := 0; i < len(ls); i += 100 {
|
||||
if _, err := s.PutMany(ctx, ls[i:i+100]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
took[mode] = time.Since(start)
|
||||
if n, _ := s.Count(ctx); n != len(ls) {
|
||||
t.Fatalf("%s stored %d of %d", mode, n, len(ls))
|
||||
}
|
||||
s.Close()
|
||||
}
|
||||
t.Logf("2000 listings: Put %v (%.0f/s), PutMany %v (%.0f/s)",
|
||||
took["Put"].Round(time.Millisecond), 2000/took["Put"].Seconds(),
|
||||
took["PutMany"].Round(time.Millisecond), 2000/took["PutMany"].Seconds())
|
||||
}
|
||||
Reference in New Issue
Block a user