/
main.go
126 lines (107 loc) · 2.75 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"os"
"reflect"
"time"
"github.com/philpearl/bqupload/client"
"github.com/philpearl/bqupload/protocol"
"github.com/philpearl/plenc"
plencnull "github.com/philpearl/plenc/null"
"github.com/philpearl/plenc/plenccodec"
"github.com/unravelin/null"
"golang.org/x/sync/errgroup"
)
type opt struct {
projectID string
dataSetID string
tableName string
addr string
msgCount int
goRoutines int
}
func (o *opt) registerFlags() {
flag.StringVar(&o.projectID, "project", "", "project ID")
flag.StringVar(&o.dataSetID, "dataset", "", "dataset ID")
flag.StringVar(&o.tableName, "table", "", "table ID")
flag.StringVar(&o.addr, "addr", "localhost:8123", "address to connect to")
flag.IntVar(&o.msgCount, "msg-count", 10, "number of messages to send")
flag.IntVar(&o.goRoutines, "go-routines", 1, "number of go routines to use")
}
func (o *opt) validate() error {
if o.projectID == "" {
return fmt.Errorf("project is required")
}
if o.dataSetID == "" {
return fmt.Errorf("dataset is required")
}
if o.tableName == "" {
return fmt.Errorf("table is required")
}
return nil
}
func main() {
var o opt
o.registerFlags()
flag.Parse()
if err := o.run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}
type TestMessage struct {
Name string `json:"name,omitempty" plenc:"1"`
Age null.Int `json:"age,omitempty" plenc:"2"`
IsARabbit bool `json:"isARabbit,omitempty" plenc:"3"`
Time time.Time `json:"time,omitempty" plenc:"4"`
}
func (o *opt) run() error {
if err := o.validate(); err != nil {
return fmt.Errorf("validating options: %w", err)
}
// Set up the plenc codec
var pl plenc.Plenc
pl.ProtoCompatibleArrays = true
pl.RegisterDefaultCodecs()
plencnull.AddCodecs(&pl)
pl.RegisterCodec(reflect.TypeOf(time.Time{}), plenccodec.BQTimestampCodec{})
codec, err := pl.CodecForType(reflect.TypeOf(TestMessage{}))
if err != nil {
return fmt.Errorf("getting codec: %w", err)
}
desc := protocol.ConnectionDescriptor{
ProjectID: o.projectID,
DataSetID: o.dataSetID,
TableName: o.tableName,
Descriptor: codec.Descriptor(),
}
cli, err := client.New(o.addr, &pl, &desc)
if err != nil {
return fmt.Errorf("creating client: %w", err)
}
msg := TestMessage{
Name: "Phil",
Age: null.I(53),
IsARabbit: false,
Time: time.Now(),
}
ctx := context.Background()
var eg errgroup.Group
start := time.Now()
defer func() {
fmt.Printf("Time taken: %s\n", time.Since(start))
}()
for i := 0; i < o.goRoutines; i++ {
eg.Go(func() error {
for i := 0; i < o.msgCount; i++ {
if err := cli.Publish(ctx, msg); err != nil {
return fmt.Errorf("publishing (%d): %w", i, err)
}
}
return nil
})
}
return eg.Wait()
}