-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_benchmark_test.go
72 lines (62 loc) · 2.02 KB
/
sync_benchmark_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package crm_test
import (
"context"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/require"
crm "github.com/vertoforce/generic-crm"
"github.com/vertoforce/generic-crm/backends/googlesheet"
)
func BenchmarkSyncGoogleSheet(b *testing.B) {
ctx := context.Background()
// Set up testing google sheet client
googleSheetCRM, err := googlesheet.New(context.Background(), &googlesheet.Config{
GoogleClientSecretFile: os.Getenv("GoogleClientSecretFile"),
SpreadsheetURL: os.Getenv("TESTING_SPREADSHEET_URL"),
SheetName: "Sheet1",
})
require.NoError(b, err)
// Disable automatic sync, we are doing the test locally
googleSheetCRM.WaitToSynchronize = true
// Insert some data
for i := 0; i < 1000; i++ {
googleSheetCRM.CreateItem(ctx, &crm.DefaultItem{Fields: map[string]interface{}{
"Name": fmt.Sprintf("Name %d", i),
"Item": fmt.Sprintf("%d", i),
}})
}
// Build set of updates
updates := []crm.Item{}
// Add a bunch of updates to items
for i := 0; i < 1000; i++ {
updates = append(updates, &crm.DefaultItem{Fields: map[string]interface{}{"Name": fmt.Sprintf("Name %d", i), "Item": "New item title"}})
}
searchFunc := func(i crm.Item) map[string]interface{} {
return map[string]interface{}{"Name": i.GetFields()["Name"]}
}
b.Run("GoogleSheetSync", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// set thread to send updated items to channel
items := make(chan crm.Item)
go func() {
for _, update := range updates {
items <- update
}
close(items)
}()
err = crm.NewSyncMachine().WithCRMs(googleSheetCRM).SetDeleteUntouchedItems(false).WithSearchFunction(searchFunc).Sync(ctx, items)
require.NoError(b, err)
}
})
// Clear the sheet
// Turns out this is too long since it does it one by one
// items, err = googleSheetCRM.GetItems(ctx)
// require.NoError(b, err)
// itemsSlice := []crm.Item{}
// for item := range items {
// itemsSlice = append(itemsSlice, item)
// }
// err = googleSheetCRM.RemoveItems(ctx, itemsSlice...)
// require.NoError(b, err)
}