Skip to content

Commit

Permalink
twitch: add benchmark for payload decoding
Browse files Browse the repository at this point in the history
zephyrtronium committed Sep 11, 2024
1 parent fd5f439 commit 72eb582
Showing 9 changed files with 97 additions and 11 deletions.
8 changes: 8 additions & 0 deletions twitch/eventsub/event.go
Original file line number Diff line number Diff line change
@@ -70,3 +70,11 @@ type metadata struct {
// SubscriptionVersion is the version of the subscription type.
SubscriptionVersion string `json:"subscription_version"`
}

type session struct {
ID string `json:"id"`
Status string `json:"status"`
Keepalive int `json:"keepalive_timeout_seconds"`
Reconnect string `json:"reconnect_url"`
Connected string `json:"connected_at"`
}
43 changes: 43 additions & 0 deletions twitch/eventsub/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package eventsub

import (
"embed"
"testing"

"github.com/go-json-experiment/json"
)

//go:embed testdata/*.message.json
var messages embed.FS

func BenchmarkEventExtra(b *testing.B) {
cases := []struct {
name string
file string
}{
{
name: "keepalive",
file: "keepalive.message.json",
},
{
name: "notification-payload",
file: "notification-payload.message.json",
},
}
for _, c := range cases {
b.Run(c.name, func(b *testing.B) {
p, err := messages.ReadFile("testdata/" + c.file)
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
var m message
if err := json.Unmarshal(p, &m); err != nil {
b.Error(err)
}
}
})
}
}
4 changes: 2 additions & 2 deletions twitch/eventsub/stream_test.go
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import (

func TestStream(t *testing.T) {
t.Run("online", func(t *testing.T) {
evt := Testdata("stream.online.json")
evt := Testdata("stream.online.event.json")
var got eventsub.Stream
if err := json.Unmarshal([]byte(evt.Event), &got); err != nil {
t.Errorf("couldn't unmarshal payload as stream: %v", err)
@@ -28,7 +28,7 @@ func TestStream(t *testing.T) {
}
})
t.Run("offline", func(t *testing.T) {
evt := Testdata("stream.offline.json")
evt := Testdata("stream.offline.event.json")
var got eventsub.Stream
if err := json.Unmarshal([]byte(evt.Event), &got); err != nil {
t.Errorf("couldn't unmarshal payload as stream: %v", err)
8 changes: 8 additions & 0 deletions twitch/eventsub/testdata/keepalive.message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"metadata": {
"message_id": "84c1e79a-2a4b-4c13-ba0b-4312293e9308",
"message_type": "session_keepalive",
"message_timestamp": "2023-07-19T10:11:12.634234626Z"
},
"payload": {}
}
35 changes: 35 additions & 0 deletions twitch/eventsub/testdata/notification-payload.message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"metadata": {
"message_id": "befa7b53-d79d-478f-86b9-120f112b044e",
"message_type": "notification",
"message_timestamp": "2022-11-16T10:11:12.464757833Z",
"subscription_type": "channel.follow",
"subscription_version": "1"
},
"payload": {
"subscription": {
"id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4",
"status": "enabled",
"type": "channel.follow",
"version": "1",
"cost": 1,
"condition": {
"broadcaster_user_id": "12826"
},
"transport": {
"method": "websocket",
"session_id": "AQoQexAWVYKSTIu4ec_2VAxyuhAB"
},
"created_at": "2022-11-16T10:11:12.464757833Z"
},
"event": {
"user_id": "1337",
"user_login": "awesome_user",
"user_name": "Awesome_User",
"broadcaster_user_id": "12826",
"broadcaster_user_login": "twitch",
"broadcaster_user_name": "Twitch",
"followed_at": "2023-07-15T18:16:11.17106713Z"
}
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion twitch/eventsub/testdata_test.go
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import (
"github.com/zephyrtronium/robot/twitch/eventsub"
)

//go:embed testdata
//go:embed testdata/*.event.json
var testdata embed.FS

func Testdata(name string) *eventsub.Event {
8 changes: 0 additions & 8 deletions twitch/eventsub/websocket.go
Original file line number Diff line number Diff line change
@@ -23,14 +23,6 @@ type Session struct {
timeout time.Duration
}

type session struct {
ID string `json:"id"`
Status string `json:"status"`
Keepalive int `json:"keepalive_timeout_seconds"`
Reconnect string `json:"reconnect_url"`
Connected string `json:"connected_at"`
}

// Connect connects to the Twitch EventSub server.
// If the HTTP client is nil, [http.DefaultClient] is used instead.
// keepalive is the interval in seconds to request keepalive messages.

0 comments on commit 72eb582

Please sign in to comment.