-
Notifications
You must be signed in to change notification settings - Fork 0
/
testperf.go
130 lines (111 loc) · 3 KB
/
testperf.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
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
package main
import (
"context"
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"time"
"cloud.google.com/go/firestore"
"go.mongodb.org/mongo-driver/x/mongo/driver/uuid"
"google.golang.org/api/iterator"
)
func ResetEmulator(ctx context.Context, project string) error {
fsHost := os.Getenv("FIRESTORE_EMULATOR_HOST")
fmt.Printf("Reset emulator:%s\n", fsHost)
if fsHost == "" {
return errors.New("FIRESTORE_EMULATOR_HOST is not set")
}
client := &http.Client{}
r, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("http://%s/emulator/v1/projects/%s/databases/(default)/documents", fsHost, project), nil)
if err != nil {
return err
}
_, err = client.Do(r)
return err
}
func createClient(ctx context.Context) *firestore.Client {
projectID := "zing-dev-adjunct"
// err := ResetEmulator(ctx, projectID)
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
return client
}
func addEventsToCollection(ctx context.Context, DocCount int, col *firestore.CollectionRef) (err error) {
baseTime := time.Now().Add(-7 * time.Hour * 24)
for i := 0; i < DocCount; i++ {
uuid_1, _ := uuid.New()
uuid_2, _ := uuid.New()
_, _, err = col.Add(ctx, map[string]interface{}{
"_id": fmt.Sprintf("%X", uuid_1),
"EventID": fmt.Sprintf("%X", uuid_2),
"TenantID": rand.Intn(5),
"timestamp": baseTime.UnixNano() / 1e6,
})
if err != nil {
fmt.Println("Failed to add")
}
baseTime = baseTime.Add(10 * time.Second)
}
fmt.Printf("Inserted %d documents\n", DocCount)
return err
}
func main() {
//var DocCount = 10000
var tenant = "smoke-tenant"
var ColName = fmt.Sprintf("EventContextTenants/%s/Events", tenant)
// Get a Firestore client.
ctx := context.Background()
client := createClient(ctx)
defer client.Close()
log.Println("Created client")
/*
collections, err := client.Collections(ctx).GetAll()
if err != nil {
log.Printf("%v\n", err)
log.Fatal("Something wrong with the client")
}
for _, c := range collections {
log.Printf("collection: %s", c.Path)
}
*/
col := client.Collection(ColName)
if col == nil {
log.Fatalf("Could not get collection [%s]", ColName)
}
log.Printf("Found Collection %s", col.Path)
/*
start1 := time.Now()
_, _ = col.Select("id").OrderBy("createdAt", firestore.Desc).Documents(ctx).GetAll()
end1 := time.Now()
secs1 := end1.Sub(start1).Seconds()
fmt.Printf("Get All documents in %v seconds.\n", secs1)
*/
start := time.Now()
q := col.Select("id").OrderBy("createdAt", firestore.Desc)
docIter := q.Documents(ctx)
totalDocs := 0
defer docIter.Stop()
for {
doc, done := docIter.Next()
if done == iterator.Done {
break
}
if done != nil {
log.Fatalf("Error processing data: %v", done)
}
m := doc.Data()
totalDocs++
_ = m
// fmt.Printf("id: %v\n", m["id"])
}
end := time.Now()
secs := end.Sub(start).Seconds()
fmt.Printf("Query completed in %v seconds.\n", secs)
log.Printf("Found %d documents", totalDocs)
fmt.Println("DONE")
}