...

Source file src/github.com/prometheus/procfs/proc_smaps_test.go

Documentation: github.com/prometheus/procfs

     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  //go:build !windows
    15  // +build !windows
    16  
    17  package procfs
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func BenchmarkProcSMapsRollup(b *testing.B) {
    24  	fs, err := NewFS(procTestFixtures)
    25  	if err != nil {
    26  		b.Fatalf("Creating pseudo fs from getProcFixtures failed at fixtures/proc with error: %s", err)
    27  	}
    28  
    29  	p, err := fs.Proc(26231)
    30  	if err != nil {
    31  		b.Fatal(err)
    32  	}
    33  
    34  	b.ResetTimer()
    35  	for n := 0; n < b.N; n++ {
    36  		_, _ = p.ProcSMapsRollup()
    37  	}
    38  }
    39  
    40  func TestProcSmapsRollup(t *testing.T) {
    41  	p, err := getProcFixtures(t).Proc(26231)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	s1, err := p.ProcSMapsRollup()
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	s2, err := p.procSMapsRollupManual()
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	cases := []struct {
    57  		name  string
    58  		smaps ProcSMapsRollup
    59  	}{
    60  		{
    61  			name:  "ProcSMapsRollup",
    62  			smaps: s1,
    63  		},
    64  		{
    65  			name:  "procSMapsRollupManual",
    66  			smaps: s2,
    67  		},
    68  	}
    69  
    70  	for _, c := range cases {
    71  		for _, test := range []struct {
    72  			name string
    73  			want uint64
    74  			have uint64
    75  		}{
    76  			{name: "Rss", want: 29948 * 1024, have: c.smaps.Rss},
    77  			{name: "Pss", want: 29944 * 1024, have: c.smaps.Pss},
    78  			{name: "SharedClean", want: 4 * 1024, have: c.smaps.SharedClean},
    79  			{name: "SharedDirty", want: 0 * 1024, have: c.smaps.SharedDirty},
    80  			{name: "PrivateClean", want: 15548 * 1024, have: c.smaps.PrivateClean},
    81  			{name: "PrivateDirty", want: 14396 * 1024, have: c.smaps.PrivateDirty},
    82  			{name: "Referenced", want: 24752 * 1024, have: c.smaps.Referenced},
    83  			{name: "Anonymous", want: 20756 * 1024, have: c.smaps.Anonymous},
    84  			{name: "Swap", want: 1940 * 1024, have: c.smaps.Swap},
    85  			{name: "SwapPss", want: 1940 * 1024, have: c.smaps.SwapPss},
    86  		} {
    87  			if test.want != test.have {
    88  				t.Errorf("want %s %s %d, have %d", c.name, test.name, test.want, test.have)
    89  			}
    90  		}
    91  	}
    92  }
    93  

View as plain text