1// Copyright 2020 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// +build !windows
15
16package procfs
17
18import (
19	"testing"
20)
21
22func BenchmarkProcSMapsRollup(b *testing.B) {
23	fs, err := NewFS(procTestFixtures)
24	if err != nil {
25		b.Fatalf("Creating pseudo fs from getProcFixtures failed at fixtures/proc with error: %s", err)
26	}
27
28	p, err := fs.Proc(26231)
29	if err != nil {
30		b.Fatal(err)
31	}
32
33	b.ResetTimer()
34	for n := 0; n < b.N; n++ {
35		_, _ = p.ProcSMapsRollup()
36	}
37}
38
39func TestProcSmapsRollup(t *testing.T) {
40	p, err := getProcFixtures(t).Proc(26231)
41	if err != nil {
42		t.Fatal(err)
43	}
44
45	s1, err := p.ProcSMapsRollup()
46	if err != nil {
47		t.Fatal(err)
48	}
49
50	s2, err := p.procSMapsRollupManual()
51	if err != nil {
52		t.Fatal(err)
53	}
54
55	cases := []struct {
56		name  string
57		smaps ProcSMapsRollup
58	}{
59		{
60			name:  "ProcSMapsRollup",
61			smaps: s1,
62		},
63		{
64			name:  "procSMapsRollupManual",
65			smaps: s2,
66		},
67	}
68
69	for _, c := range cases {
70		for _, test := range []struct {
71			name string
72			want uint64
73			have uint64
74		}{
75			{name: "Rss", want: 29948 * 1024, have: c.smaps.Rss},
76			{name: "Pss", want: 29944 * 1024, have: c.smaps.Pss},
77			{name: "SharedClean", want: 4 * 1024, have: c.smaps.SharedClean},
78			{name: "SharedDirty", want: 0 * 1024, have: c.smaps.SharedDirty},
79			{name: "PrivateClean", want: 15548 * 1024, have: c.smaps.PrivateClean},
80			{name: "PrivateDirty", want: 14396 * 1024, have: c.smaps.PrivateDirty},
81			{name: "Referenced", want: 24752 * 1024, have: c.smaps.Referenced},
82			{name: "Anonymous", want: 20756 * 1024, have: c.smaps.Anonymous},
83			{name: "Swap", want: 1940 * 1024, have: c.smaps.Swap},
84			{name: "SwapPss", want: 1940 * 1024, have: c.smaps.SwapPss},
85		} {
86			if test.want != test.have {
87				t.Errorf("want %s %s %d, have %d", c.name, test.name, test.want, test.have)
88			}
89		}
90	}
91}
92