summaryrefslogtreecommitdiffstats
path: root/src/runtime/metrics/description_test.go
blob: 4fc652362ecc0cf2860daf3bf9230b36612aba97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package metrics_test

import (
	"bytes"
	"flag"
	"fmt"
	"go/ast"
	"go/doc"
	"go/doc/comment"
	"go/format"
	"go/parser"
	"go/token"
	"internal/diff"
	"os"
	"regexp"
	"runtime/metrics"
	"sort"
	"strings"
	"testing"
	_ "unsafe"
)

// Implemented in the runtime.
//
//go:linkname runtime_readMetricNames
func runtime_readMetricNames() []string

func TestNames(t *testing.T) {
	// Note that this regexp is promised in the package docs for Description. Do not change.
	r := regexp.MustCompile("^(?P<name>/[^:]+):(?P<unit>[^:*/]+(?:[*/][^:*/]+)*)$")
	all := metrics.All()
	for i, d := range all {
		if !r.MatchString(d.Name) {
			t.Errorf("name %q does not match regexp %#q", d.Name, r)
		}
		if i > 0 && all[i-1].Name >= all[i].Name {
			t.Fatalf("allDesc not sorted: %s ≥ %s", all[i-1].Name, all[i].Name)
		}
	}

	names := runtime_readMetricNames()
	sort.Strings(names)
	samples := make([]metrics.Sample, len(names))
	for i, name := range names {
		samples[i].Name = name
	}
	metrics.Read(samples)

	for _, d := range all {
		for len(samples) > 0 && samples[0].Name < d.Name {
			t.Errorf("%s: reported by runtime but not listed in All", samples[0].Name)
			samples = samples[1:]
		}
		if len(samples) == 0 || d.Name < samples[0].Name {
			t.Errorf("%s: listed in All but not reported by runtime", d.Name)
			continue
		}
		if samples[0].Value.Kind() != d.Kind {
			t.Errorf("%s: runtime reports %v but All reports %v", d.Name, samples[0].Value.Kind(), d.Kind)
		}
		samples = samples[1:]
	}
}

func wrap(prefix, text string, width int) string {
	doc := &comment.Doc{Content: []comment.Block{&comment.Paragraph{Text: []comment.Text{comment.Plain(text)}}}}
	pr := &comment.Printer{TextPrefix: prefix, TextWidth: width}
	return string(pr.Text(doc))
}

func formatDesc(t *testing.T) string {
	var b strings.Builder
	for i, d := range metrics.All() {
		if i > 0 {
			fmt.Fprintf(&b, "\n")
		}
		fmt.Fprintf(&b, "%s\n", d.Name)
		fmt.Fprintf(&b, "%s", wrap("\t", d.Description, 80-2*8))
	}
	return b.String()
}

var generate = flag.Bool("generate", false, "update doc.go for go generate")

func TestDocs(t *testing.T) {
	want := formatDesc(t)

	src, err := os.ReadFile("doc.go")
	if err != nil {
		t.Fatal(err)
	}
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "doc.go", src, parser.ParseComments)
	if err != nil {
		t.Fatal(err)
	}
	fdoc := f.Doc
	if fdoc == nil {
		t.Fatal("no doc comment in doc.go")
	}
	pkg, err := doc.NewFromFiles(fset, []*ast.File{f}, "runtime/metrics")
	if err != nil {
		t.Fatal(err)
	}
	if pkg.Doc == "" {
		t.Fatal("doc.NewFromFiles lost doc comment")
	}
	doc := new(comment.Parser).Parse(pkg.Doc)
	expectCode := false
	foundCode := false
	updated := false
	for _, block := range doc.Content {
		switch b := block.(type) {
		case *comment.Heading:
			expectCode = false
			if b.Text[0] == comment.Plain("Supported metrics") {
				expectCode = true
			}
		case *comment.Code:
			if expectCode {
				foundCode = true
				if b.Text != want {
					if !*generate {
						t.Fatalf("doc comment out of date; use go generate to rebuild\n%s", diff.Diff("old", []byte(b.Text), "want", []byte(want)))
					}
					b.Text = want
					updated = true
				}
			}
		}
	}

	if !foundCode {
		t.Fatalf("did not find Supported metrics list in doc.go")
	}
	if updated {
		fmt.Fprintf(os.Stderr, "go test -generate: writing new doc.go\n")
		var buf bytes.Buffer
		buf.Write(src[:fdoc.Pos()-f.FileStart])
		buf.WriteString("/*\n")
		buf.Write(new(comment.Printer).Comment(doc))
		buf.WriteString("*/")
		buf.Write(src[fdoc.End()-f.FileStart:])
		src, err := format.Source(buf.Bytes())
		if err != nil {
			t.Fatal(err)
		}
		if err := os.WriteFile("doc.go", src, 0666); err != nil {
			t.Fatal(err)
		}
	} else if *generate {
		fmt.Fprintf(os.Stderr, "go test -generate: doc.go already up-to-date\n")
	}
}