Debian Codegraph @

pubsub/loadtest/loadtest.go

raw
1 // Copyright 2017 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package loadtest implements load testing for pubsub,
16 // following the interface defined in https://github.com/GoogleCloudPlatform/pubsub/tree/master/load-test-framework/ .
17 //
18 // This package is experimental.
19 package loadtest
20
21 import (
22 "bytes"
23 "context"
24 "errors"
25 "fmt"
26 "log"
27 "runtime"
28 "strconv"
29 "sync"
30 "sync/atomic"
31 "time"
32
33 "cloud.google.com/go/pubsub"
34 pb "cloud.google.com/go/pubsub/loadtest/pb"
35 "golang.org/x/time/rate"
36 )
37
38 type pubServerConfig struct {
39 topic *pubsub.Topic
40 msgData []byte
41 batchSize int32
42 ordered bool
43 }
44
45 // PubServer is a dummy Pub/Sub server for load testing.
46 type PubServer struct {
47 ID string
48
49 cfg atomic.Value
50 seqNum int32
51 pb.UnimplementedLoadtestWorkerServer
52 }
53
54 // Start starts the server.
55 func (l *PubServer) Start(ctx context.Context, req *pb.StartRequest) (*pb.StartResponse, error) {
56 log.Println("received start")
57 c, err := pubsub.NewClient(ctx, req.Project)
58 if err != nil {
59 return nil, err
60 }
61 l.init(c, req.Topic, req.GetPublisherOptions().GetMessageSize(), req.GetPublisherOptions().GetBatchSize(), req.GetPublisherOptions().GetBatchDuration().AsDuration(), false)
62 log.Println("started")
63 return &pb.StartResponse{}, nil
64 }
65
66 func (l *PubServer) init(c *pubsub.Client, topicName string, msgSize, batchSize int32, batchDur time.Duration, ordered bool) {
67 topic := c.Topic(topicName)
68 topic.PublishSettings = pubsub.PublishSettings{
69 DelayThreshold: batchDur,
70 CountThreshold: 950,
71 ByteThreshold: 9500000,
72 }
73 topic.EnableMessageOrdering = ordered
74
75 l.cfg.Store(pubServerConfig{
76 topic: topic,
77 msgData: bytes.Repeat([]byte{'A'}, int(msgSize)),
78 batchSize: batchSize,
79 ordered: ordered,
80 })
81 }
82
83 func (l *PubServer) publishBatch() ([]int64, error) {
84 var cfg pubServerConfig
85 if c, ok := l.cfg.Load().(pubServerConfig); ok {
86 cfg = c
87 } else {
88 return nil, errors.New("config not loaded")
89 }
90
91 start := time.Now()
92 latencies := make([]int64, cfg.batchSize)
93 startStr := strconv.FormatInt(start.UnixNano()/1e6, 10)
94 seqNum := atomic.AddInt32(&l.seqNum, cfg.batchSize) - cfg.batchSize
95
96 rs := make([]*pubsub.PublishResult, cfg.batchSize)
97 for i := int32(0); i < cfg.batchSize; i++ {
98 msg := &pubsub.Message{
99 Data: cfg.msgData,
100 Attributes: map[string]string{
101 "sendTime": startStr,
102 "clientId": l.ID,
103 "sequenceNumber": strconv.Itoa(int(seqNum + i)),
104 },
105 }
106 if cfg.ordered {
107 msg.OrderingKey = fmt.Sprintf("key-%d", seqNum+i)
108 }
109 rs[i] = cfg.topic.Publish(context.TODO(), msg)
110 }
111 for i, r := range rs {
112 _, err := r.Get(context.Background())
113 if err != nil {
114 return nil, err
115 }
116 // TODO(jba,pongad): fix latencies
117 // Later values will be skewed by earlier ones, since we wait for the
118 // results in order. (On the other hand, it may not matter much, since
119 // messages are added to bundles in order and bundles get sent more or
120 // less in order.) If we want more accurate values, we can either start
121 // a goroutine for each result (similar to the original code using a
122 // callback), or call reflect.Select with the Ready channels of the
123 // results.
124 latencies[i] = time.Since(start).Nanoseconds() / 1e6
125 }
126 return latencies, nil
127 }
128
129 // SubServer is a dummy Pub/Sub server for load testing.
130 type SubServer struct {
131 // TODO(deklerk): what is this actually for?
132 lim *rate.Limiter
133
134 mu sync.Mutex
135 idents []*pb.MessageIdentifier
136 latencies []int64
137 pb.UnimplementedLoadtestWorkerServer
138 }
139
140 // Start starts the server.
141 func (s *SubServer) Start(ctx context.Context, req *pb.StartRequest) (*pb.StartResponse, error) {
142 log.Println("received start")
143 s.lim = rate.NewLimiter(rate.Every(time.Second), 1)
144
145 c, err := pubsub.NewClient(ctx, req.Project)
146 if err != nil {
147 return nil, err
148 }
149
150 // Load test API doesn't define any way to stop right now.
151 go func() {
152 sub := c.Subscription(req.GetPubsubOptions().Subscription)
153 sub.ReceiveSettings.NumGoroutines = 10 * runtime.GOMAXPROCS(0)
154 err := sub.Receive(context.Background(), s.callback)
155 log.Fatal(err)
156 }()
157
158 log.Println("started")
159 return &pb.StartResponse{}, nil
160 }
161
162 func (s *SubServer) callback(_ context.Context, m *pubsub.Message) {
163 id, err := strconv.ParseInt(m.Attributes["clientId"], 10, 64)
164 if err != nil {
165 log.Println(err)
166 m.Nack()
167 return
168 }
169
170 seqNum, err := strconv.ParseInt(m.Attributes["sequenceNumber"], 10, 32)
171 if err != nil {
172 log.Println(err)
173 m.Nack()
174 return
175 }
176
177 sendTimeMillis, err := strconv.ParseInt(m.Attributes["sendTime"], 10, 64)
178 if err != nil {
179 log.Println(err)
180 m.Nack()
181 return
182 }
183
184 latency := time.Now().UnixNano()/1e6 - sendTimeMillis
185 ident := &pb.MessageIdentifier{
186 PublisherClientId: id,
187 SequenceNumber: int32(seqNum),
188 }
189
190 s.mu.Lock()
191 s.idents = append(s.idents, ident)
192 s.latencies = append(s.latencies, latency)
193 s.mu.Unlock()
194 m.Ack()
195 }