aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/metrics/description_test.go
blob: e966a281a1a61b67a3eeacaadce3d70d3ef81e59 (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
// 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 (
	"bufio"
	"os"
	"path/filepath"
	"regexp"
	"runtime"
	"runtime/metrics"
	"strings"
	"testing"
)

func TestDescriptionNameFormat(t *testing.T) {
	r := regexp.MustCompile("^(?P<name>/[^:]+):(?P<unit>[^:*/]+(?:[*/][^:*/]+)*)$")
	descriptions := metrics.All()
	for _, desc := range descriptions {
		if !r.MatchString(desc.Name) {
			t.Errorf("metrics %q does not match regexp %s", desc.Name, r)
		}
	}
}

func extractMetricDocs(t *testing.T) map[string]string {
	if runtime.GOOS == "android" {
		t.Skip("no access to Go source on android")
	}

	// Get doc.go.
	_, filename, _, _ := runtime.Caller(0)
	filename = filepath.Join(filepath.Dir(filename), "doc.go")

	f, err := os.Open(filename)
	if err != nil {
		t.Fatal(err)
	}
	const (
		stateSearch          = iota // look for list of metrics
		stateNextMetric             // look for next metric
		stateNextDescription        // build description
	)
	state := stateSearch
	s := bufio.NewScanner(f)
	result := make(map[string]string)
	var metric string
	var prevMetric string
	var desc strings.Builder
	for s.Scan() {
		line := strings.TrimSpace(s.Text())
		switch state {
		case stateSearch:
			if line == "Supported metrics" {
				state = stateNextMetric
			}
		case stateNextMetric:
			// Ignore empty lines until we find a non-empty
			// one. This will be our metric name.
			if len(line) != 0 {
				prevMetric = metric
				metric = line
				if prevMetric > metric {
					t.Errorf("metrics %s and %s are out of lexicographical order", prevMetric, metric)
				}
				state = stateNextDescription
			}
		case stateNextDescription:
			if len(line) == 0 || line == `*/` {
				// An empty line means we're done.
				// Write down the description and look
				// for a new metric.
				result[metric] = desc.String()
				desc.Reset()
				state = stateNextMetric
			} else {
				// As long as we're seeing data, assume that's
				// part of the description and append it.
				if desc.Len() != 0 {
					// Turn previous newlines into spaces.
					desc.WriteString(" ")
				}
				desc.WriteString(line)
			}
		}
		if line == `*/` {
			break
		}
	}
	if state == stateSearch {
		t.Fatalf("failed to find supported metrics docs in %s", filename)
	}
	return result
}

func TestDescriptionDocs(t *testing.T) {
	docs := extractMetricDocs(t)
	descriptions := metrics.All()
	for _, d := range descriptions {
		want := d.Description
		got, ok := docs[d.Name]
		if !ok {
			t.Errorf("no docs found for metric %s", d.Name)
			continue
		}
		if got != want {
			t.Errorf("mismatched description and docs for metric %s", d.Name)
			t.Errorf("want: %q, got %q", want, got)
			continue
		}
	}
	if len(docs) > len(descriptions) {
	docsLoop:
		for name, _ := range docs {
			for _, d := range descriptions {
				if name == d.Name {
					continue docsLoop
				}
			}
			t.Errorf("stale documentation for non-existent metric: %s", name)
		}
	}
}