github.com/xmidt-org/webpa-common@v1.11.9/health/stat_test.go (about)

     1  package health
     2  
     3  import (
     4  	"github.com/c9s/goprocinfo/linux"
     5  	"reflect"
     6  	"runtime"
     7  	"testing"
     8  )
     9  
    10  func TestClone(t *testing.T) {
    11  	initial := Stats{
    12  		CurrentMemoryUtilizationHeapSys: 123,
    13  	}
    14  
    15  	cloned := initial.Clone()
    16  	if !reflect.DeepEqual(initial, cloned) {
    17  		t.Errorf("Expected %v, got %v", initial, cloned)
    18  	}
    19  
    20  	cloned[CurrentMemoryUtilizationActive] = 123211
    21  	if reflect.DeepEqual(initial, cloned) {
    22  		t.Error("Clone should be a distinct instance")
    23  	}
    24  }
    25  
    26  func TestApply(t *testing.T) {
    27  	var testData = []struct {
    28  		options  []Option
    29  		initial  Stats
    30  		expected Stats
    31  	}{
    32  		{
    33  			options: []Option{Inc(CurrentMemoryUtilizationAlloc, 1)},
    34  			initial: Stats{},
    35  			expected: Stats{
    36  				CurrentMemoryUtilizationAlloc: 1,
    37  			},
    38  		},
    39  		{
    40  			options: []Option{
    41  				CurrentMemoryUtilizationAlloc,
    42  				MaxMemoryUtilizationActive,
    43  			},
    44  			initial: Stats{},
    45  			expected: Stats{
    46  				CurrentMemoryUtilizationAlloc: 0,
    47  				MaxMemoryUtilizationActive:    0,
    48  			},
    49  		},
    50  		{
    51  			options: []Option{
    52  				CurrentMemoryUtilizationAlloc,
    53  				MaxMemoryUtilizationActive,
    54  			},
    55  			initial: Stats{
    56  				CurrentMemoryUtilizationAlloc: 12301,
    57  			},
    58  			expected: Stats{
    59  				CurrentMemoryUtilizationAlloc: 12301,
    60  				MaxMemoryUtilizationActive:    0,
    61  			},
    62  		},
    63  		{
    64  			options: []Option{
    65  				Stats{
    66  					CurrentMemoryUtilizationAlloc: 123,
    67  					MaxMemoryUtilizationActive:    -982374,
    68  				},
    69  			},
    70  			initial: Stats{},
    71  			expected: Stats{
    72  				CurrentMemoryUtilizationAlloc: 123,
    73  				MaxMemoryUtilizationActive:    -982374,
    74  			},
    75  		},
    76  		{
    77  			options: []Option{
    78  				Stats{
    79  					CurrentMemoryUtilizationAlloc: 123,
    80  					MaxMemoryUtilizationActive:    -982374,
    81  				},
    82  			},
    83  			initial: Stats{
    84  				MaxMemoryUtilizationAlloc: 56,
    85  			},
    86  			expected: Stats{
    87  				MaxMemoryUtilizationAlloc:     56,
    88  				CurrentMemoryUtilizationAlloc: 123,
    89  				MaxMemoryUtilizationActive:    -982374,
    90  			},
    91  		},
    92  	}
    93  
    94  	for _, record := range testData {
    95  		actual := record.initial.Clone()
    96  		actual.Apply(record.options)
    97  		if !reflect.DeepEqual(record.expected, actual) {
    98  			t.Errorf("Expected %v, got %v", record.expected, actual)
    99  		}
   100  	}
   101  }
   102  
   103  func TestInc(t *testing.T) {
   104  	var testData = []struct {
   105  		stat      Stat
   106  		increment int
   107  		initial   Stats
   108  		expected  Stats
   109  	}{
   110  		{
   111  			CurrentMemoryUtilizationHeapSys,
   112  			1,
   113  			Stats{},
   114  			Stats{CurrentMemoryUtilizationHeapSys: 1},
   115  		},
   116  		{
   117  			CurrentMemoryUtilizationHeapSys,
   118  			-12,
   119  			Stats{},
   120  			Stats{CurrentMemoryUtilizationHeapSys: -12},
   121  		},
   122  		{
   123  			CurrentMemoryUtilizationHeapSys,
   124  			72,
   125  			Stats{CurrentMemoryUtilizationHeapSys: 0},
   126  			Stats{CurrentMemoryUtilizationHeapSys: 72},
   127  		},
   128  		{
   129  			CurrentMemoryUtilizationHeapSys,
   130  			6,
   131  			Stats{CurrentMemoryUtilizationHeapSys: 45},
   132  			Stats{CurrentMemoryUtilizationHeapSys: 51},
   133  		},
   134  	}
   135  
   136  	for _, record := range testData {
   137  		Inc(record.stat, record.increment)(record.initial)
   138  
   139  		if !reflect.DeepEqual(record.expected, record.initial) {
   140  			t.Errorf("Expected %v, but got %v", record.expected, record.initial)
   141  		}
   142  	}
   143  }
   144  
   145  func TestSet(t *testing.T) {
   146  	var testData = []struct {
   147  		stat     Stat
   148  		newValue int
   149  		initial  Stats
   150  		expected Stats
   151  	}{
   152  		{
   153  			CurrentMemoryUtilizationHeapSys,
   154  			123,
   155  			Stats{},
   156  			Stats{CurrentMemoryUtilizationHeapSys: 123},
   157  		},
   158  		{
   159  			CurrentMemoryUtilizationHeapSys,
   160  			37842,
   161  			Stats{CurrentMemoryUtilizationHeapSys: 42734987},
   162  			Stats{CurrentMemoryUtilizationHeapSys: 37842},
   163  		},
   164  	}
   165  
   166  	for _, record := range testData {
   167  		Set(record.stat, record.newValue)(record.initial)
   168  
   169  		if !reflect.DeepEqual(record.expected, record.initial) {
   170  			t.Errorf("Expected %v, but got %v", record.expected, record.initial)
   171  		}
   172  	}
   173  }
   174  
   175  func TestEnsure(t *testing.T) {
   176  	var testData = []struct {
   177  		stat     Stat
   178  		initial  Stats
   179  		expected Stats
   180  	}{
   181  		{
   182  			CurrentMemoryUtilizationHeapSys,
   183  			Stats{},
   184  			Stats{CurrentMemoryUtilizationHeapSys: 0},
   185  		},
   186  		{
   187  			CurrentMemoryUtilizationHeapSys,
   188  			Stats{CurrentMemoryUtilizationHeapSys: -157},
   189  			Stats{CurrentMemoryUtilizationHeapSys: -157},
   190  		},
   191  	}
   192  
   193  	for _, record := range testData {
   194  		Ensure(record.stat)(record.initial)
   195  
   196  		if !reflect.DeepEqual(record.expected, record.initial) {
   197  			t.Errorf("Expected %v, but got %v", record.expected, record.initial)
   198  		}
   199  	}
   200  }
   201  
   202  func TestUpdateMemInfo(t *testing.T) {
   203  	var testData = []struct {
   204  		memInfo  linux.MemInfo
   205  		initial  Stats
   206  		expected Stats
   207  	}{
   208  		// empty initial Stats
   209  		{
   210  			linux.MemInfo{
   211  				Active: 3457,
   212  			},
   213  			Stats{},
   214  			Stats{
   215  				CurrentMemoryUtilizationActive: int(3457 * 1024),
   216  				MaxMemoryUtilizationActive:     int(3457 * 1024),
   217  			},
   218  		},
   219  		// max is less than current
   220  		{
   221  			linux.MemInfo{
   222  				Active: 13,
   223  			},
   224  			Stats{
   225  				MaxMemoryUtilizationActive: 1,
   226  			},
   227  			Stats{
   228  				CurrentMemoryUtilizationActive: int(13 * 1024),
   229  				MaxMemoryUtilizationActive:     int(13 * 1024),
   230  			},
   231  		},
   232  		// max is larger than current
   233  		{
   234  			linux.MemInfo{
   235  				Active: 271,
   236  			},
   237  			Stats{
   238  				MaxMemoryUtilizationActive: int(34872 * 1024),
   239  			},
   240  			Stats{
   241  				CurrentMemoryUtilizationActive: int(271 * 1024),
   242  				MaxMemoryUtilizationActive:     int(34872 * 1024),
   243  			},
   244  		},
   245  	}
   246  
   247  	for _, record := range testData {
   248  		actual := record.initial.Clone()
   249  		actual.UpdateMemInfo(&record.memInfo)
   250  		if !reflect.DeepEqual(record.expected, actual) {
   251  			t.Errorf("Expected %v, but got %v", record.expected, actual)
   252  		}
   253  	}
   254  }
   255  
   256  func TestUpdateMemStats(t *testing.T) {
   257  	var testData = []struct {
   258  		memStats runtime.MemStats
   259  		initial  Stats
   260  		expected Stats
   261  	}{
   262  		// empty initial Stats
   263  		{
   264  			runtime.MemStats{
   265  				Alloc:   247,
   266  				HeapSys: 2381,
   267  			},
   268  			Stats{},
   269  			Stats{
   270  				CurrentMemoryUtilizationAlloc:   247,
   271  				MaxMemoryUtilizationAlloc:       247,
   272  				CurrentMemoryUtilizationHeapSys: 2381,
   273  				MaxMemoryUtilizationHeapSys:     2381,
   274  			},
   275  		},
   276  		// current is less than max
   277  		{
   278  			runtime.MemStats{
   279  				Alloc:   3874,
   280  				HeapSys: 1234,
   281  			},
   282  			Stats{
   283  				CurrentMemoryUtilizationAlloc:   12354,
   284  				MaxMemoryUtilizationAlloc:       927412,
   285  				CurrentMemoryUtilizationHeapSys: 7897,
   286  				MaxMemoryUtilizationHeapSys:     827123,
   287  			},
   288  			Stats{
   289  				CurrentMemoryUtilizationAlloc:   3874,
   290  				MaxMemoryUtilizationAlloc:       927412,
   291  				CurrentMemoryUtilizationHeapSys: 1234,
   292  				MaxMemoryUtilizationHeapSys:     827123,
   293  			},
   294  		},
   295  		// current is greater than max
   296  		{
   297  			runtime.MemStats{
   298  				Alloc:   8742,
   299  				HeapSys: 2903209,
   300  			},
   301  			Stats{
   302  				CurrentMemoryUtilizationAlloc:   135,
   303  				MaxMemoryUtilizationAlloc:       1254,
   304  				CurrentMemoryUtilizationHeapSys: 5412,
   305  				MaxMemoryUtilizationHeapSys:     12345,
   306  			},
   307  			Stats{
   308  				CurrentMemoryUtilizationAlloc:   8742,
   309  				MaxMemoryUtilizationAlloc:       8742,
   310  				CurrentMemoryUtilizationHeapSys: 2903209,
   311  				MaxMemoryUtilizationHeapSys:     2903209,
   312  			},
   313  		},
   314  	}
   315  
   316  	for _, record := range testData {
   317  		actual := record.initial.Clone()
   318  		actual.UpdateMemStats(&record.memStats)
   319  		if !reflect.DeepEqual(record.expected, actual) {
   320  			t.Errorf("Expected %v, but got %v", record.expected, actual)
   321  		}
   322  	}
   323  }
   324  
   325  func TestUpdateMemory(t *testing.T) {
   326  	memInfoReader := &MemInfoReader{"meminfo.test"}
   327  	stats := make(Stats)
   328  	stats.UpdateMemory(memInfoReader)
   329  
   330  	// each key in commonStats should be present in the output
   331  	for _, key := range memoryStats {
   332  		if _, ok := stats[key.(Stat)]; !ok {
   333  			t.Errorf("Key %s not present in ServeHTTP results", key)
   334  		}
   335  	}
   336  }