github.com/cilium/cilium@v1.16.2/pkg/datapath/linux/probes/kernel_hz_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package probes
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestKernelHZ(t *testing.T) {
    12  	if _, err := KernelHZ(); err != nil {
    13  		t.Fatal(err)
    14  	}
    15  }
    16  
    17  func TestJiffies(t *testing.T) {
    18  	j, err := Jiffies()
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	if j == 0 {
    23  		t.Fatal("unexpected zero jiffies reading")
    24  	}
    25  }
    26  
    27  func TestNearest(t *testing.T) {
    28  	var tests = []struct {
    29  		name   string
    30  		in     uint16
    31  		values []uint16
    32  		want   uint16
    33  	}{
    34  		{
    35  			name:   "single value",
    36  			in:     0,
    37  			values: []uint16{123},
    38  			want:   123,
    39  		},
    40  		{
    41  			name:   "equal distance to multiple values",
    42  			in:     5,
    43  			values: []uint16{0, 10},
    44  			want:   0,
    45  		},
    46  		{
    47  			name:   "in higher than last value",
    48  			in:     20,
    49  			values: []uint16{0, 10},
    50  			want:   10,
    51  		},
    52  		{
    53  			name:   "in lower than first value",
    54  			in:     0,
    55  			values: []uint16{10, 20},
    56  			want:   10,
    57  		},
    58  		{
    59  			name:   "in max value",
    60  			in:     ^uint16(0),
    61  			values: []uint16{10, 20},
    62  			want:   20,
    63  		},
    64  	}
    65  
    66  	for _, tt := range tests {
    67  		t.Run(tt.name, func(t *testing.T) {
    68  			n, err := nearest(tt.in, tt.values)
    69  			if err != nil {
    70  				t.Error(err)
    71  			}
    72  			if want, got := tt.want, n; want != got {
    73  				t.Errorf("expected %d, got %d", want, got)
    74  			}
    75  		})
    76  	}
    77  
    78  	if _, err := nearest(0, nil); err == nil {
    79  		t.Fatal("expected nearest with empty values to return error")
    80  	}
    81  }
    82  
    83  func mustParse(tb testing.TB, value string) time.Time {
    84  	t, err := time.Parse(time.StampMilli, value)
    85  	if err != nil {
    86  		tb.Fatal(err)
    87  	}
    88  	return t
    89  }
    90  
    91  func TestKtimeInterpolate(t *testing.T) {
    92  	var tests = []struct {
    93  		name string
    94  		k1   ktime
    95  		k2   ktime
    96  		hz   uint16
    97  	}{
    98  		{
    99  			name: "10 jiffies over 10 milliseconds = 1000 hz",
   100  			k1: ktime{
   101  				k: 0,
   102  				t: mustParse(t, "Jan 01 00:00:00.000"),
   103  			},
   104  			k2: ktime{
   105  				k: 10,
   106  				t: mustParse(t, "Jan 01 00:00:00.010"),
   107  			},
   108  			hz: 1000,
   109  		},
   110  		{
   111  			name: "100 jiffies over 123 milliseconds = 813 hz",
   112  			k1: ktime{
   113  				k: 100,
   114  				t: mustParse(t, "Jan 01 00:00:00.000"),
   115  			},
   116  			k2: ktime{
   117  				k: 200,
   118  				t: mustParse(t, "Jan 01 00:00:00.123"),
   119  			},
   120  			hz: 813,
   121  		},
   122  		{
   123  			name: "1 jiffy over 1 second = 1 hz",
   124  			k1: ktime{
   125  				k: 100,
   126  				t: mustParse(t, "Jan 01 00:00:00.000"),
   127  			},
   128  			k2: ktime{
   129  				k: 101,
   130  				t: mustParse(t, "Jan 01 00:00:01.000"),
   131  			},
   132  			hz: 1,
   133  		},
   134  		{
   135  			name: "0 jiffies over 1 second = 0 hz",
   136  			k1: ktime{
   137  				k: 100,
   138  				t: mustParse(t, "Jan 01 00:00:00.000"),
   139  			},
   140  			k2: ktime{
   141  				k: 100,
   142  				t: mustParse(t, "Jan 01 00:00:01.000"),
   143  			},
   144  			hz: 0,
   145  		},
   146  	}
   147  
   148  	for _, tt := range tests {
   149  		t.Run(tt.name, func(t *testing.T) {
   150  			got, err := tt.k1.interpolate(tt.k2)
   151  			if err != nil {
   152  				t.Error(err)
   153  			}
   154  
   155  			if want, got := tt.hz, got; want != got {
   156  				t.Errorf("expected %d hz, got %d hz", want, got)
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  func TestKtimeInterpolateErrors(t *testing.T) {
   163  	t1 := ktime{t: mustParse(t, "Jan 01 00:00:01.000")}
   164  	t2 := ktime{t: mustParse(t, "Jan 01 00:00:00.000")}
   165  
   166  	if _, err := t1.interpolate(t2); err == nil {
   167  		t.Error("expected error interpolating ktimes with descending time.Time")
   168  	}
   169  
   170  	t1 = ktime{k: 1}
   171  	t2 = ktime{k: 0}
   172  
   173  	if _, err := t1.interpolate(t2); err == nil {
   174  		t.Error("expected error interpolating ktimes with descending jiffies")
   175  	}
   176  }