|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | "fmt" |
6 | | - "net/http" |
7 | | - "runtime" |
8 | 5 | "time" |
9 | 6 |
|
10 | | - "connectrpc.com/connect" |
11 | 7 | "github.com/codeharik/k7" |
12 | | - "github.com/codeharik/k7/example/api" |
13 | | - "github.com/codeharik/k7/example/api/apiconnect" |
14 | | - "golang.org/x/net/http2" |
15 | | - "golang.org/x/net/http2/h2c" |
| 8 | + "github.com/go-resty/resty/v2" |
16 | 9 | ) |
17 | 10 |
|
18 | | -// curl \ |
19 | | -// --header "Content-Type: application/json" \ |
20 | | -// --data '{"name": "Jane"}' \ |
21 | | -// http://localhost:8080/greet.GreetService/Greet |
22 | | - |
23 | | -var COUNTER = 0 |
24 | | - |
25 | | -type GreetServer struct{} |
26 | | - |
27 | | -func (s *GreetServer) Greet( |
28 | | - ctx context.Context, |
29 | | - req *connect.Request[api.GreetRequest], |
30 | | -) (*connect.Response[api.GreetResponse], error) { |
31 | | - res := connect.NewResponse(&api.GreetResponse{ |
32 | | - Greeting: fmt.Sprintf("Hello, %s!", req.Msg.Name), |
33 | | - }) |
34 | | - res.Header().Set("Greet-Version", "v1") |
35 | | - |
36 | | - COUNTER++ |
| 11 | +var ( |
| 12 | + restyClients = []resty.Client{} |
| 13 | + restyClientId = 0 |
| 14 | +) |
37 | 15 |
|
38 | | - return res, nil |
| 16 | +func init() { |
| 17 | + for i := 0; i < 3; i++ { |
| 18 | + restyClients = append(restyClients, *resty.New()) |
| 19 | + } |
39 | 20 | } |
40 | 21 |
|
41 | | -func main() { |
42 | | - runtime.GOMAXPROCS(8) |
43 | | - |
44 | | - greeter := &GreetServer{} |
45 | | - mux := http.NewServeMux() |
46 | | - path, handler := apiconnect.NewGreetServiceHandler(greeter) |
47 | | - mux.Handle(path, handler) |
48 | | - |
49 | | - go func() { |
50 | | - http.ListenAndServe( |
51 | | - "localhost:8080", |
52 | | - h2c.NewHandler(mux, &http2.Server{}), |
53 | | - ) |
54 | | - }() |
55 | | - |
56 | | - ticker := time.NewTicker(1 * time.Second) |
57 | | - |
58 | | - go func() { |
59 | | - for { |
60 | | - select { |
61 | | - case <-ticker.C: |
62 | | - fmt.Println(COUNTER) |
63 | | - } |
64 | | - } |
65 | | - }() |
66 | | - |
| 22 | +func restyAttack() { |
67 | 23 | config := k7.BenchmarkConfig{ |
68 | | - Concurrency: 2, // Number of concurrent users |
69 | | - Duration: 10 * time.Second, // Run test for 10 seconds |
| 24 | + Threads: 3, |
| 25 | + Duration: 10 * time.Second, |
70 | 26 | AttackFunc: func() bool { |
71 | | - client := apiconnect.NewGreetServiceClient( |
72 | | - http.DefaultClient, |
73 | | - "http://localhost:8080", |
74 | | - connect.WithGRPC(), |
75 | | - ) |
76 | | - _, err := client.Greet( |
77 | | - context.Background(), |
78 | | - connect.NewRequest(&api.GreetRequest{Name: "Jane"}), |
79 | | - ) |
| 27 | + res, err := restyClients[restyClientId].R().Get("http://localhost:8080") |
80 | 28 | if err != nil { |
81 | 29 | fmt.Println(err) |
82 | 30 | return false |
83 | 31 | } |
84 | 32 |
|
85 | | - return true |
| 33 | + restyClientId = (restyClientId + 1) % len(restyClients) |
| 34 | + |
| 35 | + return res.StatusCode() == 200 |
86 | 36 | }, |
87 | 37 | } |
88 | | - |
89 | 38 | config.Attack() |
90 | 39 | } |
91 | 40 |
|
92 | | -// func main() { |
93 | | -// runtime.GOMAXPROCS(8) |
94 | | - |
95 | | -// go func() { |
96 | | -// server := http.ServeMux{} |
97 | | - |
98 | | -// // Set up handlers |
99 | | -// server.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
100 | | -// COUNTER++ |
101 | | - |
102 | | -// // if rand.IntN(10) < 5 { |
103 | | -// // http.Error(w, "Random error", http.StatusConflict) |
104 | | -// // return |
105 | | -// // } |
106 | | -// w.WriteHeader(http.StatusOK) |
107 | | -// w.Write([]byte("Hello world!")) |
108 | | -// return |
109 | | -// }) |
110 | | - |
111 | | -// ticker := time.NewTicker(1 * time.Second) |
112 | | - |
113 | | -// go func() { |
114 | | -// for { |
115 | | -// select { |
116 | | -// case <-ticker.C: |
117 | | -// fmt.Println(COUNTER) |
118 | | -// } |
119 | | -// } |
120 | | -// }() |
| 41 | +type Mode int |
121 | 42 |
|
122 | | -// // Start the server |
123 | | -// port := 8080 |
124 | | -// fmt.Printf("Server running on http://localhost:%d\n", port) |
125 | | -// http.ListenAndServe(fmt.Sprintf(":%d", port), &server) |
126 | | -// }() |
127 | | - |
128 | | -// config := k7.BenchmarkConfig{ |
129 | | -// Concurrency: 2, // Number of concurrent users |
130 | | -// Duration: 10 * time.Second, // Run test for 10 seconds |
131 | | -// AttackFunc: func() bool { |
132 | | -// client := &http.Client{} |
| 43 | +const ( |
| 44 | + Connect Mode = iota |
| 45 | + Fiber |
| 46 | + NetHTTP |
| 47 | +) |
133 | 48 |
|
134 | | -// res, err := client.Get("http://localhost:8080") |
135 | | -// if err != nil { |
136 | | -// fmt.Println(err) |
137 | | -// } |
138 | | -// defer res.Body.Close() |
139 | | -// return err == nil && res.StatusCode == 200 |
140 | | -// }, |
141 | | -// } |
| 49 | +func (m Mode) String() string { |
| 50 | + return [...]string{"Connect", "Fiber", "NetHTTP"}[m] |
| 51 | +} |
142 | 52 |
|
143 | | -// config.Attack() |
144 | | -// } |
| 53 | +func main() { |
| 54 | + mode := Fiber |
| 55 | + |
| 56 | + switch mode { |
| 57 | + case Connect: |
| 58 | + resty := false |
| 59 | + fmt.Println("Starting Connect Server, Resty:", resty) |
| 60 | + connectServer(resty) |
| 61 | + case Fiber: |
| 62 | + fmt.Println("Starting Fiber Server") |
| 63 | + fiberServer() |
| 64 | + case NetHTTP: |
| 65 | + fmt.Println("Starting NetHTTP Server") |
| 66 | + nethttpServer() |
| 67 | + default: |
| 68 | + fmt.Println("Invalid mode") |
| 69 | + } |
| 70 | +} |
0 commit comments