Debian Codegraph @

test/util/loadgen/loadgen.go

raw
1 //go:build !coverage
2 // +build !coverage
3
4 // SMTP load generator, for testing purposes.
5 package main
6
7 import (
8 "flag"
9 "net"
10 "net/http"
11 "net/textproto"
12 "runtime"
13 "sync"
14 "time"
15
16 _ "net/http/pprof"
17
18 "blitiri.com.ar/go/chasquid/internal/nettrace"
19 "blitiri.com.ar/go/chasquid/internal/smtp"
20 "blitiri.com.ar/go/log"
21 )
22
23 var (
24 addr = flag.String("addr", "",
25 "server address")
26 httpAddr = flag.String("http_addr", "localhost:8011",
27 "monitoring HTTP server listening address")
28 parallel = flag.Int("parallel", 0,
29 "how many sending loops to run in parallel")
30 runFor = flag.Duration("run_for", 0,
31 "how long to run for (0 = forever)")
32 wait = flag.Bool("wait", false,
33 "don't exit after --run_for has lapsed")
34 noop = flag.Bool("noop", false,
35 "don't send an email, just connect and run a NOOP")
36 )
37
38 var (
39 host string
40 exit bool
41
42 globalCount int64 = 0
43 globalRuntime time.Duration
44 globalMu = &sync.Mutex{}
45 )
46
47 func main() {
48 var err error
49
50 flag.Parse()
51 log.Init()
52
53 host, _, err = net.SplitHostPort(*addr)
54 if err != nil {
55 log.Fatalf("failed to split --addr=%q: %v", *addr, err)
56 }
57
58 if *wait {
59 go http.ListenAndServe(*httpAddr, nil)
60 log.Infof("monitoring address: http://%v/debug/requests?fam=one&b=11",
61 *httpAddr)
62 }
63
64 if *parallel == 0 {
65 *parallel = runtime.GOMAXPROCS(0)
66 }
67
68 lt := "full"
69 if *noop {
70 lt = "noop"
71 }
72
73 log.Infof("launching %d %s sending loops in parallel", *parallel, lt)
74 for i := 0; i < *parallel; i++ {
75 go serial(i)
76 }
77
78 var totalCount int64
79 var totalRuntime time.Duration
80 start := time.Now()
81 for range time.Tick(1 * time.Second) {
82 globalMu.Lock()
83 totalCount += globalCount
84 totalRuntime += globalRuntime
85 count := globalCount
86 runtime := globalRuntime
87 globalCount = 0
88 globalRuntime = 0
89 globalMu.Unlock()
90
91 if count == 0 {
92 log.Infof("0 ops")
93 } else {
94 log.Infof("%d ops, %v /op", count,
95 time.Duration(runtime.Nanoseconds()/count).Truncate(time.Microsecond))
96 }
97
98 if *runFor > 0 && time.Since(start) > *runFor {
99 exit = true
100 break
101 }
102 }
103
104 end := time.Now()
105 window := end.Sub(start)
106 log.Infof("total: %d ops, %v wall, %v run",
107 totalCount,
108 window.Truncate(time.Millisecond),
109 totalRuntime.Truncate(time.Millisecond))
110
111 avgLat := time.Duration(totalRuntime.Nanoseconds() / totalCount)
112 log.Infof("avg: %v /op, %.0f ops/s",
113 avgLat.Truncate(time.Microsecond),
114 float64(totalCount)/window.Seconds(),
115 )
116
117 if *wait {
118 for {
119 time.Sleep(24 * time.Hour)
120 }
121 }
122 }
123
124 func serial(id int) {
125 var count int64
126 start := time.Now()
127 for {
128 count++
129 err := one()
130 if err != nil {
131 log.Fatalf("%v", err)
132 }
133
134 if count == 5 {
135 globalMu.Lock()
136 globalCount += count
137 globalRuntime += time.Since(start)
138 globalMu.Unlock()
139 count = 0
140 start = time.Now()
141
142 if exit {
143 return
144 }
145 }
146 }
147 }
148
149 func one() error {
150 tr := nettrace.New("one", *addr)
151 defer tr.Finish()
152
153 conn, err := net.Dial("tcp", *addr)
154 if err != nil {
155 return err
156 }
157 defer conn.Close()
158
159 client, err := smtp.NewClient(conn, host)
160 if err != nil {
161 return err
162 }
163 defer client.Close()
164
165 if *noop {
166 err = client.Noop()
167 if err != nil {
168 return err
169 }
170 } else {
171 err = client.MailAndRcpt("test@test", "null@testserver")
172 if err != nil {
173 return err
174 }
175
176 retry:
177 w, err := client.Data()
178 if err != nil {
179 return err
180 }
181 _, err = w.Write(body)
182 if err != nil {
183 return err
184 }
185 err = w.Close()
186 if err != nil {
187 // If we are sending too fast we might hit chasquid's queue size
188 // limit. In that case, wait and try again.
189 // We detect it with error code 451 which is used for this
190 // situation.
191 if terr, ok := err.(*textproto.Error); ok {
192 if terr.Code == 451 {
193 time.Sleep(10 * time.Millisecond)
194 goto retry
195 }
196 }
197 return err
198 }
199 }
200
201 return nil
202 }
203
204 var body = []byte(`Subject: Load test
205
206 This is the body of the load test email.
207 `)