// Command allmende-load fills a node's data directory with many invented but // validly signed listings, for capacity tests: how large the database gets, // what a node holding them costs in memory, how fast it searches, and how a // newcomer catches up from it. // // allmende init --data DIR --no-phrase // allmende-load --data DIR -n 100000 -difficulty 8 // allmende run --data DIR --difficulty 8 ... // // The listings come from many authors (so the store's per-author keys look // like a real network), are spread over the area around a point, and are dated // over the last ten days. Every description says the listing is invented. // // Proof-of-work is the expensive part of making a listing at the network's // real difficulty; -mine-only measures it without storing anything. package main import ( "context" "flag" "fmt" "math" "math/rand/v2" "os" "path/filepath" "runtime" "strings" "sync" "time" "git.stinnesbeck.com/nils/allmende/internal/identity" "git.stinnesbeck.com/nils/allmende/internal/listing" "git.stinnesbeck.com/nils/allmende/internal/places" "git.stinnesbeck.com/nils/allmende/internal/store" ) func main() { data := flag.String("data", "", "data directory of a node made with 'allmende init' (its identity is not used; the database is filled)") n := flag.Int("n", 100000, "listings to make") difficulty := flag.Int("difficulty", 8, "proof-of-work bits; the node that serves them must use the same (the network's is "+fmt.Sprint(listing.DefaultDifficulty)+")") authors := flag.Int("authors", 1000, "distinct authors to spread the listings over") lat := flag.Float64("lat", 50.59, "centre latitude (default: Mechernich)") lon := flag.Float64("lon", 6.65, "centre longitude") spread := flag.Float64("spread-km", 25, "radius around the centre, in km") workers := flag.Int("workers", runtime.NumCPU(), "parallel miners") seed := flag.Uint64("seed", 1, "same seed, same listings") mineOnly := flag.Bool("mine-only", false, "only make and sign the listings and report how long that took; store nothing") countOnly := flag.Bool("count", false, "make nothing: print how many live listings --data holds in each region around the centre") flag.Parse() if *n <= 0 || *authors <= 0 || *workers <= 0 || (*data == "" && !*mineOnly) { fmt.Fprintln(os.Stderr, "usage: allmende-load --data DIR [-n 100000] [-difficulty 8] [-authors 1000] [-lat 50.59 -lon 6.65 -spread-km 25] [-seed 1] [-mine-only | -count]") os.Exit(2) } if *countOnly { if err := count(*data, *difficulty, *lat, *lon, *spread); err != nil { fmt.Fprintln(os.Stderr, "allmende-load:", err) os.Exit(1) } return } if err := run(*data, *n, *difficulty, *authors, *lat, *lon, *spread, *workers, *seed, *mineOnly); err != nil { fmt.Fprintln(os.Stderr, "allmende-load:", err) os.Exit(1) } } // count prints the live listings per region around the centre, which is what a // node following those regions would hold and what a newcomer should receive. func count(data string, difficulty int, lat, lon, spread float64) error { st, err := store.Open(filepath.Join(data, "allmende.db"), store.Options{Difficulty: difficulty}) if err != nil { return err } defer st.Close() total := 0 for _, cell := range places.CellsNear(lat, lon, spread+10) { d, err := st.ListingDigest(context.Background(), cell) if err != nil { return err } fmt.Printf("%s %d\n", cell, d.Count) total += d.Count } fmt.Printf("total %d\n", total) return nil } func run(data string, n, difficulty, authors int, lat, lon, spread float64, workers int, seed uint64, mineOnly bool) error { now := time.Now() // Devices certified 25 days ago, so listings dated up to 10 days back are // inside the certificate's term and still live for another 10+ days. devs := make([]*identity.Device, authors) for i := range devs { root, _, err := identity.NewRoot() if err != nil { return err } if devs[i], err = identity.Enroll(root, now.Add(-25*24*time.Hour), 90*24*time.Hour); err != nil { return err } } var st *store.Store if !mineOnly { var err error st, err = store.Open(filepath.Join(data, "allmende.db"), store.Options{Difficulty: difficulty}) if err != nil { return err } defer st.Close() } type job struct{ i int } jobs := make(chan job) made := make(chan *listing.Listing, workers*4) var wg sync.WaitGroup var mineErr error var errOnce sync.Once for w := 0; w < workers; w++ { wg.Add(1) go func() { defer wg.Done() for j := range jobs { rng := rand.New(rand.NewPCG(seed, uint64(j.i))) l, err := invent(rng, now, lat, lon, spread) if err == nil { err = l.Seal(devs[j.i%authors], difficulty) } if err != nil { errOnce.Do(func() { mineErr = err }) return } made <- l } }() } go func() { for i := 0; i < n; i++ { jobs <- job{i} } close(jobs) wg.Wait() close(made) }() start := time.Now() done, bytes := 0, 0 ctx := context.Background() for l := range made { if st != nil { if _, err := st.Put(ctx, l); err != nil { return fmt.Errorf("listing %d: %w", done, err) } } if b, err := l.Bytes(); err == nil { bytes += len(b) } done++ if done%5000 == 0 || done == n { el := time.Since(start) fmt.Printf("%7d / %d %6.0f listings/s %s\n", done, n, float64(done)/el.Seconds(), el.Round(time.Second)) } } if mineErr != nil { return mineErr } el := time.Since(start) fmt.Printf("\n%d listings, difficulty %d, %d workers: %s (%.1f ms of CPU each, %.0f listings/s)\n", done, difficulty, workers, el.Round(time.Millisecond), float64(workers)*el.Seconds()*1000/float64(done), float64(done)/el.Seconds()) fmt.Printf("average size on the wire: %d bytes\n", bytes/max(done, 1)) if st != nil { if info, err := os.Stat(filepath.Join(data, "allmende.db")); err == nil { fmt.Printf("database file: %.1f MB\n", float64(info.Size())/1e6) } } return nil } var ( moods = []string{"Helle", "Ruhige", "Gepflegte", "Moderne", "Gemütliche", "Sanierte", "Geräumige", "Zentrale", "Sonnige", "Familienfreundliche"} places_ = []string{"Mechernich", "Kommern", "Satzvey", "Vussem", "Strempt", "Lessenich", "Antweiler", "Bleibuir", "Firmenich", "Holzheim", "Kallmuth", "Eiserfey", "Weyer", "Harzheim", "Euskirchen", "Zülpich", "Bad Münstereifel", "Schleiden", "Kall", "Blankenheim"} feats = []string{"Balkon", "Terrasse", "Garten", "Garage", "Stellplatz", "Keller", "Aufzug", "Einbauküche", "Fußbodenheizung", "Neubau", "Altbau", "Barrierefrei", "Haustiere erlaubt", "Photovoltaik", "Kamin", "Dachboden", "Gäste-WC"} words = strings.Fields("Die Wohnung liegt ruhig und doch zentral, mit guter Anbindung an Bus und Bahn. Einkaufsmöglichkeiten, Schulen und Ärzte sind zu Fuß erreichbar. Der Grundriss ist durchdacht, die Räume sind hell und haben große Fenster. Böden aus Parkett oder Fliesen, Bad mit Dusche und Fenster, Küche gerne mit Möbeln. Der Keller bietet Platz für Fahrräder und Vorräte. Die Heizung wurde erneuert, die Nebenkosten sind gering. Besichtigung nach Absprache, Kaution nach Vereinbarung, kein Makler.") nouns = map[string]string{"apartment": "Wohnung", "house": "Haus", "room": "Zimmer", "commercial": "Gewerbefläche"} ) func between(r *rand.Rand, a, b int) int { return a + r.IntN(b-a+1) } func invent(r *rand.Rand, now time.Time, lat0, lon0, spread float64) (*listing.Listing, error) { id, err := listing.NewID() if err != nil { return nil, err } cat := []string{"apartment", "apartment", "apartment", "house", "house", "room", "commercial"}[r.IntN(7)] kind := "rent" if cat != "room" && r.IntN(10) < 3 { kind = "sale" } area := map[string][2]int{"apartment": {30, 120}, "house": {90, 220}, "room": {10, 30}, "commercial": {30, 300}}[cat] m2 := between(r, area[0], area[1]) euros := m2 * 10 if kind == "sale" { euros = m2 * 2800 } rad, ang := spread*math.Sqrt(r.Float64()), r.Float64()*2*math.Pi lat := lat0 + rad*math.Cos(ang)/111 lon := lon0 + rad*math.Sin(ang)/(111*math.Cos(lat0*math.Pi/180)) var fs []string for i := between(r, 0, 5); i > 0; i-- { f := feats[r.IntN(len(feats))] dup := false for _, g := range fs { dup = dup || g == f } if !dup { fs = append(fs, f) } } var d strings.Builder for d.Len() < between(r, 150, 900) { d.WriteString(words[r.IntN(len(words))]) d.WriteByte(' ') } d.WriteString("\n\nTestanzeige: erfunden, kein echtes Angebot.") created := now.Add(-time.Duration(r.Int64N(int64(10*24*time.Hour))) - time.Minute) period := "month" if kind == "sale" { period = "once" } return &listing.Listing{ Version: listing.Version, ID: id, Seq: 1, CreatedAt: created.Unix(), ExpiresAt: created.Add(time.Duration(between(r, 20, 30)) * 24 * time.Hour).Unix(), Kind: kind, Category: cat, Geohash: places.Encode(lat, lon, 6), Price: &listing.Price{AmountCents: int64(euros) * 100, Currency: "EUR", Period: period}, AreaM2: m2, RoomsX2: between(r, 2, 8), Features: fs, Title: fmt.Sprintf("%s %s in %s", moods[r.IntN(len(moods))], nouns[cat], places_[r.IntN(len(places_))]), Description: d.String(), }, nil }