allmende-sim: record runs as JSON (samples, phases, arrival times); sweep script and graph data; scaling results

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
2026-11-18 22:23:33 +01:00
co-authored by Claude Sonnet 5.5
parent 5db90c2720
commit 130ba5dc65
6 changed files with 321 additions and 3 deletions
+37 -3
View File
@@ -22,6 +22,7 @@ import (
"runtime"
"sort"
"strings"
"sync"
"syscall"
"time"
@@ -76,7 +77,8 @@ type sim struct {
extra []libp2p.Option // transport conditions (latency, connection loss)
timeout time.Duration
made []*listing.Listing
seedConns int // raised connection limit of the seeds; 0 keeps libp2p's default
mu sync.Mutex // guards nodes against the sampler
seedConns int // raised connection limit of the seeds; 0 keeps libp2p's default
}
func main() {
@@ -94,6 +96,7 @@ func main() {
latency := flag.Duration("latency", 0, "delay added to every write on connections a node dials (one direction), with up to 50% jitter")
lifetime := flag.Duration("lifetime", 0, "mean lifetime of a dialed connection before it is cut (0: never); redials and resyncs follow")
seedConns := flag.Int("seed-conns", 0, "raise the seeds' inbound connection limit to this many (0: libp2p's default, which scales with RAM)")
jsonOut := flag.String("json", "", "write the run (samples every second, phases, arrival times) to this JSON file, for graphs")
hostile := flag.Bool("hostile", false, "also run attackers: invalid listings, a message flood")
killSeeds := flag.Bool("kill-seeds", false, "also shut the seeds down and see what the network and newcomers do")
repair := flag.Bool("repair", false, "also test anti-entropy: a listing only one node has, nobody told (set -anti-entropy to a few seconds)")
@@ -115,7 +118,11 @@ func main() {
s.extra = conditions.options()
}
regions := allRegions[:*nregions]
report0 := newReport()
rec := newRecorder()
report0 := newReport(rec)
stopSampling := make(chan struct{})
go rec.run(s, stopSampling)
report0.phase("start")
// --- start the network ---------------------------------------------------
fmt.Printf("starting %d nodes (%d seeds), %d regions ...\n", *nodes, *seeds, *nregions)
@@ -131,7 +138,9 @@ func main() {
}
}
s.must(s.makeIdentity(n))
s.mu.Lock()
s.nodes = append(s.nodes, n)
s.mu.Unlock()
s.must(s.start(n))
if n.seed {
s.seedAddrs = append(s.seedAddrs, n.node.Addrs()[0])
@@ -140,11 +149,14 @@ func main() {
startup := time.Since(t0)
report0.row("start", fmt.Sprintf("%d nodes started in %s", *nodes, round(startup)))
report0.phase("mesh")
formed := s.waitFormed(regions, *timeout)
meshSeconds := time.Since(t0).Seconds()
report0.row("mesh", fmt.Sprintf("every node knows regional peers after %s (%s)", round(time.Since(t0)), formed))
report0.resources("after start", s)
// --- listings ------------------------------------------------------------
report0.phase("listings")
fmt.Printf("publishing %d listings at %.0f/s ...\n", *listings, *rate)
var made []*listing.Listing
var pairs []*pair
@@ -170,6 +182,7 @@ func main() {
report0.resources("after listings", s)
// --- messages ------------------------------------------------------------
report0.phase("messages")
fmt.Printf("sending %d buyer messages ...\n", *messages)
pairs = nil
refused := 0
@@ -197,6 +210,7 @@ func main() {
report0.latency("message", "from a random node of the region to the listing's owner", res)
// --- reports -------------------------------------------------------------
report0.phase("reports")
fmt.Printf("publishing %d abuse reports ...\n", *reports)
pairs = nil
var reps []*report.Report
@@ -226,6 +240,7 @@ func main() {
// --- churn ---------------------------------------------------------------
down := s.pickDown(int(float64(*nodes) * *churn))
if len(down) > 0 {
report0.phase("churn")
fmt.Printf("taking %d nodes offline, publishing 10 more listings, bringing them back ...\n", len(down))
for _, n := range down {
_ = n.node.Close() // the store (and so what it already knew) stays, as on a real machine
@@ -276,6 +291,17 @@ func main() {
report0.row("agreement", s.agreement(regions))
report0.resources("at the end", s)
report0.print(os.Stdout)
close(stopSampling)
if *jsonOut != "" {
meta := map[string]any{
"nodes": *nodes, "regions": *nregions, "seeds": *seeds, "seed_conns": *seedConns,
"latency_ms": latency.Milliseconds(), "lifetime_s": lifetime.Seconds(),
"mesh_s": meshSeconds, "mesh_complete": formed == "complete", "mesh": formed,
"agreement": s.agreement(regions), "date": time.Now().Format(time.RFC3339),
}
must(rec.write(*jsonOut, meta))
fmt.Println("recorded to", *jsonOut)
}
for _, n := range s.nodes {
if n.node != nil {
@@ -568,18 +594,26 @@ func await(pairs []*pair, timeout time.Duration) []*pair {
}
type reportTable struct {
rec *recorder
rows [][2]string
res []string
}
func newReport() *reportTable { return &reportTable{} }
func newReport(rec *recorder) *reportTable { return &reportTable{rec: rec} }
// phase marks where a part of the run begins, for the graphs.
func (r *reportTable) phase(name string) { r.rec.phase(name) }
func (r *reportTable) row(k, v string) {
r.rows = append(r.rows, [2]string{k, v})
r.rec.mu.Lock()
r.rec.rows[k] = v
r.rec.mu.Unlock()
fmt.Printf(" %-10s %s\n", k, v) // live, so a crash later does not lose it
}
func (r *reportTable) latency(k, what string, ps []*pair) {
r.rec.addSeries(k, what, ps)
var d []time.Duration
for _, p := range ps {
if p.done {