github.com/songzhibin97/gkit@v1.2.13/gctuner/tuner_test.go (about) 1 package gctuner 2 3 import ( 4 "runtime" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 var testHeap []byte 11 12 func TestTuner(t *testing.T) { 13 is := assert.New(t) 14 memLimit := uint64(100 * 1024 * 1024) // 100 MB 15 threshold := memLimit / 2 16 tn := newTuner(threshold) 17 currentGCPercent := tn.getGCPercent() 18 is.Equal(tn.threshold, threshold) 19 is.Equal(defaultGCPercent, currentGCPercent) 20 21 // wait for tuner set gcPercent to maxGCPercent 22 t.Logf("old gc percent before gc: %d", tn.getGCPercent()) 23 for tn.getGCPercent() != maxGCPercent { 24 runtime.GC() 25 t.Logf("new gc percent after gc: %d", tn.getGCPercent()) 26 } 27 28 // 1/4 threshold 29 testHeap = make([]byte, threshold/4) 30 // wait for tuner set gcPercent to ~= 300 31 t.Logf("old gc percent before gc: %d", tn.getGCPercent()) 32 for tn.getGCPercent() == maxGCPercent { 33 runtime.GC() 34 t.Logf("new gc percent after gc: %d", tn.getGCPercent()) 35 } 36 currentGCPercent = tn.getGCPercent() 37 is.GreaterOrEqual(currentGCPercent, uint32(250)) 38 is.LessOrEqual(currentGCPercent, uint32(300)) 39 40 // 1/2 threshold 41 testHeap = make([]byte, threshold/2) 42 // wait for tuner set gcPercent to ~= 100 43 t.Logf("old gc percent before gc: %d", tn.getGCPercent()) 44 for tn.getGCPercent() == currentGCPercent { 45 runtime.GC() 46 t.Logf("new gc percent after gc: %d", tn.getGCPercent()) 47 } 48 currentGCPercent = tn.getGCPercent() 49 is.GreaterOrEqual(currentGCPercent, uint32(50)) 50 is.LessOrEqual(currentGCPercent, uint32(100)) 51 52 // 3/4 threshold 53 testHeap = make([]byte, threshold/4*3) 54 // wait for tuner set gcPercent to minGCPercent 55 t.Logf("old gc percent before gc: %d", tn.getGCPercent()) 56 for tn.getGCPercent() != minGCPercent { 57 runtime.GC() 58 t.Logf("new gc percent after gc: %d", tn.getGCPercent()) 59 } 60 is.Equal(minGCPercent, tn.getGCPercent()) 61 62 // out of threshold 63 testHeap = make([]byte, threshold+1024) 64 t.Logf("old gc percent before gc: %d", tn.getGCPercent()) 65 runtime.GC() 66 for i := 0; i < 8; i++ { 67 runtime.GC() 68 is.Equal(minGCPercent, tn.getGCPercent()) 69 } 70 71 // no heap 72 testHeap = nil 73 // wait for tuner set gcPercent to maxGCPercent 74 t.Logf("old gc percent before gc: %d", tn.getGCPercent()) 75 for tn.getGCPercent() != maxGCPercent { 76 runtime.GC() 77 t.Logf("new gc percent after gc: %d", tn.getGCPercent()) 78 } 79 } 80 81 func TestCalcGCPercent(t *testing.T) { 82 is := assert.New(t) 83 const gb = 1024 * 1024 * 1024 84 // use default value when invalid params 85 is.Equal(defaultGCPercent, calcGCPercent(0, 0)) 86 is.Equal(defaultGCPercent, calcGCPercent(0, 1)) 87 is.Equal(defaultGCPercent, calcGCPercent(1, 0)) 88 89 is.Equal(maxGCPercent, calcGCPercent(1, 3*gb)) 90 is.Equal(maxGCPercent, calcGCPercent(gb/10, 4*gb)) 91 is.Equal(maxGCPercent, calcGCPercent(gb/2, 4*gb)) 92 is.Equal(uint32(300), calcGCPercent(1*gb, 4*gb)) 93 is.Equal(uint32(166), calcGCPercent(1.5*gb, 4*gb)) 94 is.Equal(uint32(100), calcGCPercent(2*gb, 4*gb)) 95 is.Equal(minGCPercent, calcGCPercent(3*gb, 4*gb)) 96 is.Equal(minGCPercent, calcGCPercent(4*gb, 4*gb)) 97 is.Equal(minGCPercent, calcGCPercent(5*gb, 4*gb)) 98 }