github.com/aergoio/aergo@v1.3.1/p2p/metric/exponentmetric_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package metric
     7  
     8  import (
     9  	"fmt"
    10  	"math"
    11  	"math/rand"
    12  	"testing"
    13  )
    14  
    15  func TestExponentMetric_Calculate(t *testing.T) {
    16  	lenIn := 200
    17  	constIn := []int64{779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779,779}
    18  	varIn := make([]int64,lenIn)
    19  	for i:=0 ; i<lenIn; i++ {
    20  		varIn[i] = rand.Int63() & 0x0fffffff
    21  	}
    22  
    23  	tests := []struct {
    24  		name string
    25  		interval int
    26  		meantime int
    27  		inBytes []int64
    28  
    29  		//expectLF []int64
    30  	}{
    31  		{"TConst", 15, 30, constIn },
    32  		{"TVarin", 5, 60, varIn},
    33  		// TODO: test cases
    34  	}
    35  	for _, test := range tests {
    36  		t.Run(test.name, func(t *testing.T) {
    37  			m := NewExponentMetric(test.interval, test.meantime)
    38  
    39  			//errorRatio := 1.0
    40  			var total int64 = 0
    41  			for i, in := range test.inBytes {
    42  				m.AddBytes(int(in))
    43  				total += in
    44  				realbps :=  total / int64(i+1)
    45  				m.Calculate()
    46  				diffRatio := math.Abs(float64(realbps - m.APS()) / float64(realbps))
    47  				fmt.Printf("%03d: in %10d, Total %11d, expectBps %9d, aps %9d, diffR %.4f loadScore %10d \n", i, in, total, realbps, m.APS(), diffRatio, m.LoadScore())
    48  				//assert.Equal(t, realbps, m.APS())
    49  				// assert.True(t, diffRatio <= errorRatio)
    50  				//errorRatio = diffRatio
    51  				//assert.Equal(t, test.expectLF[i], m.LoadScore())
    52  			}
    53  		})
    54  	}
    55  }