Git designed for AI agents.
graft-bench: latency of each RPC (WP91)
Federico Elia02a6cabd68ad
diff --git a/cmd/graft-bench/bench.go b/cmd/graft-bench/bench.goindex 885a9fb..617bd53 100644--- a/cmd/graft-bench/bench.go+++ b/cmd/graft-bench/bench.go@@ -238,6 +238,31 @@ type Sample struct {// Agent is the agent that made the change. Multi-node runs group the// samples by the node that serves the agent.Agent int+ // CommitLatency is the time of the Commit call, one UserCommitFiles, and+ // Committed reports that it succeeded. LandLatency is the time of the Land+ // call, one UserLand, without the wait of queue mode; it is zero when the+ // change did not get to land.+ CommitLatency time.Duration+ Committed bool+ LandLatency time.Duration+}++// The RPCs whose latency a run reports (Result.RPCs).+const (+ rpcCommitFiles = "commit_files"+ rpcLand = "land"+)++// RPCLatency is the latency of one RPC of a run, as the client measures it+// around each call.+type RPCLatency struct {+ Calls int `json:"calls"`+ Failed int `json:"failed"`+ // P50MS, P95MS and P99MS are the latency percentiles of the calls that+ // succeeded.+ P50MS float64 `json:"p50_ms"`+ P95MS float64 `json:"p95_ms"`+ P99MS float64 `json:"p99_ms"`}// Result is the aggregated outcome of a run. Its JSON form is the output of@@ -303,6 +328,10 @@ type Result struct {P99MS float64 `json:"p99_ms"`// FailedP95MS is the p95 latency of the failed changes, zero if none failed.FailedP95MS float64 `json:"failed_p95_ms"`+ // RPCs holds the latency of each RPC of the run by name: commit_files (the+ // UserCommitFiles of every change) and land (the UserLand of every change+ // that was committed).+ RPCs map[string]RPCLatency `json:"rpcs,omitempty"`// FailureClasses counts the failed changes by the kind of their failure// message, with the object IDs removed (see errorKind).@@ -407,8 +436,9 @@ func runChange(ctx context.Context, cfg Config, be Backend, c Change, base strinbegan := time.Now()commit, err := be.Commit(ctx, c, base)+ commitLatency := time.Since(began)if err != nil {- return Sample{Latency: time.Since(began), Err: fmt.Errorf("commit: %w", err), Agent: c.Agent}+ return Sample{Latency: commitLatency, CommitLatency: commitLatency, Err: fmt.Errorf("commit: %w", err), Agent: c.Agent}}if cfg.Mode == ModeQueue {@@ -416,7 +446,9 @@ func runChange(ctx context.Context, cfg Config, be Backend, c Change, base strindefer queueMu.Unlock()}+ landStart := time.Now()outcome, err := be.Land(ctx, cfg, c, commit)+ landLatency := time.Since(landStart)if cfg.Mode == ModeQueue && cfg.TestDuration > 0 && err == nil {// The simulated test runs while the queue is still held.timer := time.NewTimer(cfg.TestDuration)@@ -427,7 +459,14 @@ func runChange(ctx context.Context, cfg Config, be Backend, c Change, base strin}}- s := Sample{Latency: time.Since(began), Outcome: outcome, Agent: c.Agent}+ s := Sample{+ Latency: time.Since(began),+ Outcome: outcome,+ Agent: c.Agent,+ CommitLatency: commitLatency,+ Committed: true,+ LandLatency: landLatency,+ }switch {case err != nil:s.Err = fmt.Errorf("land: %w", err)@@ -454,8 +493,13 @@ func aggregate(cfg Config, samples []Sample, elapsed time.Duration) Result {var landedMS, failedMS []float64var batchSum, batchCount intseen := map[string]bool{}+ rpcs := map[string]*rpcSamples{rpcCommitFiles: {}, rpcLand: {}}for _, s := range samples {ms := float64(s.Latency) / float64(time.Millisecond)+ rpcs[rpcCommitFiles].add(s.CommitLatency, s.Committed)+ if s.Committed {+ rpcs[rpcLand].add(s.LandLatency, s.Landed)+ }res.Attempts += int64(s.Outcome.Attempts)res.Resolutions += s.Outcome.Resolvedif s.Outcome.Attempts > res.MaxAttempts {@@ -519,9 +563,45 @@ func aggregate(cfg Config, samples []Sample, elapsed time.Duration) Result {res.P99MS = percentile(landedMS, 99)res.FailedP95MS = percentile(failedMS, 95)+ for name, r := range rpcs {+ if r.calls == 0 {+ continue+ }+ if res.RPCs == nil {+ res.RPCs = map[string]RPCLatency{}+ }+ res.RPCs[name] = RPCLatency{+ Calls: r.calls,+ Failed: r.failed,+ P50MS: percentile(r.succeededMS, 50),+ P95MS: percentile(r.succeededMS, 95),+ P99MS: percentile(r.succeededMS, 99),+ }+ }+return res}+// rpcSamples collects the calls of one RPC of a run.+type rpcSamples struct {+ calls, failed int+ succeededMS []float64+}++func (r *rpcSamples) add(latency time.Duration, succeeded bool) {+ r.calls+++ if !succeeded {+ r.failed+++ return+ }+ r.succeededMS = append(r.succeededMS, float64(latency)/float64(time.Millisecond))+}++// formatRPCLatency formats the latency of an RPC of a run for a report.+func formatRPCLatency(r RPCLatency) string {+ return fmt.Sprintf("p50 %.1fms, p95 %.1fms, p99 %.1fms (%d calls, %d failed)", r.P50MS, r.P95MS, r.P99MS, r.Calls, r.Failed)+}+// objectIDPattern matches a SHA-1 or SHA-256 object ID as a whole word.var objectIDPattern = regexp.MustCompile(`\b[0-9a-f]{40}(?:[0-9a-f]{24})?\b`)diff --git a/cmd/graft-bench/bench_v2_test.go b/cmd/graft-bench/bench_v2_test.goindex 24a716f..af5d053 100644--- a/cmd/graft-bench/bench_v2_test.go+++ b/cmd/graft-bench/bench_v2_test.go@@ -376,3 +376,64 @@ func TestRunCompareCommand(t *testing.T) {require.Equal(t, 1, run(context.Background(), []string{"compare", filepath.Join(dir, "missing.json")}, &out, &errOut))require.Contains(t, errOut.String(), "missing.json")}++func TestAggregateRPCLatency(t *testing.T) {+ t.Parallel()++ samples := []Sample{+ {Landed: true, Committed: true, CommitLatency: 10 * time.Millisecond, LandLatency: 100 * time.Millisecond},+ {Landed: true, Committed: true, CommitLatency: 20 * time.Millisecond, LandLatency: 200 * time.Millisecond},+ {Committed: true, CommitLatency: 30 * time.Millisecond, LandLatency: 900 * time.Millisecond, Err: errors.New("merge conflict"), Conflict: true},+ {CommitLatency: 500 * time.Millisecond, Err: errors.New("commit: unavailable")},+ }++ res := aggregate(baseConfig(), samples, time.Second)++ commit := res.RPCs[rpcCommitFiles]+ require.Equal(t, 4, commit.Calls)+ require.Equal(t, 1, commit.Failed)+ require.InDelta(t, 20, commit.P50MS, 1e-9, "failed calls are not in the percentiles")+ require.InDelta(t, 30, commit.P99MS, 1e-9)++ land := res.RPCs[rpcLand]+ require.Equal(t, 3, land.Calls, "a change whose commit failed does not land")+ require.Equal(t, 1, land.Failed)+ require.InDelta(t, 100, land.P50MS, 1e-9)+ require.InDelta(t, 200, land.P99MS, 1e-9)+}++// slowBackend takes a fixed time for each call.+type slowBackend struct {+ commit, land time.Duration+}++func (b slowBackend) Prepare(context.Context, Config) (string, error) { return "base", nil }++func (b slowBackend) Commit(context.Context, Change, string) (string, error) {+ time.Sleep(b.commit)+ return "commit", nil+}++func (b slowBackend) Land(context.Context, Config, Change, string) (LandOutcome, error) {+ time.Sleep(b.land)+ return LandOutcome{Attempts: 1}, nil+}++// TestRunMeasuresEachRPC times the commit and the land of a change separately: the latency of the+// change covers both, each RPC only its own call.+func TestRunMeasuresEachRPC(t *testing.T) {+ t.Parallel()++ cfg := baseConfig()+ cfg.Agents, cfg.ChangesPerAgent = 2, 2+ res, err := Run(context.Background(), cfg, slowBackend{commit: 20 * time.Millisecond, land: 60 * time.Millisecond})+ require.NoError(t, err)++ commit, land := res.RPCs[rpcCommitFiles], res.RPCs[rpcLand]+ require.Equal(t, 4, commit.Calls)+ require.Equal(t, 4, land.Calls)+ require.GreaterOrEqual(t, commit.P50MS, 20.0)+ require.Less(t, commit.P50MS, 60.0, "the commit does not include the land")+ require.GreaterOrEqual(t, land.P50MS, 60.0)+ require.GreaterOrEqual(t, res.P50MS, 80.0, "a change takes its commit and its land")+}diff --git a/cmd/graft-bench/compare.go b/cmd/graft-bench/compare.goindex 53b32d0..798ae95 100644--- a/cmd/graft-bench/compare.go+++ b/cmd/graft-bench/compare.go@@ -43,6 +43,10 @@ var comparisonMetrics = []comparisonMetric{{"latency p95 (landed)", func(r Result) string { return fmt.Sprintf("%.1fms", r.P95MS) }},{"latency p99 (landed)", func(r Result) string { return fmt.Sprintf("%.1fms", r.P99MS) }},{"latency p95 (failed)", func(r Result) string { return fmt.Sprintf("%.1fms", r.FailedP95MS) }},+ {"UserCommitFiles p50", func(r Result) string { return fmt.Sprintf("%.1fms", r.RPCs[rpcCommitFiles].P50MS) }},+ {"UserCommitFiles p99", func(r Result) string { return fmt.Sprintf("%.1fms", r.RPCs[rpcCommitFiles].P99MS) }},+ {"UserLand p50", func(r Result) string { return fmt.Sprintf("%.1fms", r.RPCs[rpcLand].P50MS) }},+ {"UserLand p99", func(r Result) string { return fmt.Sprintf("%.1fms", r.RPCs[rpcLand].P99MS) }},}// writeComparison prints the runs side by side, one column per run, labelleddiff --git a/cmd/graft-bench/main.go b/cmd/graft-bench/main.goindex 68f8210..995e9b0 100644--- a/cmd/graft-bench/main.go+++ b/cmd/graft-bench/main.go@@ -267,6 +267,8 @@ func writeResult(w io.Writer, format string, res Result) error {{"latency p95", fmt.Sprintf("%.1fms (landed changes)", res.P95MS)},{"latency p99", fmt.Sprintf("%.1fms (landed changes)", res.P99MS)},{"failed p95", fmt.Sprintf("%.1fms (failed changes)", res.FailedP95MS)},+ {"UserCommitFiles", formatRPCLatency(res.RPCs[rpcCommitFiles])},+ {"UserLand", formatRPCLatency(res.RPCs[rpcLand])},}for _, row := range rows {if row[0] == "label" && row[1] == "" {diff --git a/doc/graft/BENCH.md b/doc/graft/BENCH.mdindex 3a86c31..5c26785 100644--- a/doc/graft/BENCH.md+++ b/doc/graft/BENCH.md@@ -53,6 +53,7 @@ Markdown report show the same values.| `lands_per_minute` | `landed` divided by the wall-clock duration of the run. || `p50_ms`, `p95_ms`, `p99_ms` | Latency of the LANDED changes, from the start of the commit to the end of the land (nearest rank). Failed changes are not included. || `failed_p95_ms` | p95 latency of the failed changes, measured the same way. 0 when none failed. |+| `rpcs` | The latency of each RPC as the client measures it around the call: `commit_files` (the `UserCommitFiles` of every change) and `land` (the `UserLand` of every change that was committed, without the wait of queue mode). Each has `calls`, `failed`, and `p50_ms`, `p95_ms`, `p99_ms` of the calls that succeeded (nearest rank). A land that fails on a conflict is a failed call. || `failure_classes` | Failed changes counted by the kind of their error message, with commit IDs removed, so the same conflict on different commits is one kind. || `errors` | Up to five distinct kinds of failure, each as the first full message of its kind. |