Debian Codegraph @

pubsub/loadtest/benchmark_test.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
16
17 // Performance benchmarks for pubsub.
18 // Run with
19 // go test -bench . -cpu 1
20
21 import (
22 "context"
23 "log"
24 "sync"
25 "sync/atomic"
26 "testing"
27 "time"
28
29 "cloud.google.com/go/internal/testutil"
30 "cloud.google.com/go/pubsub"
31 pb "cloud.google.com/go/pubsub/apiv1/pubsubpb"
32 "google.golang.org/api/option"
33 gtransport "google.golang.org/api/transport/grpc"
34 "google.golang.org/grpc"
35 )
36
37 // These constants are designed to match the "throughput" test in
38 // https://github.com/GoogleCloudPlatform/pubsub/blob/master/load-test-framework/run.py
39 // and
40 // https://github.com/GoogleCloudPlatform/pubsub/blob/master/load-test-framework/src/main/java/com/google/pubsub/clients/experimental/CPSPublisherTask.java
41
42 const (
43 nMessages = 1e5
44 messageSize = 10000 // size of msg data in bytes
45 batchSize = 10
46 batchDuration = 50 * time.Millisecond
47 serverDelay = 200 * time.Millisecond
48 maxOutstandingPublishes = 1600 // max_outstanding_messages in run.py
49 useOrdered = true
50 )
51
52 func BenchmarkPublishThroughput(b *testing.B) {
53 b.SetBytes(nMessages * messageSize)
54 client := perfClient(serverDelay, 1, b)
55
56 lts := &PubServer{ID: "xxx"}
57 lts.init(client, "t", messageSize, batchSize, batchDuration, useOrdered)
58
59 b.ResetTimer()
60 for i := 0; i < b.N; i++ {
61 runOnce(lts)
62 }
63 }
64
65 func runOnce(lts *PubServer) {
66 nRequests := int64(nMessages / batchSize)
67 var nPublished int64
68 var wg sync.WaitGroup
69 // The Java loadtest framework is rate-limited to 1 billion Execute calls a
70 // second (each Execute call corresponding to a publishBatch call here),
71 // but we can ignore this because of the following.
72 // The framework runs 10,000 threads, each calling Execute in a loop, but
73 // we can ignore this too.
74 // The framework caps the number of outstanding calls to Execute at
75 // maxOutstandingPublishes. That is what we simulate here.
76 for i := 0; i < maxOutstandingPublishes; i++ {
77 wg.Add(1)
78 go func() {
79 defer wg.Done()
80 for atomic.AddInt64(&nRequests, -1) >= 0 {
81 latencies, err := lts.publishBatch()
82 if err != nil {
83 log.Fatalf("publishBatch: %v", err)
84 }
85 atomic.AddInt64(&nPublished, int64(len(latencies)))
86 }
87 }()
88 }
89 wg.Wait()
90 sent := atomic.LoadInt64(&nPublished)
91 if sent != nMessages {
92 log.Fatalf("sent %d messages, expected %d", sent, int(nMessages))
93 }
94 }
95
96 func perfClient(pubDelay time.Duration, nConns int, f interface {
97 Fatal(...interface{})
98 }) *pubsub.Client {
99 ctx := context.Background()
100 srv, err := newPerfServer(pubDelay)
101 if err != nil {
102 f.Fatal(err)
103 }
104 conn, err := gtransport.DialInsecure(ctx,
105 option.WithEndpoint(srv.Addr),
106 option.WithGRPCConnectionPool(nConns),
107
108 // TODO(grpc/grpc-go#1388) using connection pool without WithBlock
109 // can cause RPCs to fail randomly. We can delete this after the issue is fixed.
110 option.WithGRPCDialOption(grpc.WithBlock()))
111 if err != nil {
112 f.Fatal(err)
113 }
114 client, err := pubsub.NewClient(ctx, "projectID", option.WithGRPCConn(conn))
115 if err != nil {
116 f.Fatal(err)
117 }
118 return client
119 }
120
121 type perfServer struct {
122 pb.PublisherServer
123 pb.SubscriberServer
124
125 Addr string
126 pubDelay time.Duration
127
128 mu sync.Mutex
129 activePubs int
130 maxActivePubs int
131 }
132
133 func newPerfServer(pubDelay time.Duration) (*perfServer, error) {
134 srv, err := testutil.NewServer(grpc.MaxMsgSize(pubsub.MaxPublishRequestBytes))
135 if err != nil {
136 return nil, err
137 }
138 perf := &perfServer{Addr: srv.Addr, pubDelay: pubDelay}
139 pb.RegisterPublisherServer(srv.Gsrv, perf)
140 pb.RegisterSubscriberServer(srv.Gsrv, perf)
141 srv.Start()
142 return perf, nil
143 }
144
145 var doLog = false
146
147 func (p *perfServer) incActivePubs(n int) (int, bool) {
148 p.mu.Lock()
149 defer p.mu.Unlock()
150 p.activePubs += n
151 newMax := false
152 if p.activePubs > p.maxActivePubs {
153 p.maxActivePubs = p.activePubs
154 newMax = true
155 }
156 return p.activePubs, newMax
157 }
158
159 func (p *perfServer) Publish(ctx context.Context, req *pb.PublishRequest) (*pb.PublishResponse, error) {
160 a, newMax := p.incActivePubs(1)
161 defer p.incActivePubs(-1)
162 if newMax && doLog {
163 log.Printf("max %d active publish calls", a)
164 }
165 if doLog {
166 log.Printf("%p -> Publish %d", p, len(req.Messages))
167 }
168 res := &pb.PublishResponse{MessageIds: make([]string, len(req.Messages))}
169 for i := range res.MessageIds {
170 res.MessageIds[i] = "x"
171 }
172 time.Sleep(p.pubDelay)
173 if doLog {
174 log.Printf("%p <- Publish %d", p, len(req.Messages))
175 }
176 return res, nil
177 }