You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
512 lines
12 KiB
Go
512 lines
12 KiB
Go
package statcampaign
|
|
|
|
import (
|
|
"context"
|
|
"joybuywork/internal/repository/mtype"
|
|
"joybuywork/internal/service/queue/types"
|
|
"joybuywork/internal/utils/logic"
|
|
"fmt"
|
|
"math/rand"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
_ "joybuywork/test"
|
|
)
|
|
|
|
// TestLogic_Custom 测试自定义行为事件
|
|
func TestLogic_Custom(t *testing.T) {
|
|
bt := int32(mtype.FissionBehaviorTypeCustom)
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "ios",
|
|
OsVersion: "16",
|
|
DeviceModel: xid.New().String(),
|
|
DeviceID: xid.New().String(),
|
|
UserID: "123",
|
|
CampaignID: 3458,
|
|
Event: "pay",
|
|
EventData: []types.EventData{
|
|
{
|
|
EventKey: "pay_10",
|
|
EventVal: "10",
|
|
},
|
|
},
|
|
ProjectID: 554,
|
|
Channel: "",
|
|
FromSrv: false,
|
|
Hid: "",
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("TestLogic_Custom err: %v", errCode)
|
|
}
|
|
|
|
// TestLogic_AppInstall 测试App安装行为
|
|
func TestLogic_AppInstall(t *testing.T) {
|
|
bt := int32(mtype.FissionBehaviorTypeAppInstall)
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "ios",
|
|
OsVersion: "16",
|
|
DeviceModel: xid.New().String(),
|
|
DeviceID: xid.New().String(),
|
|
UserID: "123",
|
|
CampaignID: 3458,
|
|
OwnerID: "",
|
|
ProjectID: 554,
|
|
Channel: "",
|
|
FromSrv: false,
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("TestLogic_AppInstall err: %v", errCode)
|
|
}
|
|
|
|
// TestLogic_AppOpen 测试App打开行为
|
|
func TestLogic_AppOpen(t *testing.T) {
|
|
bt := int32(mtype.FissionBehaviorTypeAppOpen)
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "ios",
|
|
OsVersion: "16",
|
|
DeviceModel: xid.New().String(),
|
|
DeviceID: xid.New().String(),
|
|
UserID: "123",
|
|
CampaignID: 3458,
|
|
OwnerID: "",
|
|
ProjectID: 554,
|
|
Channel: "",
|
|
FromSrv: false,
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("TestLogic_AppOpen err: %v", errCode)
|
|
}
|
|
|
|
// TestLogic_AppRegister 测试App注册行为
|
|
func TestLogic_AppRegister(t *testing.T) {
|
|
bt := int32(mtype.FissionBehaviorTypeAppRegister)
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "ios",
|
|
OsVersion: "16",
|
|
DeviceModel: xid.New().String(),
|
|
DeviceID: xid.New().String(),
|
|
UserID: "123",
|
|
CampaignID: 3458,
|
|
OwnerID: "",
|
|
ProjectID: 554,
|
|
Channel: "",
|
|
FromSrv: false,
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("TestLogic_AppRegister err: %v", errCode)
|
|
}
|
|
|
|
// TestLogic_WebFirstOpen 测试Web首次打开行为
|
|
func TestLogic_WebFirstOpen(t *testing.T) {
|
|
bt := int32(mtype.FissionBehaviorTypeWebFirstOpen)
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "web",
|
|
OsVersion: "chrome",
|
|
DeviceModel: xid.New().String(),
|
|
DeviceID: xid.New().String(),
|
|
UserID: "",
|
|
WebId: xid.New().String(),
|
|
CampaignID: 3458,
|
|
OwnerID: "",
|
|
ProjectID: 554,
|
|
Channel: "",
|
|
FromSrv: false,
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("TestLogic_WebFirstOpen err: %v", errCode)
|
|
}
|
|
|
|
// TestLogic_FromServer 测试来自服务端的统计
|
|
func TestLogic_FromServer(t *testing.T) {
|
|
bt := int32(mtype.FissionBehaviorTypeCustom)
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: "",
|
|
Os: "",
|
|
OsVersion: "",
|
|
DeviceModel: "",
|
|
DeviceID: "",
|
|
UserID: "server_user_1",
|
|
CampaignID: 3458,
|
|
Event: "pay",
|
|
EventData: []types.EventData{
|
|
{
|
|
EventKey: "pay_10",
|
|
EventVal: "10",
|
|
},
|
|
},
|
|
ProjectID: 554,
|
|
Channel: "",
|
|
FromSrv: true,
|
|
Hid: "",
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("TestLogic_FromServer err: %v", errCode)
|
|
}
|
|
|
|
// TestLogic_WithChannel 测试带渠道标识的行为统计
|
|
func TestLogic_WithChannel(t *testing.T) {
|
|
bt := int32(mtype.FissionBehaviorTypeAppInstall)
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "android",
|
|
OsVersion: "13",
|
|
DeviceModel: xid.New().String(),
|
|
DeviceID: xid.New().String(),
|
|
UserID: "123",
|
|
CampaignID: 3458,
|
|
OwnerID: "",
|
|
ProjectID: 554,
|
|
Channel: "test_channel_mark",
|
|
FromSrv: false,
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("TestLogic_WithChannel err: %v", errCode)
|
|
}
|
|
|
|
// TestLogic_InvalidCampaign 测试无效活动ID
|
|
func TestLogic_InvalidCampaign(t *testing.T) {
|
|
bt := int32(mtype.FissionBehaviorTypeCustom)
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "ios",
|
|
OsVersion: "16",
|
|
DeviceModel: xid.New().String(),
|
|
DeviceID: xid.New().String(),
|
|
UserID: "123",
|
|
CampaignID: 0, // 无效的活动ID
|
|
Event: "pay",
|
|
EventData: []types.EventData{
|
|
{
|
|
EventKey: "pay_10",
|
|
EventVal: "10",
|
|
},
|
|
},
|
|
ProjectID: 554,
|
|
Channel: "",
|
|
FromSrv: false,
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("TestLogic_InvalidCampaign err: %v", errCode)
|
|
|
|
if errCode == nil {
|
|
t.Log("expected error for invalid campaign but got nil")
|
|
}
|
|
}
|
|
|
|
// TestLogic_Purchase 测试支付行为统计
|
|
func TestLogic_Purchase(t *testing.T) {
|
|
bt := int32(mtype.FissionBehaviorTypePurchase)
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "ios",
|
|
OsVersion: "16",
|
|
DeviceModel: xid.New().String(),
|
|
DeviceID: xid.New().String(),
|
|
UserID: "123",
|
|
CampaignID: 3458,
|
|
Event: "purchases",
|
|
EventData: []types.EventData{
|
|
{
|
|
EventKey: "count",
|
|
EventVal: "100",
|
|
},
|
|
{
|
|
EventKey: "times",
|
|
EventVal: "1",
|
|
},
|
|
},
|
|
ProjectID: 554,
|
|
Channel: "",
|
|
FromSrv: false,
|
|
PurchasesData: &types.PurchaseData{
|
|
TransactionId: xid.New().String(),
|
|
TransactionDesc: "test purchase",
|
|
Amount: 100,
|
|
Currency: "USD",
|
|
PurchaseCount: 1,
|
|
ContentItems: []types.PurchaseItem{
|
|
{
|
|
PayTime: time.Now().Format("2006-01-02 15:04:05"),
|
|
ProductId: "product_1",
|
|
ProductName: "Test Product",
|
|
ProductPrice: 100,
|
|
ProductQuantity: 1,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("TestLogic_Purchase err: %v", errCode)
|
|
}
|
|
|
|
// TestLogic_WithOwnerId 测试带上级用户ID的行为
|
|
func TestLogic_WithOwnerId(t *testing.T) {
|
|
bt := int32(mtype.FissionBehaviorTypeAppOpen)
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "ios",
|
|
OsVersion: "16",
|
|
DeviceModel: xid.New().String(),
|
|
DeviceID: xid.New().String(),
|
|
UserID: "new_user_1",
|
|
CampaignID: 3458,
|
|
OwnerID: "123", // 上级用户ID
|
|
ProjectID: 554,
|
|
Channel: "",
|
|
FromSrv: false,
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("TestLogic_WithOwnerId err: %v", errCode)
|
|
}
|
|
|
|
// TestLogic_Concurrent 并发测试多种行为类型
|
|
func TestLogic_Concurrent(t *testing.T) {
|
|
behaviorTypes := []int32{
|
|
mtype.FissionBehaviorTypeCustom,
|
|
mtype.FissionBehaviorTypeAppInstall,
|
|
mtype.FissionBehaviorTypeAppOpen,
|
|
mtype.FissionBehaviorTypeAppRegister,
|
|
}
|
|
|
|
wg := sync.WaitGroup{}
|
|
for i, bt := range behaviorTypes {
|
|
wg.Add(1)
|
|
go func(idx int, behaviorType int32) {
|
|
defer wg.Done()
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: behaviorType,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "ios",
|
|
OsVersion: "16",
|
|
DeviceModel: xid.New().String(),
|
|
DeviceID: xid.New().String(),
|
|
UserID: fmt.Sprintf("user_%d", idx),
|
|
CampaignID: 3458,
|
|
OwnerID: "",
|
|
ProjectID: 554,
|
|
Channel: "",
|
|
FromSrv: false,
|
|
}
|
|
|
|
// 为自定义事件设置event数据
|
|
if behaviorType == mtype.FissionBehaviorTypeCustom {
|
|
val.Event = "pay"
|
|
val.EventData = []types.EventData{
|
|
{
|
|
EventKey: "pay_10",
|
|
EventVal: "10",
|
|
},
|
|
}
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("Concurrent[%d] behaviorType: %d, err: %v", idx, behaviorType, errCode)
|
|
}(i, bt)
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
// TestLogic_ViewDownload 测试浏览下载页行为
|
|
func TestLogic_ViewDownload(t *testing.T) {
|
|
bt := int32(mtype.FissionBehaviorTypeViewDownload)
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "web",
|
|
OsVersion: "chrome",
|
|
DeviceModel: xid.New().String(),
|
|
DeviceID: xid.New().String(),
|
|
UserID: "",
|
|
WebId: xid.New().String(),
|
|
CampaignID: 3458,
|
|
OwnerID: "123",
|
|
ProjectID: 554,
|
|
Channel: "",
|
|
FromSrv: false,
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
t.Logf("TestLogic_ViewDownload err: %v", errCode)
|
|
}
|
|
|
|
// BenchmarkLogic 基准测试自定义事件处理性能
|
|
func BenchmarkLogic(b *testing.B) {
|
|
var id = map[string]bool{
|
|
xid.New().String(): true,
|
|
xid.New().String(): true,
|
|
xid.New().String(): true,
|
|
"": false,
|
|
}
|
|
|
|
bt := int32(mtype.FissionBehaviorTypeCustom)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
var dm string
|
|
for k, v := range id {
|
|
if v {
|
|
dm = k
|
|
} else {
|
|
dm = xid.New().String()
|
|
}
|
|
break
|
|
}
|
|
|
|
var ctx = context.Background()
|
|
var val = types.StatCampaignValue{
|
|
BehaviorType: bt,
|
|
BehaviorTime: time.Now().Unix(),
|
|
IpAddress: fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(255), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)),
|
|
Os: "ios",
|
|
OsVersion: "16",
|
|
DeviceModel: dm,
|
|
UserID: "123",
|
|
CampaignID: 3458,
|
|
OwnerID: "",
|
|
}
|
|
|
|
var container = new(Logic)
|
|
{
|
|
container.Init(ctx)
|
|
container.Req = &val
|
|
}
|
|
|
|
errCode := logic.Run(container)
|
|
b.Log(errCode)
|
|
}
|
|
}
|