Skip to content

Commit ba651c2

Browse files
committed
k7
1 parent 2245aa4 commit ba651c2

File tree

13 files changed

+512
-445
lines changed

13 files changed

+512
-445
lines changed

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ golang 1.24.0
22
zig 0.13.0
33
bun 1.2.2
44
buf 1.50.0
5+
wrk 4.2.0

go.work.sum

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,25 @@ github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkC
5454
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
5555
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
5656
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
57-
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
5857
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
5958
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
59+
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
6060
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
6161
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
62+
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
63+
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
6264
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
65+
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
66+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
6367
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
68+
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
6469
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
6570
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
71+
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
72+
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
6673
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
6774
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
75+
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
6876
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
6977
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
7078
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

k7/example/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ gen:
22
buf dep update
33
buf format -w
44
buf generate
5+
6+
run:
7+
go run example/*.go

k7/example/connect.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"time"
8+
9+
"connectrpc.com/connect"
10+
"github.com/codeharik/k7"
11+
"github.com/codeharik/k7/example/api"
12+
"github.com/codeharik/k7/example/api/apiconnect"
13+
"golang.org/x/net/http2"
14+
"golang.org/x/net/http2/h2c"
15+
)
16+
17+
type GreetServer struct{}
18+
19+
func (s *GreetServer) Greet(
20+
ctx context.Context,
21+
req *connect.Request[api.GreetRequest],
22+
) (*connect.Response[api.GreetResponse], error) {
23+
res := connect.NewResponse(&api.GreetResponse{
24+
Greeting: fmt.Sprintf("Hello, %s!", req.Msg.Name),
25+
})
26+
res.Header().Set("Greet-Version", "v1")
27+
28+
return res, nil
29+
}
30+
31+
func connectServer(resty bool) {
32+
greeter := &GreetServer{}
33+
mux := http.NewServeMux()
34+
path, handler := apiconnect.NewGreetServiceHandler(greeter)
35+
mux.Handle(path, handler)
36+
37+
go func() {
38+
http.ListenAndServe(
39+
"localhost:8080",
40+
h2c.NewHandler(mux,
41+
&http2.Server{},
42+
),
43+
)
44+
}()
45+
46+
if resty {
47+
restyAttack()
48+
} else {
49+
clients := []apiconnect.GreetServiceClient{}
50+
for i := 0; i < 3; i++ {
51+
clients = append(clients, apiconnect.NewGreetServiceClient(
52+
&http.Client{Timeout: time.Millisecond * 100},
53+
"http://localhost:8080",
54+
// connect.WithGRPCWeb(),
55+
// connect.WithGRPC(),
56+
// connect.WithHTTPGet(),
57+
))
58+
}
59+
cc := 0
60+
config := k7.BenchmarkConfig{
61+
Threads: 3,
62+
Duration: 10 * time.Second,
63+
AttackFunc: func() bool {
64+
_, err := clients[cc].Greet(
65+
context.Background(),
66+
connect.NewRequest(&api.GreetRequest{Name: "Jane"}),
67+
)
68+
if err != nil {
69+
fmt.Println(err)
70+
return false
71+
}
72+
73+
cc = (cc + 1) % len(clients)
74+
75+
return true
76+
},
77+
}
78+
config.Attack()
79+
}
80+
}

k7/example/fiber.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strconv"
7+
8+
"github.com/gofiber/fiber/v3"
9+
)
10+
11+
func fiberServer() {
12+
greet := func(c fiber.Ctx) error {
13+
return c.SendString("Hello, World")
14+
}
15+
go func() {
16+
port := 8080
17+
fmt.Printf("Server running on http://localhost:%d\n", port)
18+
19+
app := fiber.New(fiber.Config{
20+
CaseSensitive: true,
21+
StrictRouting: true,
22+
DisableDefaultDate: true,
23+
DisableHeaderNormalizing: true,
24+
DisableDefaultContentType: true,
25+
})
26+
app.Get("/", greet)
27+
log.Fatal(app.Listen(":" + strconv.Itoa(port)))
28+
}()
29+
30+
restyAttack()
31+
}

k7/example/main.go

Lines changed: 43 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,70 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
6-
"net/http"
7-
"runtime"
85
"time"
96

10-
"connectrpc.com/connect"
117
"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"
169
)
1710

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+
)
3715

38-
return res, nil
16+
func init() {
17+
for i := 0; i < 3; i++ {
18+
restyClients = append(restyClients, *resty.New())
19+
}
3920
}
4021

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() {
6723
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,
7026
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")
8028
if err != nil {
8129
fmt.Println(err)
8230
return false
8331
}
8432

85-
return true
33+
restyClientId = (restyClientId + 1) % len(restyClients)
34+
35+
return res.StatusCode() == 200
8636
},
8737
}
88-
8938
config.Attack()
9039
}
9140

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
12142

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+
)
13348

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+
}
14252

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+
}

k7/example/nethttp.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func nethttpServer() {
9+
greet := func(w http.ResponseWriter, r *http.Request) {
10+
w.WriteHeader(http.StatusOK)
11+
w.Write([]byte("Hello world!"))
12+
return
13+
}
14+
15+
go func() {
16+
server := http.ServeMux{}
17+
18+
// Set up handlers
19+
server.HandleFunc("/", greet)
20+
21+
// Start the server
22+
port := 8080
23+
fmt.Printf("Server running on http://localhost:%d\n", port)
24+
http.ListenAndServe(fmt.Sprintf(":%d", port), &server)
25+
}()
26+
27+
restyAttack()
28+
}

0 commit comments

Comments
 (0)