Skip to content

Commit

Permalink
livestream stats
Browse files Browse the repository at this point in the history
  • Loading branch information
tukeJonny committed Nov 24, 2023
1 parent 5f6c633 commit 72d6268
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 79 deletions.
28 changes: 14 additions & 14 deletions bench/internal/scheduler/stats_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,25 @@ type LivestreamStats struct {
LivestreamID int64

// 視聴者数 (初期では0)
totalViewers int64
TotalViewers int64

// トータルレポート数 (初期では0)
totalReports int64
TotalReports int64

// トータルリアクション数
totalReactions int64
TotalReactions int64

// 最大チップ金額 (ライブコメント)
totalTips int64
maxTip int64
TotalTips int64
MaxTip int64
}

func NewLivestreamStats(livestreamID int64) *LivestreamStats {
return &LivestreamStats{LivestreamID: livestreamID}
}

func (s *LivestreamStats) Score() int64 {
return s.totalReactions + s.totalTips
return s.TotalReactions + s.TotalTips
}

type StatsScheduler struct {
Expand Down Expand Up @@ -277,7 +277,7 @@ func (s *StatsScheduler) EnterLivestream(streamerName string, livestreamID int64
if !ok {
return fmt.Errorf("統計情報の更新に失敗(EnterLivestream.livestreamStats): user=%s, livestream=%d", streamerName, livestreamID)
}
livestreamStats.totalViewers++
livestreamStats.TotalViewers++

return nil
}
Expand All @@ -301,10 +301,10 @@ func (s *StatsScheduler) ExitLivestream(streamerName string, livestreamID int64)
if !ok {
return fmt.Errorf("統計情報の更新に失敗(ExitLivestream.livestreamStats): user=%s, livestream=%d", streamerName, livestreamID)
}
if livestreamStats.totalViewers <= 0 {
return fmt.Errorf("ExitLivestreamの呼び出し回数が不正です: streamer=%s viewers_count=%d", streamerName, livestreamStats.totalViewers)
if livestreamStats.TotalViewers <= 0 {
return fmt.Errorf("ExitLivestreamの呼び出し回数が不正です: streamer=%s viewers_count=%d", streamerName, livestreamStats.TotalViewers)
}
livestreamStats.totalViewers--
livestreamStats.TotalViewers--

return nil
}
Expand All @@ -324,7 +324,7 @@ func (s *StatsScheduler) addReactionForLivestream(streamerName string, livestrea
if !ok {
return fmt.Errorf("統計情報の更新に失敗(AddReaction.livestreamStats): user=%s, livestream=%d", streamerName, livestreamID)
}
livestreamStats.totalReactions++
livestreamStats.TotalReactions++

return nil
}
Expand All @@ -351,7 +351,7 @@ func (s *StatsScheduler) AddReport(streamerName string, livestreamID int64) erro
if !ok {
return fmt.Errorf("統計情報の更新に失敗(AddReaction.livestreamStats): user=%s, livestream=%d", streamerName, livestreamID)
}
livestreamStats.totalReports++
livestreamStats.TotalReports++

return nil
}
Expand All @@ -370,8 +370,8 @@ func (s *StatsScheduler) addLivecommentForLivestream(streamerName string, livest
if !ok {
return fmt.Errorf("統計情報の更新に失敗(AddLivecomment.livestreamStats): user=%s, livestream=%d", streamerName, livestreamID)
}
livestreamStats.totalTips += int64(tip.Tip)
livestreamStats.maxTip = max(livestreamStats.maxTip, int64(tip.Tip))
livestreamStats.TotalTips += int64(tip.Tip)
livestreamStats.MaxTip = max(livestreamStats.MaxTip, int64(tip.Tip))
return nil
}
func (s *StatsScheduler) AddLivecomment(streamerName string, livestreamID int64, tip *Tip) error {
Expand Down
18 changes: 9 additions & 9 deletions bench/internal/scheduler/stats_scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestEnterExitStats(t *testing.T) {
for j := 0; j < 5; j++ {
livestreamID := int64(j)
assert.Equal(t, int64(5), s.userStats[streamerName].TotalViewers)
assert.Equal(t, int64(5), s.livestreamStats[livestreamID].totalViewers)
assert.Equal(t, int64(5), s.livestreamStats[livestreamID].TotalViewers)
}
}
for i := 0; i < 5; i++ {
Expand All @@ -48,7 +48,7 @@ func TestEnterExitStats(t *testing.T) {
for j := 0; j < 5; j++ {
livestreamID := int64(j)
assert.Equal(t, int64(0), s.userStats[streamerName].TotalViewers)
assert.Equal(t, int64(0), s.livestreamStats[livestreamID].totalViewers)
assert.Equal(t, int64(0), s.livestreamStats[livestreamID].TotalViewers)
}
}
}
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestReactionStats(t *testing.T) {
for j := 0; j < 5; j++ {
livestreamID := int64(j)
assert.Equal(t, int64(5), s.userStats[streamerName].TotalReactions())
assert.Equal(t, int64(5), s.livestreamStats[livestreamID].totalReactions)
assert.Equal(t, int64(5), s.livestreamStats[livestreamID].TotalReactions)
favoriteEmoji, ok := s.userStats[streamerName].FavoriteEmoji()
assert.True(t, ok)
assert.Equal(t, "4", favoriteEmoji)
Expand All @@ -88,11 +88,11 @@ func TestReactionStats(t *testing.T) {
log.Printf("start = %s\n", startAt.String())
userRank, err := s.GetUserRank("streamer1")
assert.NoError(t, err)
assert.Equal(t, int64(2), userRank)
assert.Equal(t, int64(4), userRank)

livestreamRank, err := s.GetLivestreamRank(3)
assert.NoError(t, err)
assert.Equal(t, int64(4), livestreamRank)
assert.Equal(t, int64(2), livestreamRank)
endAt := time.Now()
log.Printf("end = %s\n", endAt.String())
log.Printf("elapsed = %s\n", time.Since(startAt).String())
Expand Down Expand Up @@ -120,19 +120,19 @@ func TestLivecommentStats(t *testing.T) {
for j := 0; j < 5; j++ {
livestreamID := int64(j)
assert.Equal(t, int64(5), s.userStats[streamerName].TotalLivecomments)
assert.Equal(t, int64(j*5), s.livestreamStats[livestreamID].totalTips)
assert.Equal(t, int64(j), s.livestreamStats[livestreamID].maxTip)
assert.Equal(t, int64(j*5), s.livestreamStats[livestreamID].TotalTips)
assert.Equal(t, int64(j), s.livestreamStats[livestreamID].MaxTip)
}
}

startAt := time.Now()
userRank, err := s.GetUserRank("streamer4")
assert.NoError(t, err)
assert.Equal(t, int64(5), userRank)
assert.Equal(t, int64(1), userRank)

livestreamRank, err := s.GetLivestreamRank(1)
assert.NoError(t, err)
assert.Equal(t, int64(2), livestreamRank)
assert.Equal(t, int64(4), livestreamRank)
endAt := time.Now()
log.Printf("end = %s\n", endAt.String())
log.Printf("elapsed = %s\n", time.Since(startAt).String())
Expand Down
126 changes: 70 additions & 56 deletions bench/scenario/core_pretest_calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ func normalUserStatsCalcPretest(ctx context.Context, contestantLogger *zap.Logge
return fmt.Errorf("ユーザ %s のお気に入り絵文字が不正です: expected=%s, actual=%s", username, favoriteEmoji, stats1.FavoriteEmoji)
}
}
if stats1.TotalReactions != wantStats1.TotalReactions() {
return fmt.Errorf("ユーザ %s の総リアクション数が不正です: expected=%d, actual=%d", username, stats1.TotalReactions, wantStats1.TotalReactions())
}
if stats1.TotalLivecomments != wantStats1.TotalLivecomments {
return fmt.Errorf("ユーザ %s の総ライブコメント数が不正です: expected=%d, actual=%d", username, stats1.TotalLivecomments, wantStats1.TotalLivecomments)
}

// // LivestreamStatsのイテレーション数 * 配信数(2)とかにして、LivestreamStatsのユーザより上に位置するようにする
// count := 5 + rand.Intn(10)
Expand Down Expand Up @@ -202,79 +208,87 @@ func normalLivestreamStatsCalcPretest(ctx context.Context, contestantLogger *zap
return err
}

livestreams, err := client.GetMyLivestreams(ctx)
randNumber := rand.Intn(100)
livestreamID := int64(scheduler.GetLivestreamLength() - randNumber)
livestream := scheduler.GetLivestreamByID(livestreamID)
user, err := scheduler.UserScheduler.GetInitialUserForPretest(livestream.OwnerID)
if err != nil {
return err
}
if len(livestreams) != 1 {
return fmt.Errorf("test user has just one livestream")
}

// FIXME: ライブコメント投稿のスパム処理にて、正しいNGワードと件数のエラー文が返ってくるように検証

livestream := livestreams[0]
stats1, err := client.GetLivestreamStatistics(ctx, livestreamID, user.Name)
if err != nil {
return err
}

// NOTE: rankは変動をみる
stats1, err := client.GetLivestreamStatistics(ctx, livestream.ID, livestream.Owner.Name)
wantStats1, err := scheduler.StatsSched.GetLivestreamStats(livestreamID)
if err != nil {
return err
}
if stats1.MaxTip != 0 ||
stats1.TotalReactions != 0 ||
stats1.TotalReports != 0 ||
stats1.ViewersCount != 0 {
return fmt.Errorf("initial livestream stats must be zero")
livestreamRank, err := scheduler.StatsSched.GetLivestreamRank(livestreamID)
if err != nil {
return err
}

count := 5 + rand.Intn(10)
for i := 0; i < count; i++ {
viewer, err := isupipe.NewCustomResolverClient(
contestantLogger,
dnsResolver,
agent.WithTimeout(config.PretestTimeout),
)
if err != nil {
return err
}
if stats1.Rank != livestreamRank {
return fmt.Errorf("配信 %d のランクが不正です: expected=%d, actual=%d", livestreamID, livestreamRank, stats1.Rank)
}
if stats1.MaxTip != wantStats1.MaxTip {
return fmt.Errorf("配信 %d の最大チップが不正です: expected=%d, actual=%d", livestreamID, livestreamRank, stats1.Rank)
}
if stats1.TotalReactions != wantStats1.TotalReactions {
return fmt.Errorf("配信 %d の総リアクション数が不正です: expected=%d, actual=%d", livestreamID, livestreamRank, stats1.Rank)
}

_, err = viewer.Register(ctx, &isupipe.RegisterRequest{
// FIXME: ユーザ
})
if err != nil {
return err
}
// count := 5 + rand.Intn(10)
// for i := 0; i < count; i++ {
// viewer, err := isupipe.NewCustomResolverClient(
// contestantLogger,
// dnsResolver,
// agent.WithTimeout(config.PretestTimeout),
// )
// if err != nil {
// return err
// }

if err := viewer.EnterLivestream(ctx, livestream.ID, livestream.Owner.Name); err != nil {
return err
}
// _, err = viewer.Register(ctx, &isupipe.RegisterRequest{
// // FIXME: ユーザ
// })
// if err != nil {
// return err
// }

_, err = viewer.PostReaction(ctx, livestream.ID, livestream.Owner.Name, &isupipe.PostReactionRequest{
EmojiName: "innocent",
})
if err != nil {
return err
}
// if err := viewer.EnterLivestream(ctx, livestream.ID, livestream.Owner.Name); err != nil {
// return err
// }

livecommentResp, _, err := viewer.PostLivecomment(ctx, livestream.ID, livestream.Owner.Name, "isuisu~", &scheduler.Tip{
Tip: i,
})
if err != nil {
return err
}
// _, err = viewer.PostReaction(ctx, livestream.ID, livestream.Owner.Name, &isupipe.PostReactionRequest{
// EmojiName: "innocent",
// })
// if err != nil {
// return err
// }

err = viewer.ReportLivecomment(ctx, livestream.ID, livestream.Owner.Name, livecommentResp.ID, isupipe.WithValidateReportLivecomment())
if err != nil {
return err
}
}
// livecommentResp, _, err := viewer.PostLivecomment(ctx, livestream.ID, livestream.Owner.Name, "isuisu~", &scheduler.Tip{
// Tip: i,
// })
// if err != nil {
// return err
// }

stats2, err := client.GetLivestreamStatistics(ctx, livestream.ID, livestream.Owner.Name)
if err != nil {
return err
}
// err = viewer.ReportLivecomment(ctx, livestream.ID, livestream.Owner.Name, livecommentResp.ID)
// if err != nil {
// return err
// }
// }

_ = stats1
_ = stats2
// stats2, err := client.GetLivestreamStatistics(ctx, livestream.ID, livestream.Owner.Name)
// if err != nil {
// return err
// }

// _ = stats1
// _ = stats2

return nil
}

0 comments on commit 72d6268

Please sign in to comment.