Files
EdgeAPI/internal/db/models/http_access_log_dao_test.go

218 lines
5.0 KiB
Go
Raw Permalink Normal View History

2020-10-10 11:49:21 +08:00
package models
import (
2022-04-02 11:52:42 +08:00
"encoding/json"
2020-10-10 11:49:21 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
2021-01-10 17:34:35 +08:00
"github.com/iwind/TeaGo/dbs"
2020-10-10 19:21:32 +08:00
timeutil "github.com/iwind/TeaGo/utils/time"
2020-10-10 11:49:21 +08:00
"testing"
"time"
)
2021-12-14 14:55:22 +08:00
func TestCreateHTTPAccessLog(t *testing.T) {
dbs.NotifyReady()
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
2020-10-10 11:49:21 +08:00
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
var accessLog = &pb.HTTPAccessLog{
2020-10-10 11:49:21 +08:00
ServerId: 1,
NodeId: 4,
Status: 200,
Timestamp: time.Now().Unix(),
}
var dao = randomHTTPAccessLogDAO()
2020-10-10 11:49:21 +08:00
t.Log("dao:", dao)
2021-12-14 14:55:22 +08:00
// 先初始化
_ = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
var before = time.Now()
defer func() {
t.Log(time.Since(before).Seconds()*1000, "ms")
}()
for i := 0; i < 1000; i++ {
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
if err != nil {
t.Fatal(err)
}
}
t.Log("ok")
}
func TestCreateHTTPAccessLog_Tx(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
var accessLog = &pb.HTTPAccessLog{
ServerId: 1,
NodeId: 4,
Status: 200,
Timestamp: time.Now().Unix(),
}
var dao = randomHTTPAccessLogDAO()
t.Log("dao:", dao)
// 先初始化
_ = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
var before = time.Now()
defer func() {
t.Log(time.Since(before).Seconds()*1000, "ms")
}()
tx, err = dao.DAO.Instance.Begin()
if err != nil {
t.Fatal(err)
}
for i := 0; i < 200; i++ {
2021-12-14 14:55:22 +08:00
err = SharedHTTPAccessLogDAO.CreateHTTPAccessLog(tx, dao.DAO, accessLog)
if err != nil {
t.Fatal(err)
}
2020-10-10 11:49:21 +08:00
}
err = tx.Commit()
if err != nil {
t.Fatal(err)
}
2020-10-10 11:49:21 +08:00
t.Log("ok")
}
2020-10-10 19:21:32 +08:00
func TestHTTPAccessLogDAO_ListAccessLogs(t *testing.T) {
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
2020-10-10 19:21:32 +08:00
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
2022-04-17 16:18:53 +08:00
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, -1, "", 10, timeutil.Format("Ymd"), "", "", 0, 0, 0, false, false, 0, 0, 0, false, 0, "", "", "")
2020-10-10 19:21:32 +08:00
if err != nil {
t.Fatal(err)
}
t.Log("requestId:", requestId, "hasMore:", hasMore)
if len(accessLogs) == 0 {
t.Log("no access logs yet")
return
}
for _, accessLog := range accessLogs {
t.Log(accessLog.Id, accessLog.CreatedAt, timeutil.FormatTime("H:i:s", int64(accessLog.CreatedAt)))
}
}
func TestHTTPAccessLogDAO_ListAccessLogs_Page(t *testing.T) {
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
2020-10-10 19:21:32 +08:00
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
lastRequestId := ""
times := 0 // 防止循环次数太多
for {
before := time.Now()
2022-04-17 16:18:53 +08:00
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, -1, lastRequestId, 2, timeutil.Format("Ymd"), "", "", 0, 0, 0, false, false, 0, 0, 0, false, 0, "", "", "")
2020-10-10 19:21:32 +08:00
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)
}
lastRequestId = requestId
if len(accessLogs) == 0 {
break
}
t.Log("===")
t.Log("requestId:", requestId[:10]+"...", "hasMore:", hasMore, "cost:", cost*1000, "ms")
for _, accessLog := range accessLogs {
t.Log(accessLog.Id, accessLog.CreatedAt, timeutil.FormatTime("H:i:s", int64(accessLog.CreatedAt)))
}
times++
if times > 10 {
break
}
}
}
func TestHTTPAccessLogDAO_ListAccessLogs_Reverse(t *testing.T) {
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
2020-10-10 19:21:32 +08:00
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
before := time.Now()
2022-04-17 16:18:53 +08:00
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, -1, "16023261176446590001000000000000003500000004", 2, timeutil.Format("Ymd"), "", "", 0, 0, 0, true, false, 0, 0, 0, false, 0, "", "", "")
2020-10-10 19:21:32 +08:00
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)
}
t.Log("===")
t.Log("requestId:", requestId[:19]+"...", "hasMore:", hasMore, "cost:", cost*1000, "ms")
if len(accessLogs) > 0 {
t.Log("accessLog:", accessLogs[0].RequestId[:19]+"...", len(accessLogs[0].RequestId))
}
}
func TestHTTPAccessLogDAO_ListAccessLogs_Page_NotExists(t *testing.T) {
2021-01-10 17:34:35 +08:00
var tx *dbs.Tx
2020-10-10 19:21:32 +08:00
err := NewDBNodeInitializer().loop()
if err != nil {
t.Fatal(err)
}
lastRequestId := ""
times := 0 // 防止循环次数太多
for {
before := time.Now()
2022-04-17 16:18:53 +08:00
accessLogs, requestId, hasMore, err := SharedHTTPAccessLogDAO.ListAccessLogs(tx, -1, lastRequestId, 2, timeutil.Format("Ymd", time.Now().AddDate(0, 0, 1)), "", "", 0, 0, 0, false, false, 0, 0, 0, false, 0, "", "", "")
2020-10-10 19:21:32 +08:00
cost := time.Since(before).Seconds()
if err != nil {
t.Fatal(err)
}
lastRequestId = requestId
if len(accessLogs) == 0 {
break
}
t.Log("===")
t.Log("requestId:", requestId[:10]+"...", "hasMore:", hasMore, "cost:", cost*1000, "ms")
for _, accessLog := range accessLogs {
t.Log(accessLog.Id, accessLog.CreatedAt, timeutil.FormatTime("H:i:s", int64(accessLog.CreatedAt)))
}
times++
if times > 10 {
break
}
}
}
2022-04-02 11:52:42 +08:00
func BenchmarkHTTPAccessLogDAO_JSONEncode(b *testing.B) {
var accessLog = &pb.HTTPAccessLog{
RequestPath: "/hello/world",
}
for i := 0; i < b.N; i++ {
_, _ = json.Marshal(accessLog)
}
}