go.dedis.ch/onet/v4@v4.0.0-pre1/simul/monitor/stats_test.go (about)

     1  package monitor
     2  
     3  import (
     4  	"bytes"
     5  	"strconv"
     6  	"strings"
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  
    11  	"go.dedis.ch/onet/v4/log"
    12  )
    13  
    14  func TestNewDataFilter(t *testing.T) {
    15  	rc := make(map[string]string)
    16  	rc["filter_round"] = "50"
    17  	rc["filter_verify"] = "90"
    18  	df := NewDataFilter(rc)
    19  	if df.percentiles["round"] == 0 || df.percentiles["verify"] == 0 {
    20  		t.Error("Datafilter not correctly parsed the run config")
    21  	}
    22  	if df.percentiles["round"] != 50.0 || df.percentiles["verify"] != 90.0 {
    23  		t.Errorf("datafilter not correctly parsed the percentile: %f vs 50 or %f vs 90", df.percentiles["round"], df.percentiles["verifiy"])
    24  	}
    25  }
    26  
    27  func TestDataFilterFilter(t *testing.T) {
    28  	rc := make(map[string]string)
    29  	rc["filter_round"] = "75"
    30  	df := NewDataFilter(rc)
    31  
    32  	values := []float64{35, 20, 15, 40, 50}
    33  	filtered := df.Filter("round", values)
    34  	shouldBe := []float64{35, 20, 15, 40}
    35  	if len(shouldBe) != len(filtered) {
    36  		t.Errorf("Filter returned %d values instead of %d", len(filtered), len(shouldBe))
    37  	}
    38  	for i, v := range filtered {
    39  		if v != shouldBe[i] {
    40  			t.Errorf("Element %d = %f vs %f", i, filtered[i], shouldBe[i])
    41  		}
    42  	}
    43  }
    44  
    45  func TestStatsUpdate(t *testing.T) {
    46  	rc := make(map[string]string)
    47  	rc["servers"] = "2"
    48  	rc["hosts"] = "2"
    49  	stats := NewStats(rc)
    50  
    51  	m1 := newSingleMeasure("round_wall", 10)
    52  	m2 := newSingleMeasure("round_wall", 30)
    53  	stats.Update(m1)
    54  	stats.Update(m2)
    55  	stats.Collect()
    56  	val := stats.values["round_wall"]
    57  	if val.Avg() != 20 {
    58  		t.Error("Aggregate or Update not working")
    59  	}
    60  }
    61  func TestStatsOrder(t *testing.T) {
    62  	m := make(map[string]string)
    63  	m["servers"] = "1"
    64  	m["hosts"] = "1"
    65  	m["bf"] = "2"
    66  	// create stats
    67  	stat := NewStats(m)
    68  	m1 := newSingleMeasure("round", 10)
    69  	m2 := newSingleMeasure("setup", 5)
    70  	stat.Update(m1)
    71  	stat.Update(m2)
    72  	str := new(bytes.Buffer)
    73  	stat.WriteHeader(str)
    74  	stat.WriteValues(str)
    75  
    76  	stat2 := NewStats(m)
    77  	stat2.Update(m2)
    78  	stat2.Update(m1)
    79  
    80  	str2 := new(bytes.Buffer)
    81  	stat2.WriteHeader(str2)
    82  	stat2.WriteValues(str2)
    83  	if !bytes.Equal(str.Bytes(), str2.Bytes()) {
    84  		t.Fatal("KeyOrder / output not the same for same stats")
    85  	}
    86  }
    87  
    88  func TestValues(t *testing.T) {
    89  	v1 := NewValue("test")
    90  	v1.Store(5.0)
    91  	v1.Store(10.0)
    92  	v1.Store(15.0)
    93  
    94  	v1.Collect()
    95  	if v1.Avg() != 10.0 || v1.Min() != 5.0 || v1.Max() != 15.0 || v1.Sum() != 30.0 || v1.Dev() != 5.0 {
    96  		t.Fatal("Wrong value calculation")
    97  	}
    98  }
    99  
   100  func TestStatsAverage(t *testing.T) {
   101  	m := make(map[string]string)
   102  	m["servers"] = "1"
   103  	m["hosts"] = "1"
   104  	m["bf"] = "2"
   105  	// create stats
   106  	stat1 := NewStats(m)
   107  	stat2 := NewStats(m)
   108  	m1 := newSingleMeasure("round", 10)
   109  	m2 := newSingleMeasure("setup", 5)
   110  	stat1.Update(m1)
   111  	stat2.Update(m2)
   112  
   113  	str := new(bytes.Buffer)
   114  	avgStat := AverageStats([]*Stats{stat1, stat2})
   115  	avgStat.WriteHeader(str)
   116  	avgStat.WriteValues(str)
   117  
   118  	stat3 := NewStats(m)
   119  	stat4 := NewStats(m)
   120  	stat3.Update(m1)
   121  	stat4.Update(m2)
   122  
   123  	str2 := new(bytes.Buffer)
   124  	avgStat2 := AverageStats([]*Stats{stat3, stat4})
   125  	avgStat2.WriteHeader(str2)
   126  	avgStat2.WriteValues(str2)
   127  
   128  	if !bytes.Equal(str.Bytes(), str2.Bytes()) {
   129  		t.Fatal("Average are not the same !")
   130  	}
   131  }
   132  
   133  func TestStatsAverageFiltered(t *testing.T) {
   134  	m := make(map[string]string)
   135  	m["servers"] = "1"
   136  	m["hosts"] = "1"
   137  	m["bf"] = "2"
   138  	// create the filter entry
   139  	m["filter_round"] = "50"
   140  	// create stats
   141  	stat1 := NewStats(m)
   142  	stat2 := NewStats(m)
   143  	m1 := newSingleMeasure("round", 10)
   144  	m2 := newSingleMeasure("round", 20)
   145  	m3 := newSingleMeasure("round", 150)
   146  	stat1.Update(m1)
   147  	stat1.Update(m2)
   148  	stat1.Update(m3)
   149  	stat2.Update(m1)
   150  	stat2.Update(m2)
   151  	stat2.Update(m3)
   152  
   153  	/* stat2.Collect()*/
   154  	//val := stat2.Value("round")
   155  	//if val.Avg() != (10+20)/2 {
   156  	//t.Fatal("Average with filter does not work?")
   157  	//}
   158  
   159  	str := new(bytes.Buffer)
   160  	avgStat := AverageStats([]*Stats{stat1, stat2})
   161  	avgStat.WriteHeader(str)
   162  	avgStat.WriteValues(str)
   163  
   164  	stat3 := NewStats(m)
   165  	stat4 := NewStats(m)
   166  	stat3.Update(m1)
   167  	stat3.Update(m2)
   168  	stat3.Update(m3)
   169  	stat4.Update(m1)
   170  	stat4.Update(m2)
   171  	stat4.Update(m3)
   172  
   173  	str2 := new(bytes.Buffer)
   174  	avgStat2 := AverageStats([]*Stats{stat3, stat4})
   175  	avgStat2.WriteHeader(str2)
   176  	avgStat2.WriteValues(str2)
   177  
   178  	if !bytes.Equal(str.Bytes(), str2.Bytes()) {
   179  		t.Fatal("Average are not the same !")
   180  	}
   181  
   182  }
   183  
   184  func TestStatsString(t *testing.T) {
   185  	rc := map[string]string{"servers": "10", "hosts": "10"}
   186  	rs := NewStats(rc)
   187  	m := NewMonitor(rs)
   188  
   189  	wg := sync.WaitGroup{}
   190  	wg.Add(1)
   191  	go func() {
   192  		if err := m.Listen(); err != nil {
   193  			log.Fatal("Could not Listen():", err)
   194  		}
   195  		wg.Done()
   196  	}()
   197  
   198  	defer func() {
   199  		EndAndCleanup()
   200  		wg.Wait()
   201  	}()
   202  
   203  	time.Sleep(100 * time.Millisecond)
   204  	log.ErrFatal(ConnectSink("localhost:" + strconv.Itoa(DefaultSinkPort)))
   205  	measure := NewTimeMeasure("test")
   206  	time.Sleep(time.Millisecond * 100)
   207  	measure.Record()
   208  	time.Sleep(time.Millisecond * 100)
   209  
   210  	if !strings.Contains(rs.String(), "0.1") {
   211  		t.Fatal("The measurement should contain 0.1:", rs.String())
   212  	}
   213  }