| 1 | package main |
| 2 | |
| 3 | // How long each decision took, said in the integrator's own output. |
| 4 | // |
| 5 | // Every decision the integrator reports also gets one timing line: which |
| 6 | // request, what was decided, how long the whole decision took, and how |
| 7 | // that time divides among the stages it went through. The line is JSON |
| 8 | // after a fixed label, so it can be read out of the journal without |
| 9 | // parsing the rest. It is output and nothing else: no ref, record or event |
| 10 | // carries it, and nothing the integrator decides reads it. |
| 11 | |
| 12 | import ( |
| 13 | "encoding/json" |
| 14 | "fmt" |
| 15 | "time" |
| 16 | ) |
| 17 | |
| 18 | // The stages a decision is timed in. A stage a decision did not reach is |
| 19 | // absent from its line. |
| 20 | const ( |
| 21 | stageRefusal = "refusal" // the record and merge rules, before judging and again before landing |
| 22 | stageCheckout = "checkout" // the worktree of the target's tip |
| 23 | stageEvidence = "evidence" // the head's tree digest and verifying its notes |
| 24 | stagePolicy = "policy" // composing the policy and deriving the required checks |
| 25 | stageApply = "apply" // merging the delta onto the tip |
| 26 | stageClosures = "closures" // recomputing input closures on a moved tree |
| 27 | stageSign = "sign" // composing and signing the transfer statement |
| 28 | stageLand = "land" // the ref write that moves the target |
| 29 | stageRecord = "record" // storing the evidence and consuming the request |
| 30 | stagePublish = "publish" // the bus events |
| 31 | ) |
| 32 | |
| 33 | // stopwatch times one decision, as a whole and by stage. A nil stopwatch |
| 34 | // times nothing, so a caller with no decision to time can pass none. |
| 35 | type stopwatch struct { |
| 36 | began time.Time |
| 37 | stages map[string]time.Duration |
| 38 | } |
| 39 | |
| 40 | func newStopwatch() *stopwatch { |
| 41 | return &stopwatch{began: time.Now(), stages: map[string]time.Duration{}} |
| 42 | } |
| 43 | |
| 44 | // stage starts timing a stage and returns the function that stops it. A |
| 45 | // stage timed more than once adds up. |
| 46 | func (s *stopwatch) stage(name string) func() { |
| 47 | if s == nil { |
| 48 | return func() {} |
| 49 | } |
| 50 | began := time.Now() |
| 51 | return func() { s.stages[name] += time.Since(began) } |
| 52 | } |
| 53 | |
| 54 | // decisionTiming is the timing line's JSON. |
| 55 | type decisionTiming struct { |
| 56 | Time string `json:"time"` // when the decision began, UTC |
| 57 | Project string `json:"project"` |
| 58 | Request string `json:"request"` |
| 59 | Change string `json:"change"` |
| 60 | Head string `json:"head"` |
| 61 | Target string `json:"target"` |
| 62 | Tip string `json:"tip"` |
| 63 | Outcome string `json:"outcome"` |
| 64 | DurationMS int64 `json:"duration_ms"` |
| 65 | StagesMS map[string]int64 `json:"stages_ms"` |
| 66 | } |
| 67 | |
| 68 | // reportTiming prints the timing line for one decision about a request. |
| 69 | func (in *integrator) reportTiming(r request, tip, outcome string, s *stopwatch) { |
| 70 | t := decisionTiming{ |
| 71 | Time: s.began.UTC().Format("2006-01-02T15:04:05.000Z"), |
| 72 | Project: in.project, |
| 73 | Request: r.ref, |
| 74 | Change: r.change, |
| 75 | Head: r.head, |
| 76 | Target: r.target, |
| 77 | Tip: tip, |
| 78 | Outcome: outcome, |
| 79 | DurationMS: time.Since(s.began).Milliseconds(), |
| 80 | StagesMS: map[string]int64{}, |
| 81 | } |
| 82 | for name, d := range s.stages { |
| 83 | t.StagesMS[name] = d.Milliseconds() |
| 84 | } |
| 85 | line, err := json.Marshal(t) |
| 86 | if err != nil { |
| 87 | return |
| 88 | } |
| 89 | fmt.Fprintf(in.out, " timing %s\n", line) |
| 90 | } |