go.dedis.ch/onet/v4@v4.0.0-pre1/simul/monitor/measure_test.go (about) 1 package monitor 2 3 import ( 4 "bytes" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestSingleMeasure(t *testing.T) { 13 mon, stats := setupMonitor(t) 14 mon.InsertBucket(0, []string{"0:1"}, NewStats(nil)) 15 16 RecordSingleMeasure("a", 1) 17 RecordSingleMeasureWithHost("a", 5, 0) 18 19 time.Sleep(100 * time.Millisecond) 20 21 stats.Collect() 22 require.NotNil(t, stats.Value("a")) 23 require.Equal(t, 3.0, stats.Value("a").Avg()) 24 25 b := mon.buckets.Get(0) 26 require.NotNil(t, b.Value(("a"))) 27 require.Equal(t, 5.0, b.Value("a").Avg()) 28 29 EndAndCleanup() 30 time.Sleep(100 * time.Millisecond) 31 } 32 33 func TestTimeMeasure(t *testing.T) { 34 mon, stats := setupMonitor(t) 35 mon.InsertBucket(0, []string{"0:1"}, NewStats(nil)) 36 37 NewTimeMeasure("a").Record() 38 tm := NewTimeMeasureWithHost("a", 0) 39 40 time.Sleep(50 * time.Millisecond) 41 tm.Record() 42 43 time.Sleep(100 * time.Millisecond) 44 45 stats.Collect() 46 v := stats.Value("a_wall") 47 require.NotNil(t, v) 48 // the value is in seconds 49 require.True(t, v.Min() > 0 && v.Min() < 0.05, fmt.Sprintf("%v < 0.05", v.Min())) 50 51 b := mon.buckets.Get(0) 52 require.NotNil(t, b.Value("a_wall")) 53 // the value is in seconds 54 require.True(t, b.Value("a_wall").Min() > 0.05, fmt.Sprintf("%v > 0.05", v.Min())) 55 56 EndAndCleanup() 57 time.Sleep(100 * time.Millisecond) 58 } 59 60 type DummyCounterIO struct { 61 rvalue uint64 62 wvalue uint64 63 msgrvalue uint64 64 msgwvalue uint64 65 } 66 67 func (dm *DummyCounterIO) Rx() uint64 { 68 dm.rvalue += 10 69 return dm.rvalue 70 } 71 72 func (dm *DummyCounterIO) Tx() uint64 { 73 dm.wvalue += 10 74 return dm.wvalue 75 } 76 77 func (dm *DummyCounterIO) MsgRx() uint64 { 78 dm.msgrvalue++ 79 return dm.msgrvalue 80 } 81 82 func (dm *DummyCounterIO) MsgTx() uint64 { 83 dm.msgwvalue++ 84 return dm.msgwvalue 85 } 86 87 func TestCounterIOMeasureRecord(t *testing.T) { 88 mon, _ := setupMonitor(t) 89 mon.InsertBucket(0, []string{"0:1"}, NewStats(nil)) 90 dm := &DummyCounterIO{0, 0, 0, 0} 91 // create the counter measure 92 cm := NewCounterIOMeasureWithHost("dummy", dm, 0) 93 if cm.baseRx != dm.rvalue || cm.baseTx != dm.wvalue { 94 t.Logf("baseRx = %d vs rvalue = %d || baseTx = %d vs wvalue = %d", cm.baseRx, dm.rvalue, cm.baseTx, dm.wvalue) 95 t.Fatal("Tx() / Rx() not working ?") 96 } 97 //bread, bwritten := cm.baseRx, cm.baseTx 98 cm.Record() 99 // check the values again 100 if cm.baseRx != dm.rvalue || cm.baseTx != dm.wvalue { 101 t.Fatal("Record() not working for CounterIOMeasure") 102 } 103 104 // Important otherwise data don't get written down to the monitor yet. 105 time.Sleep(100 * time.Millisecond) 106 str := new(bytes.Buffer) 107 stat := mon.stats 108 stat.Collect() 109 stat.WriteHeader(str) 110 stat.WriteValues(str) 111 wr, re := stat.Value("dummy_tx"), stat.Value("dummy_rx") 112 if wr == nil || wr.Avg() != 10 { 113 t.Logf("stats => %v", stat.values) 114 if wr != nil { 115 t.Logf("wr.Avg() = %f", wr.Avg()) 116 } 117 t.Fatal("Stats doesn't have the right value (write)") 118 } 119 if re == nil || re.Avg() != 10 { 120 t.Fatal("Stats doesn't have the right value (read)") 121 } 122 mwr, mre := stat.Value("dummy_msg_tx"), stat.Value("dummy_msg_rx") 123 if mwr == nil || mwr.Avg() != 1 { 124 t.Fatal("Stats doesn't have the right value (msg written)") 125 } 126 if mre == nil || mre.Avg() != 1 { 127 t.Fatal("Stats doesn't have the right value (msg read)") 128 } 129 130 // check the bucket is filled 131 b := mon.buckets.Get(0) 132 require.Equal(t, 10.0, b.Value("dummy_tx").Avg()) 133 134 EndAndCleanup() 135 time.Sleep(100 * time.Millisecond) 136 } 137 138 // Test that reset sets the values to the base ones 139 func TestCounterIOMeasureReset(t *testing.T) { 140 dm := &DummyCounterIO{0, 0, 0, 0} 141 cm := NewCounterIOMeasureWithHost("dummy", dm, 0) 142 143 // increase the actual 144 dm.Tx() 145 dm.Rx() 146 dm.MsgRx() 147 dm.MsgTx() 148 149 // several resets should still get the base values 150 cm.Reset() 151 cm.Reset() 152 153 if cm.baseRx != dm.rvalue || cm.baseTx != dm.wvalue { 154 t.Logf("baseRx = %d vs rvalue = %d || baseTx = %d vs wvalue = %d", 155 cm.baseRx, dm.rvalue, cm.baseTx, dm.wvalue) 156 t.Fatal("Tx() / Rx() not working ?") 157 } 158 159 if cm.baseMsgRx != dm.msgrvalue || cm.baseMsgTx != dm.msgwvalue { 160 t.Logf("baseMsgRx = %d vs msgrvalue = %d || baseMsgTx = %d vs msgwvalue = %d", 161 cm.baseMsgRx, dm.msgrvalue, cm.baseMsgTx, dm.msgwvalue) 162 t.Fatal("MsgTx() / MsgRx() not working ?") 163 } 164 }