-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathe2e_test.go
More file actions
329 lines (302 loc) · 7.63 KB
/
e2e_test.go
File metadata and controls
329 lines (302 loc) · 7.63 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gigapi/gigapi-config/config"
"github.com/gigapi/gigapi/v2/merge"
"github.com/gigapi/gigapi/v2/merge/repository"
utils2 "github.com/gigapi/gigapi/v2/merge/utils"
"github.com/gigapi/gigapi/v2/router"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime/pprof"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
func startCPUProfile(t *testing.T) func() {
cpuFile, err := os.Create("cpu.pprof")
if err != nil {
t.Fatal(err)
}
if err := pprof.StartCPUProfile(cpuFile); err != nil {
t.Fatal(err)
}
return func() {
pprof.StopCPUProfile()
cpuFile.Close()
}
}
const N = 200
const S = 10000
func runServer() {
initModules()
r := router.NewRouter()
fmt.Printf("GigAPI Running: %s:%d\n", config.Config.HTTP.Host, config.Config.HTTP.Port)
if err := http.ListenAndServe(fmt.Sprintf("%s:%d",
config.Config.HTTP.Host, config.Config.HTTP.Port), r); err != nil {
panic(err)
}
}
func testE2EWriting(t *testing.T) {
start := time.Now()
wg := sync.WaitGroup{}
for i := 0; i < N; i++ {
_i := i
wg.Add(1)
go func() {
defer wg.Done()
var data strings.Builder
for j := 0; j < S; j++ {
data.WriteString(fmt.Sprintf("logs,instance=1,level=info msg=\"Hello world %d\"\n", _i))
}
_, err := http.Post("http://localhost:7988/gigapi/insert", "", bytes.NewBuffer(
[]byte(data.String())))
if err != nil {
panic(err)
}
}()
}
wg.Wait()
fmt.Printf("%d rows MB written in %v\n", S*N, time.Since(start))
parquets := 0
filepath.Walk("./_testdata/l1", func(path string, info os.FileInfo, err error) error {
if info == nil {
return nil
}
if strings.HasSuffix(info.Name(), ".parquet") {
parquets++
}
return nil
})
fmt.Printf("Found %d parquets\n", parquets)
fmt.Println("Wating for merge...")
time.Sleep(time.Second * 75)
parquets = 0
filepath.Walk("./_testdata/l1", func(path string, info os.FileInfo, err error) error {
if info == nil {
return nil
}
if strings.HasSuffix(info.Name(), ".parquet") {
parquets++
}
return nil
})
fmt.Printf("Found %d parquets\n", parquets)
}
func testE2EReading(t *testing.T) {
var requests = []string{
`{"query": "SELECT count() as c FROM logs"}`,
`{"query": "SHOW DATABASES"}`,
`{"query": "SHOW TABLES"}`,
}
var responses = []string{
`{"results":[{"c":"2000000"}]}`,
`{"results":[{"database_name":"default"}]}`,
`{"results":[{"table_name":"logs"}]}`,
}
for i, reqBody := range requests {
res, err := http.Post("http://localhost:7988/query?db=default&format=json", "application/json",
bytes.NewBuffer([]byte(reqBody)))
if err != nil {
panic(err)
}
if res.StatusCode/100 != 2 {
body, _ := io.ReadAll(res.Body)
panic(fmt.Sprintf("[%d]: %s", res.StatusCode, string(body)))
}
body, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}
strBody := strings.TrimSpace(string(body))
fmt.Println(strBody)
if strBody != responses[i] {
panic(fmt.Sprintf("Unexpected response: `%s`", strBody))
}
}
}
func TestE2E(t *testing.T) {
// Start CPU profiling
stopCPUProfile := startCPUProfile(t)
defer stopCPUProfile()
defer os.RemoveAll("_testdata/l1")
config.Config = &config.Configuration{
Gigapi: config.GigapiConfiguration{
Root: "_testdata",
MergeTimeoutS: 10,
Mode: "aio",
Metadata: config.MetadataConfiguration{
Type: "json",
},
Layers: []config.LayersConfiguration{
{
Name: "l1",
Type: "fs",
Global: false,
URL: "file://./_testdata/l1",
TTL: 0,
},
},
},
HTTP: config.HTTPConfiguration{
Port: 7988,
Host: "localhost",
BasicAuth: config.BasicAuthConfiguration{},
},
FlightSql: config.FlightSqlConfiguration{Port: 7989},
}
go runServer()
time.Sleep(time.Second)
testE2EWriting(t)
testE2EReading(t)
}
type ParquetData struct {
Type string `json:"type"`
ParquetSizeBytes int64 `json:"parquet_size_bytes"`
RowCount int64 `json:"row_count"`
MinTime int64 `json:"min_time"`
MaxTime int64 `json:"max_time"`
WalSequence int64 `json:"wal_sequence"`
Files []File `json:"files"`
}
type File struct {
ID int `json:"id"`
Path string `json:"path"`
SizeBytes int64 `json:"size_bytes"`
RowCount int64 `json:"row_count"`
ChunkTime int64 `json:"chunk_time"`
MinTime int64 `json:"min_time"`
MaxTime int64 `json:"max_time"`
Range string `json:"range"`
Type string `json:"type"`
}
func ConsistencyTestIter(t *testing.T, count *int64) error {
url := "http://localhost:7971/gigapi/write"
data := `weather,location=us-midwest,season=summer temperature=82 1748253664000000000
weather,location=us-midwest,season=summer temperature=83
weather,location=us-midwest,season2=summer2 temperature=84
weather,location=us-midwest,season2=summer2 temperature=84`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(data))
if err != nil {
t.Fatal("error creating request: ", err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("error sending request: ", err)
return nil
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("error reading response: ", err)
return nil
}
*count += 4
fmt.Printf("Total rows: %d\n", *count)
return nil
}
func TestConsistency(t *testing.T) {
if os.Getenv("INTERNAL_TEST") != "1" {
return
}
count := int64(0)
nowNs := time.Now().UnixNano()
tillNextSec := 1000000000 - nowNs%1000000000
time.Sleep(time.Nanosecond * time.Duration(tillNextSec))
tck := time.NewTicker(time.Second)
for range tck.C {
ConsistencyTestIter(t, &count)
}
}
func TestMetadataFiles(t *testing.T) {
if os.Getenv("INTERNAL_TEST") != "1" {
return
}
// Start CPU profiling
stopCPUProfile := startCPUProfile(t)
defer stopCPUProfile()
config.Config = &config.Configuration{
Gigapi: config.GigapiConfiguration{
Root: "_testdata",
MergeTimeoutS: 10,
},
}
merge.Init(&api{})
var data = map[string]any{
"timestamp": []int64{},
"value": []float64{},
"str": []string{},
}
for i := 0; i < 5; i++ {
data["timestamp"] = append(data["timestamp"].([]int64), int64(time.Now().UnixNano()))
data["value"] = append(data["value"].([]float64), float64(i)/100.0)
str := fmt.Sprintf("str%d", i)
data["str"] = append(data["str"].([]string), str)
}
db, cancel, err := utils2.ConnectDuckDB("")
if err != nil {
panic(err)
}
defer cancel()
var expectedSize int32 = 0
checkMetadata := func() int64 {
f, err := os.Open(
fmt.Sprintf("_testdata/default/test/date=%s/hour=%02d/metadata.json",
time.Now().UTC().Format("2006-01-02"), time.Now().UTC().Hour()))
if err != nil {
panic(err)
}
defer f.Close()
data, err := io.ReadAll(f)
if err != nil {
panic(err)
}
pData := ParquetData{}
json.Unmarshal(data, &pData)
var pqts []string
for _, f := range pData.Files {
pqts = append(pqts, "'"+f.Path+"'")
}
rows, err := db.Query(fmt.Sprintf("SELECT COUNT(*) FROM read_parquet([%s])", strings.Join(pqts, ",")))
if err != nil {
panic(err)
}
defer rows.Close()
var count int64
for rows.Next() {
err := rows.Scan(&count)
if err != nil {
panic(err)
}
}
return count
}
go func() {
time.Sleep(time.Second * 5)
for {
mdSize := checkMetadata()
fmt.Printf("Metadata count: %d - %d\n", mdSize, expectedSize)
if mdSize != int64(expectedSize) {
println("UNEXPECTED metadata count")
}
time.Sleep(time.Second * 1)
}
}()
for i := 0; i < 100; i++ {
promise := repository.Store("", "test", data)
_, err := promise.Get()
if err != nil {
panic(err)
}
atomic.AddInt32(&expectedSize, 5)
time.Sleep(time.Second)
}
}