package sfive import ( "fmt" "os" "os/user" "testing" "time" ) var ( __sqlitePath = "file:memdb1?mode=memory&cache=shared" __boltPath = "/run/s5hub_testing_db.bolt" ) func TestMain(m *testing.M) { u, err := user.Current() if err != nil { os.Exit(-1) } __boltPath = fmt.Sprintf("/run/user/%s/s5hub_testing_db.bolt", u.Uid) os.Exit(m.Run()) } func TestAppend(t *testing.T) { os.Remove(__boltPath) store, err := NewStore(__sqlitePath, __boltPath) if err != nil { t.Errorf("Failed to initialize: %v", err) return } defer store.Close() startTime := time.Date(2014, time.August, 24, 14, 35, 33, 847282000, time.UTC) update := DataUpdate{Data: SourceData{BytesSent: 1, ClientCount: 3, BytesReceived: 1}, StartTime: startTime, Duration: 5000} streamId := StreamId{ContentId: "content", Format: "7bitascii", Quality: QualityHigh} source := SourceId{Hostname: "localhost", Tags: []string{"tag1", "master"}, StreamId: streamId, Version: 1} dat := StatisticsData{nil, nil, source, update} err = store.Append(dat) if err != nil { t.Errorf("Failed to append: %v", err) return } // stats, err := store.GetStats(nil) // if err != nil { // t.Errorf("Failed to get stats: %v", err) // } else { // clientCount := int(stats.ClientCount) // updateCount := stats.UpdateCount // if 3 != clientCount { // t.Errorf("Failed fo append, invalid number of clients, 3 != %v", clientCount) // } // if 1 != updateCount { // t.Errorf("Failed to append, invalid number of updates, 1 != %v", updateCount) // } // } // queryStartTime := time.Date(2015, time.December, 24, 1, 1, 1, 0, time.UTC) // filterStruct := StatsFilter{start: &queryStartTime} // stats, err = store.GetStats(&filterStruct) // if err != nil { // t.Errorf("Failed to get stats: %v", err) // } else { // updateCount := stats.UpdateCount // if 0 != updateCount { // t.Errorf("Failed to filter entries by start time, 0 != %v", updateCount) // } // } } func TestCount(t *testing.T) { os.Remove(__boltPath) store, err := NewStore(__sqlitePath, __boltPath) if err != nil { t.Errorf("Failed to initialize: %v", err) } defer store.Close() stats, err := store.GetStats(nil) clientCount := int(stats.ClientCount) if 0 != clientCount { t.Errorf("Failed to count correctly.") } } func TestGetUpdatesAfter(t *testing.T) { os.Remove(__boltPath) store, err := NewStore(__sqlitePath, __boltPath) if err != nil { t.Errorf("Failed to initialize: %v", err) return } defer store.Close() startTime := time.Date(2014, time.August, 24, 14, 35, 33, 847282000, time.UTC) update := DataUpdate{Data: SourceData{BytesSent: 1, ClientCount: 3, BytesReceived: 1}, StartTime: startTime, Duration: 5000} streamId := StreamId{ContentId: "content", Format: "7bitascii", Quality: QualityHigh} source := SourceId{Hostname: "localhost", Tags: []string{"tag1", "master"}, StreamId: streamId, Version: 1} dat := StatisticsData{nil, nil, source, update} err = store.Append(dat) if err != nil { t.Errorf("Failed to append: %v", err) return } res, err := store.GetUpdatesAfter(2) t.Logf("got updates (err %v):\n%#v", err, res) } func generateStatisticsData(n int) (data []StatisticsData) { hostnames := []string{"streamer1", "streamer2"} contents := []string{"av", "audio"} formats := []string{"webm", "flash", "hls"} qualities := []string{"high", "medium", "low"} numcombis := len(hostnames) * len(contents) * len(formats) * len(qualities) clients := []ClientData{ ClientData{"127.0.0.1", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0", 6400}, ClientData{"10.12.0.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400}, ClientData{"127.0.0.1", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0", 6400}, ClientData{"192.168.0.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400}, ClientData{"172.16.0.2", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/57.0.2987.98 Chrome/57.0.2987.98 Safari/537.36", 6400}} starttime := time.Now() duration := int64(15000) tags := []string{"2017", "elevate", "discourse"} for i := 0; i < n; i += numcombis { for _, hostname := range hostnames { for _, content := range contents { for _, format := range formats { for _, quality := range qualities { d := StatisticsData{} d.SourceId.Version = 1 d.SourceId.Hostname = hostname d.SourceId.Tags = tags d.SourceId.StreamId.ContentId = content d.SourceId.StreamId.Format = format d.SourceId.StreamId.Quality = quality d.DataUpdate.StartTime = starttime d.DataUpdate.Duration = duration d.DataUpdate.Data.ClientCount = uint(len(clients)) d.DataUpdate.Data.BytesSent = 6400 * uint(len(clients)) for _, client := range clients { c := ClientData{client.Ip, client.UserAgent, client.BytesSent} d.DataUpdate.Data.Clients = append(d.DataUpdate.Data.Clients, c) } data = append(data, d) } } } } starttime = starttime.Add(time.Duration(duration) * time.Millisecond) } return } func BenchmarkAppendMany(b *testing.B) { os.Remove(__boltPath) store, err := NewStore(__sqlitePath, __boltPath) if err != nil { b.Errorf("Failed to initialize: %v", err) } defer store.Close() data := generateStatisticsData(b.N) b.ResetTimer() if err := store.AppendMany(data); err != nil { b.Errorf("Failed to append: %v", err) } } func BenchmarkGetUpdatesAfter(b *testing.B) { os.Remove(__boltPath) store, err := NewStore(__sqlitePath, __boltPath) if err != nil { b.Errorf("Failed to initialize: %v", err) } defer store.Close() data := generateStatisticsData(b.N) if err := store.AppendMany(data); err != nil { b.Errorf("Failed to append: %v", err) } b.ResetTimer() latestId := -1 for { updates, err := store.GetUpdatesAfter(latestId) if err != nil { b.Errorf("Failed to retrieve: %v", err) } if len(updates) == 0 { break } latestId = findMaxId(updates) } }