@@ -2,23 +2,24 @@ package db
22
33import (
44 "context"
5+ "path/filepath"
56 "testing"
67 "time"
78
89 "github.com/stretchr/testify/require"
910)
1011
1112type mockSession struct {
12- WriteLogLineFuc func (ctx context.Context , payload WriteLogLinePayload ) error
13+ WriteLogLinesBatchFunc func (ctx context.Context , payload WriteLogLinesBatchPayload ) error
1314}
1415
1516var _ Session = (* mockSession )(nil )
1617
17- func (s * mockSession ) WriteLogLine (
18+ func (s * mockSession ) WriteLogLinesBatch (
1819 ctx context.Context ,
19- payload WriteLogLinePayload ,
20+ payload WriteLogLinesBatchPayload ,
2021) error {
21- return s .WriteLogLineFuc (ctx , payload )
22+ return s .WriteLogLinesBatchFunc (ctx , payload )
2223}
2324
2425func TestSessionLogWriter (t * testing.T ) {
@@ -32,11 +33,11 @@ func TestSessionLogWriter(t *testing.T) {
3233 }
3334
3435 t .Run ("write serial" , func (t * testing.T ) {
35- var wrote []WriteLogLinePayload
36+ var wrote []string
3637
3738 mockSession := & mockSession {
38- WriteLogLineFuc : func (ctx context.Context , payload WriteLogLinePayload ) error {
39- wrote = append (wrote , payload )
39+ WriteLogLinesBatchFunc : func (ctx context.Context , payload WriteLogLinesBatchPayload ) error {
40+ wrote = append (wrote , payload . Lines ... )
4041
4142 return nil
4243 },
@@ -53,16 +54,16 @@ func TestSessionLogWriter(t *testing.T) {
5354 require .NoError (t , w .Close ())
5455
5556 require .Len (t , wrote , 2 )
56- require .Equal (t , "hello" , wrote [0 ]. Line )
57- require .Equal (t , "world" , wrote [1 ]. Line )
57+ require .Equal (t , "hello" , wrote [0 ])
58+ require .Equal (t , "world" , wrote [1 ])
5859 })
5960
6061 t .Run ("write partial" , func (t * testing.T ) {
61- var wrote []WriteLogLinePayload
62+ var wrote []string
6263
6364 mockSession := & mockSession {
64- WriteLogLineFuc : func (ctx context.Context , payload WriteLogLinePayload ) error {
65- wrote = append (wrote , payload )
65+ WriteLogLinesBatchFunc : func (ctx context.Context , payload WriteLogLinesBatchPayload ) error {
66+ wrote = append (wrote , payload . Lines ... )
6667
6768 return nil
6869 },
@@ -81,8 +82,39 @@ func TestSessionLogWriter(t *testing.T) {
8182 require .NoError (t , w .Close ())
8283
8384 require .Len (t , wrote , 3 )
84- require .Equal (t , "hello" , wrote [0 ]. Line )
85- require .Equal (t , "world" , wrote [1 ]. Line )
86- require .Equal (t , "foo" , wrote [2 ]. Line )
85+ require .Equal (t , "hello" , wrote [0 ])
86+ require .Equal (t , "world" , wrote [1 ])
87+ require .Equal (t , "foo" , wrote [2 ])
8788 })
8889}
90+
91+ // go test -run=^$ -benchtime 30s -bench ^BenchmarkSessionLogWriter$ github.com/b4fun/ku/server/internal/db
92+ func BenchmarkSessionLogWriter (b * testing.B ) {
93+ run := func () {
94+ ctx , cancel := context .WithCancel (context .Background ())
95+ defer cancel ()
96+
97+ p := b .TempDir ()
98+ dbFile := filepath .Join (p , "test.db" )
99+ dbProvider , err := NewSqliteProvider (dbFile )
100+ require .NoError (b , err )
101+
102+ _ , session , err := dbProvider .CreateSession (ctx , & CreateSessionOpts {Prefix : "test" })
103+ require .NoError (b , err )
104+
105+ writer := SessionLogWriteCloser (ctx , session , 100 * time .Millisecond )
106+
107+ for i := 0 ; i < 10000 ; i ++ {
108+ content := []byte ("hello world\n " )
109+
110+ _ , err := writer .Write (content )
111+ require .NoError (b , err )
112+ }
113+
114+ require .NoError (b , writer .Close ())
115+ }
116+
117+ for i := 0 ; i < b .N ; i ++ {
118+ run ()
119+ }
120+ }
0 commit comments