package notify import ( "fmt" "testing" "time" "firebase.google.com/go/v4/messaging" "github.com/appleboy/gorush/core" "github.com/sideshow/apns2" ) var ( benchmarkBytes []byte benchmarkNotification *PushNotification benchmarkFCMMessages []*messaging.Message benchmarkAPNSMessage *apns2.Notification ) func benchmarkPushNotification(tokenCount int) *PushNotification { tokens := make([]string, tokenCount) for i := range tokens { tokens[i] = fmt.Sprintf("device-token-%03d", i) } expiration := time.Unix(1_725_184_800, 0).Unix() badge := 7 return &PushNotification{ ID: "campaign-2024-09-01", Tokens: tokens, Platform: core.PlatFormAndroid, Message: "Your order is ready for pickup", Title: "Order update", Image: "https://example.com/order.png", Priority: HIGH, ContentAvailable: true, MutableContent: true, Sound: "default", Data: D{ "order_id": "ord_01J6PTQ8E9ZC5Q8DXNK5NVW9CR", "attempt": 2, "route": map[string]interface{}{ "screen": "order", "source": "push", }, }, Expiration: &expiration, Badge: &badge, Category: "ORDER_UPDATE", ThreadID: "orders", URLArgs: []string{"orders", "ord_01J6PTQ8E9ZC5Q8DXNK5NVW9CR"}, } } func BenchmarkQueueEncode(b *testing.B) { req := benchmarkPushNotification(100) b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { benchmarkBytes = req.Bytes() } } func BenchmarkQueueDecode(b *testing.B) { data := benchmarkPushNotification(100).Bytes() b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { var req *PushNotification if err := json.Unmarshal(data, &req); err != nil { b.Fatal(err) } benchmarkNotification = req } } func BenchmarkBuildFCMOneRecipient(b *testing.B) { req := benchmarkPushNotification(1) b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { req.Notification = nil req.Android = nil req.APNS = nil benchmarkFCMMessages = GetAndroidNotification(req) } } func BenchmarkBuildFCM100Recipients(b *testing.B) { req := benchmarkPushNotification(100) b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { req.Notification = nil req.Android = nil req.APNS = nil benchmarkFCMMessages = GetAndroidNotification(req) } } func BenchmarkBuildAPNS(b *testing.B) { req := benchmarkPushNotification(1) req.Platform = core.PlatFormIos b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { benchmarkAPNSMessage = GetIOSNotification(req) } }