...

Source file src/github.com/prometheus/procfs/bcache/get_test.go

Documentation: github.com/prometheus/procfs/bcache

     1  // Copyright 2017 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  package bcache
    15  
    16  import (
    17  	"math"
    18  	"testing"
    19  )
    20  
    21  func TestFSBcacheStats(t *testing.T) {
    22  	bcache, err := NewFS("testdata/fixtures/sys")
    23  	if err != nil {
    24  		t.Fatalf("failed to access bcache fs: %v", err)
    25  	}
    26  	stats, err := bcache.Stats()
    27  	if err != nil {
    28  		t.Fatalf("failed to parse bcache stats: %v", err)
    29  	}
    30  
    31  	tests := []struct {
    32  		name   string
    33  		bdevs  int
    34  		caches int
    35  	}{
    36  		{
    37  			name:   "deaddd54-c735-46d5-868e-f331c5fd7c74",
    38  			bdevs:  1,
    39  			caches: 1,
    40  		},
    41  	}
    42  
    43  	const expect = 1
    44  
    45  	if l := len(stats); l != expect {
    46  		t.Fatalf("unexpected number of bcache stats: %d", l)
    47  	}
    48  	if l := len(tests); l != expect {
    49  		t.Fatalf("unexpected number of tests: %d", l)
    50  	}
    51  
    52  	for i, tt := range tests {
    53  		if want, got := tt.name, stats[i].Name; want != got {
    54  			t.Errorf("unexpected stats name:\nwant: %q\nhave: %q", want, got)
    55  		}
    56  
    57  		if want, got := tt.bdevs, len(stats[i].Bdevs); want != got {
    58  			t.Errorf("unexpected value allocated:\nwant: %d\nhave: %d", want, got)
    59  		}
    60  
    61  		if want, got := tt.caches, len(stats[i].Caches); want != got {
    62  			t.Errorf("unexpected value allocated:\nwant: %d\nhave: %d", want, got)
    63  		}
    64  	}
    65  }
    66  
    67  func TestDehumanizeTests(t *testing.T) {
    68  	dehumanizeTests := []struct {
    69  		in      []byte
    70  		out     uint64
    71  		invalid bool
    72  	}{
    73  		{
    74  			in:  []byte("542k"),
    75  			out: 555008,
    76  		},
    77  		{
    78  			in:  []byte("322M"),
    79  			out: 337641472,
    80  		},
    81  		{
    82  			in:  []byte("1.1k"),
    83  			out: 1124,
    84  		},
    85  		{
    86  			in:  []byte("1.9k"),
    87  			out: 1924,
    88  		},
    89  		{
    90  			in:  []byte("1.10k"),
    91  			out: 2024,
    92  		},
    93  		{
    94  			in:      []byte(""),
    95  			out:     0,
    96  			invalid: true,
    97  		},
    98  	}
    99  	for _, tst := range dehumanizeTests {
   100  		got, err := dehumanize(tst.in)
   101  		if tst.invalid && err == nil {
   102  			t.Error("expected an error, but none occurred")
   103  		}
   104  		if !tst.invalid && err != nil {
   105  			t.Errorf("unexpected error: %v", err)
   106  		}
   107  		if got != tst.out {
   108  			t.Errorf("dehumanize: '%s', want %d, got %d", tst.in, tst.out, got)
   109  		}
   110  	}
   111  }
   112  
   113  func TestParsePseudoFloatTests(t *testing.T) {
   114  	parsePseudoFloatTests := []struct {
   115  		in  string
   116  		out float64
   117  	}{
   118  		{
   119  			in:  "1.1",
   120  			out: float64(1.097656),
   121  		},
   122  		{
   123  			in:  "1.9",
   124  			out: float64(1.878906),
   125  		},
   126  		{
   127  			in:  "1.10",
   128  			out: float64(1.976562),
   129  		},
   130  	}
   131  	for _, tst := range parsePseudoFloatTests {
   132  		got, err := parsePseudoFloat(tst.in)
   133  		if err != nil || math.Abs(got-tst.out) > 0.0001 {
   134  			t.Errorf("parsePseudoFloat: %s, want %f, got %f", tst.in, tst.out, got)
   135  		}
   136  	}
   137  }
   138  
   139  func TestPriorityStats(t *testing.T) {
   140  	var want = PriorityStats{
   141  		UnusedPercent:   99,
   142  		MetadataPercent: 5,
   143  	}
   144  	var (
   145  		in     string
   146  		gotErr error
   147  		got    PriorityStats
   148  	)
   149  	in = "Metadata:       5%"
   150  	gotErr = parsePriorityStats(in, &got)
   151  	if gotErr != nil || got.MetadataPercent != want.MetadataPercent {
   152  		t.Errorf("parsePriorityStats: '%s', want %d, got %d", in, want.MetadataPercent, got.MetadataPercent)
   153  	}
   154  
   155  	in = "Unused:         99%"
   156  	gotErr = parsePriorityStats(in, &got)
   157  	if gotErr != nil || got.UnusedPercent != want.UnusedPercent {
   158  		t.Errorf("parsePriorityStats: '%s', want %d, got %d", in, want.UnusedPercent, got.UnusedPercent)
   159  	}
   160  }
   161  
   162  func TestWritebackRateDebug(t *testing.T) {
   163  	var want = WritebackRateDebugStats{
   164  		Rate:         1765376,
   165  		Dirty:        21789409280,
   166  		Target:       21894266880,
   167  		Proportional: -1124,
   168  		Integral:     -257624,
   169  		Change:       2648,
   170  		NextIO:       -150773,
   171  	}
   172  	var (
   173  		in     string
   174  		gotErr error
   175  		got    WritebackRateDebugStats
   176  	)
   177  	in = "rate:           1.7M/sec"
   178  	gotErr = parseWritebackRateDebug(in, &got)
   179  	if gotErr != nil || got.Rate != want.Rate {
   180  		t.Errorf("parsePriorityStats: '%s', want %d, got %d", in, want.Rate, got.Rate)
   181  	}
   182  
   183  	in = "dirty:           20.3G"
   184  	gotErr = parseWritebackRateDebug(in, &got)
   185  	if gotErr != nil || got.Dirty != want.Dirty {
   186  		t.Errorf("parsePriorityStats: '%s', want %d, got %d", in, want.Dirty, got.Dirty)
   187  	}
   188  
   189  	in = "target:           20.4G"
   190  	gotErr = parseWritebackRateDebug(in, &got)
   191  	if gotErr != nil || got.Target != want.Target {
   192  		t.Errorf("parsePriorityStats: '%s', want %d, got %d", in, want.Target, got.Target)
   193  	}
   194  
   195  	in = "proportional:           -1.1k"
   196  	gotErr = parseWritebackRateDebug(in, &got)
   197  	if gotErr != nil || got.Proportional != want.Proportional {
   198  		t.Errorf("parsePriorityStats: '%s', want %d, got %d", in, want.Proportional, got.Proportional)
   199  	}
   200  
   201  	in = "integral:           -251.6k"
   202  	gotErr = parseWritebackRateDebug(in, &got)
   203  	if gotErr != nil || got.Integral != want.Integral {
   204  		t.Errorf("parsePriorityStats: '%s', want %d, got %d", in, want.Integral, got.Integral)
   205  	}
   206  
   207  	in = "change:           2.6k/sec"
   208  	gotErr = parseWritebackRateDebug(in, &got)
   209  	if gotErr != nil || got.Change != want.Change {
   210  		t.Errorf("parsePriorityStats: '%s', want %d, got %d", in, want.Change, got.Change)
   211  	}
   212  
   213  	in = "next io:           -150773ms"
   214  	gotErr = parseWritebackRateDebug(in, &got)
   215  	if gotErr != nil || got.NextIO != want.NextIO {
   216  		t.Errorf("parsePriorityStats: '%s', want %d, got %d", in, want.NextIO, got.NextIO)
   217  	}
   218  }
   219  

View as plain text