Debian Codegraph @

tools/loadgen/main.go

raw
1 package main
2
3 import (
4 "bufio"
5 "context"
6 "fmt"
7 "net/http"
8 "os"
9 "time"
10 )
11
12 func main() {
13 baseURL := "https://staging.ntfy.sh"
14 if len(os.Args) > 1 {
15 baseURL = os.Args[1]
16 }
17 for i := 0; i < 2000; i++ {
18 go subscribe(i, baseURL)
19 }
20 time.Sleep(5 * time.Second)
21 for i := 0; i < 2000; i++ {
22 go func(worker int) {
23 for {
24 poll(worker, baseURL)
25 }
26 }(i)
27 }
28 time.Sleep(time.Hour)
29 }
30
31 func subscribe(worker int, baseURL string) {
32 fmt.Printf("[subscribe] worker=%d STARTING\n", worker)
33 start := time.Now()
34 topic, ip := fmt.Sprintf("subtopic%d", worker), fmt.Sprintf("1.2.%d.%d", (worker/255)%255, worker%255)
35 req, _ := http.NewRequest("GET", fmt.Sprintf("%s/%s/json", baseURL, topic), nil)
36 req.Header.Set("X-Forwarded-For", ip)
37 resp, err := http.DefaultClient.Do(req)
38 if err != nil {
39 fmt.Printf("[subscribe] worker=%d time=%d error=%s\n", worker, time.Since(start).Milliseconds(), err.Error())
40 return
41 }
42 defer resp.Body.Close()
43 scanner := bufio.NewScanner(resp.Body)
44 for scanner.Scan() {
45 // Do nothing
46 }
47 fmt.Printf("[subscribe] worker=%d status=%d time=%d EXITED\n", worker, resp.StatusCode, time.Since(start).Milliseconds())
48 }
49
50 func poll(worker int, baseURL string) {
51 fmt.Printf("[poll] worker=%d STARTING\n", worker)
52 topic, ip := fmt.Sprintf("polltopic%d", worker), fmt.Sprintf("1.2.%d.%d", (worker/255)%255, worker%255)
53 start := time.Now()
54 ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
55 defer cancel()
56
57 //req, _ := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://staging.ntfy.sh/%s/json?poll=1&since=all", topic), nil)
58 req, _ := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/json?poll=1&since=all", baseURL, topic), nil)
59 req.Header.Set("X-Forwarded-For", ip)
60
61 resp, err := http.DefaultClient.Do(req)
62 if err != nil {
63 fmt.Printf("[poll] worker=%d time=%d status=- error=%s\n", worker, time.Since(start).Milliseconds(), err.Error())
64 cancel()
65 return
66 }
67 defer resp.Body.Close()
68 fmt.Printf("[poll] worker=%d time=%d status=%s\n", worker, time.Since(start).Milliseconds(), resp.Status)
69 }