-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbatch_operations_test.go
More file actions
247 lines (205 loc) · 8.66 KB
/
batch_operations_test.go
File metadata and controls
247 lines (205 loc) · 8.66 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package integration
import (
"context"
"fmt"
"testing"
"time"
kwilcrypto "github.com/kwilteam/kwil-db/core/crypto"
"github.com/kwilteam/kwil-db/core/crypto/auth"
kwiltypes "github.com/kwilteam/kwil-db/core/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/trufnetwork/sdk-go/core/tnclient"
"github.com/trufnetwork/sdk-go/core/types"
"github.com/trufnetwork/sdk-go/core/util"
)
func TestBatchOperations(t *testing.T) {
ctx := context.Background()
fixture := NewServerFixture(t)
err := fixture.Setup()
t.Cleanup(func() {
fixture.Teardown()
})
require.NoError(t, err, "Failed to setup server fixture")
deployerWallet, err := kwilcrypto.Secp256k1PrivateKeyFromHex(AnonWalletPK)
require.NoError(t, err, "failed to parse anon wallet private key")
tnClient, err := tnclient.NewClient(ctx, TestKwilProvider, tnclient.WithSigner(auth.GetUserSigner(deployerWallet)))
require.NoError(t, err, "failed to create client")
authorizeWalletToDeployStreams(t, ctx, fixture, deployerWallet)
t.Run("TestSequentialSmallBatches", func(t *testing.T) {
streamId := util.GenerateStreamId("test-sequential-small")
streamLocator := tnClient.OwnStreamLocator(streamId)
// Set up cleanup
t.Cleanup(func() {
destroyResult, err := tnClient.DestroyStream(ctx, streamId)
assertNoErrorOrFail(t, err, "Failed to destroy stream")
waitTxToBeMinedWithSuccess(t, ctx, tnClient, destroyResult)
})
// Deploy and initialize stream
deployTxHash, err := tnClient.DeployStream(ctx, streamId, types.StreamTypePrimitive)
assertNoErrorOrFail(t, err, "Failed to deploy stream")
waitTxToBeMinedWithSuccess(t, ctx, tnClient, deployTxHash)
deployedStream, err := tnClient.LoadPrimitiveActions()
assertNoErrorOrFail(t, err, "Failed to load stream")
const numBatches = 500
const recordsPerBatch = 5
baseTimestamp := 1672531200 // Start from 2023-01-01
// Insert multiple batches without waiting
txHashes := make([]kwiltypes.Hash, 0, numBatches)
startTime := time.Now()
for batch := 0; batch <= numBatches; batch++ {
records := make([]types.InsertRecordInput, recordsPerBatch)
for i := 0; i < recordsPerBatch; i++ {
records[i] = types.InsertRecordInput{
DataProvider: streamLocator.DataProvider.Address(),
StreamId: streamLocator.StreamId.String(),
EventTime: baseTimestamp + (batch * 86400) + (i * 3600),
Value: float64(batch*100 + i),
}
}
txHash, err := deployedStream.InsertRecords(ctx, records)
assertNoErrorOrFail(t, err, "Failed to insert batch")
txHashes = append(txHashes, txHash)
}
insertionDuration := time.Since(startTime)
fmt.Printf("[Small Batches] All insertions completed in %v (avg %v per batch, %v per record)\n",
insertionDuration,
insertionDuration/time.Duration(numBatches),
insertionDuration/time.Duration(numBatches*recordsPerBatch))
// Wait for all transactions after sending them all
waitStart := time.Now()
for _, txHash := range txHashes {
waitTxToBeMinedWithSuccess(t, ctx, tnClient, txHash)
}
waitDuration := time.Since(waitStart)
fmt.Printf("[Small Batches] All transactions confirmed in %v\n", waitDuration)
// Verify total number of records
totalRecords := numBatches * recordsPerBatch
dateFrom := baseTimestamp
dateTo := baseTimestamp + (numBatches * 86400)
records, err := deployedStream.GetRecord(ctx, types.GetRecordInput{
DataProvider: streamLocator.DataProvider.Address(),
StreamId: streamLocator.StreamId.String(),
From: &dateFrom,
To: &dateTo,
})
assertNoErrorOrFail(t, err, "Failed to query records")
assert.Equal(t, totalRecords, len(records), "Unexpected number of records")
})
t.Run("TestSequentialLargeBatches", func(t *testing.T) {
streamId := util.GenerateStreamId("test-sequential-large")
streamLocator := tnClient.OwnStreamLocator(streamId)
// Set up cleanup
t.Cleanup(func() {
destroyResult, err := tnClient.DestroyStream(ctx, streamId)
assertNoErrorOrFail(t, err, "Failed to destroy stream")
waitTxToBeMinedWithSuccess(t, ctx, tnClient, destroyResult)
})
// Deploy and initialize stream
deployTxHash, err := tnClient.DeployStream(ctx, streamId, types.StreamTypePrimitive)
assertNoErrorOrFail(t, err, "Failed to deploy stream")
waitTxToBeMinedWithSuccess(t, ctx, tnClient, deployTxHash)
deployedStream, err := tnClient.LoadPrimitiveActions()
assertNoErrorOrFail(t, err, "Failed to load stream")
const numBatches = 500
const recordsPerBatch = 100
baseTimestamp := 1672531200 // Start from 2023-01-01
// Insert multiple batches without waiting
txHashes := make([]kwiltypes.Hash, 0, numBatches)
startTime := time.Now()
for batch := 0; batch <= numBatches; batch++ {
records := make([]types.InsertRecordInput, recordsPerBatch)
for i := 0; i < recordsPerBatch; i++ {
records[i] = types.InsertRecordInput{
DataProvider: streamLocator.DataProvider.Address(),
StreamId: streamLocator.StreamId.String(),
EventTime: baseTimestamp + (batch * 86400) + (i * 300), // 5-minute intervals
Value: float64(batch*1000 + i),
}
}
txHash, err := deployedStream.InsertRecords(ctx, records)
assertNoErrorOrFail(t, err, "Failed to insert batch")
txHashes = append(txHashes, txHash)
}
insertionDuration := time.Since(startTime)
fmt.Printf("[Large Batches] All insertions completed in %v (avg %v per batch, %v per record)\n",
insertionDuration,
insertionDuration/time.Duration(numBatches),
insertionDuration/time.Duration(numBatches*recordsPerBatch))
// Wait for all transactions after sending them all
waitStart := time.Now()
for _, txHash := range txHashes {
waitTxToBeMinedWithSuccess(t, ctx, tnClient, txHash)
}
waitDuration := time.Since(waitStart)
fmt.Printf("[Large Batches] All transactions confirmed in %v\n", waitDuration)
// Verify total number of records
totalRecords := numBatches * recordsPerBatch
dateFrom := baseTimestamp
dateTo := baseTimestamp + (numBatches * 86400)
records, err := deployedStream.GetRecord(ctx, types.GetRecordInput{
DataProvider: streamLocator.DataProvider.Address(),
StreamId: streamLocator.StreamId.String(),
From: &dateFrom,
To: &dateTo,
})
assertNoErrorOrFail(t, err, "Failed to query records")
assert.Equal(t, totalRecords, len(records), "Unexpected number of records")
})
t.Run("TestRapidSingleRecordInserts", func(t *testing.T) {
streamId := util.GenerateStreamId("test-rapid-singles")
streamLocator := tnClient.OwnStreamLocator(streamId)
// Set up cleanup
t.Cleanup(func() {
destroyResult, err := tnClient.DestroyStream(ctx, streamId)
assertNoErrorOrFail(t, err, "Failed to destroy stream")
waitTxToBeMinedWithSuccess(t, ctx, tnClient, destroyResult)
})
// Deploy and initialize stream
deployTxHash, err := tnClient.DeployStream(ctx, streamId, types.StreamTypePrimitive)
assertNoErrorOrFail(t, err, "Failed to deploy stream")
waitTxToBeMinedWithSuccess(t, ctx, tnClient, deployTxHash)
deployedStream, err := tnClient.LoadPrimitiveActions()
assertNoErrorOrFail(t, err, "Failed to load stream")
const numRecords = 500
baseTimestamp := 1672531200 // Start from 2023-01-01
// Rapidly insert individual records without waiting
txHashes := make([]kwiltypes.Hash, 0, numRecords)
startTime := time.Now()
for i := 0; i <= numRecords; i++ {
records := []types.InsertRecordInput{
{
DataProvider: streamLocator.DataProvider.Address(),
StreamId: streamLocator.StreamId.String(),
EventTime: baseTimestamp + (i * 3600),
Value: float64(i),
},
}
txHash, err := deployedStream.InsertRecords(ctx, records)
assertNoErrorOrFail(t, err, "Failed to insert record")
txHashes = append(txHashes, txHash)
}
insertionDuration := time.Since(startTime)
fmt.Printf("[Single Records] All insertions completed in %v (avg %v per record)\n",
insertionDuration,
insertionDuration/time.Duration(numRecords))
// Wait for all transactions after sending them all
waitStart := time.Now()
for _, txHash := range txHashes {
waitTxToBeMinedWithSuccess(t, ctx, tnClient, txHash)
}
waitDuration := time.Since(waitStart)
fmt.Printf("[Single Records] All transactions confirmed in %v\n", waitDuration)
// Verify all records were inserted
dateFrom := baseTimestamp
dateTo := baseTimestamp + (numRecords * 3600)
records, err := deployedStream.GetRecord(ctx, types.GetRecordInput{
DataProvider: streamLocator.DataProvider.Address(),
StreamId: streamLocator.StreamId.String(),
From: &dateFrom,
To: &dateTo,
})
assertNoErrorOrFail(t, err, "Failed to query records")
assert.Equal(t, numRecords, len(records), "Unexpected number of records")
})
}