-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ecf030
commit 765cebe
Showing
16 changed files
with
5,663 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
2023/11/08 15:40:55.429364 engine.go:666: [Debug] HERTZ: Method=GET absolutePath=/plaintext --> handlerName=main.hertzPlaintext (num=1 handlers) | ||
2023/11/08 15:40:55.429736 engine.go:666: [Debug] HERTZ: Method=GET absolutePath=/json --> handlerName=main.hertzJSON (num=1 handlers) | ||
2023/11/08 15:40:55.429762 engine.go:666: [Debug] HERTZ: Method=GET absolutePath=/query/:queryID --> handlerName=main.hertzQuery (num=1 handlers) | ||
2023/11/08 15:40:55.429911 engine.go:394: [Info] HERTZ: Using network library=netpoll | ||
2023/11/08 15:40:55.430204 transport.go:115: [Info] HERTZ: HTTP server listening on address=[::]:8080 | ||
|
||
┌───────────────────────────────────────────────────┐ | ||
│ Fiber v2.50.0 │ | ||
│ http://127.0.0.1:8080 │ | ||
│ (bound on host 0.0.0.0 and port 8080) │ | ||
│ │ | ||
│ Handlers ............. 6 Processes ........... 1 │ | ||
│ Prefork ....... Disabled PID ............. 55369 │ | ||
└───────────────────────────────────────────────────┘ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
hertz,plaintext,128,88120,4465,17707 | ||
hertz,plaintext,256,74168,21733,69901 | ||
hertz,plaintext,512,64664,35093,76669 | ||
hertz,plaintext,1024,56405,60771,107718 | ||
hertz,plaintext,2048,32,5568351,6362738 | ||
hertz,plaintext,4096,5769,2516230,3393500 | ||
hertz,json,128,75346,14273,38655 | ||
hertz,json,256,75008,10591,42136 | ||
hertz,json,512,73413,19349,62038 | ||
hertz,json,1024,54280,73547,122530 | ||
hertz,json,2048,63,3540236,5731425 | ||
hertz,json,4096,4952,2334259,3946933 | ||
hertz,query,128,103449,3173,23576 | ||
hertz,query,256,94422,6051,12872 | ||
hertz,query,512,98142,7446,29442 | ||
hertz,query,1024,89925,27363,56243 | ||
hertz,query,2048,379,1583043,2230204 | ||
hertz,query,4096,81,5604590,5606366 | ||
fiber,plaintext,128,143151,54347,115456 | ||
fiber,plaintext,256,132547,62265,132104 | ||
fiber,plaintext,512,127158,12165,57228 | ||
fiber,plaintext,1024,116265,52665,101130 | ||
fiber,plaintext,2048,966,334286,1058179 | ||
fiber,plaintext,4096,152,4240725,7931917 | ||
fiber,json,128,135473,89281,125031 | ||
fiber,json,256,134873,86672,145170 | ||
fiber,json,512,129276,22100,103514 | ||
fiber,json,1024,121937,16930,40417 | ||
fiber,json,2048,858,289022,313216 | ||
fiber,json,4096,267,2334039,3268279 | ||
fiber,query,128,147857,68342,116754 | ||
fiber,query,256,132903,88568,121318 | ||
fiber,query,512,127182,31415,87100 | ||
fiber,query,1024,120340,45965,111843 | ||
fiber,query,2048,932,350760,375346 | ||
fiber,query,4096,427,2759610,4871423 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package bpool | ||
|
||
import ( | ||
"runtime" | ||
"sync" | ||
) | ||
|
||
var bpool sync.Pool | ||
|
||
func init() { | ||
bpool = sync.Pool{ | ||
New: func() interface{} { | ||
return &Buffer{} | ||
}, | ||
} | ||
} | ||
|
||
type Buffer struct { | ||
B []byte | ||
} | ||
|
||
func (b *Buffer) Write(p []byte) (int, error) { | ||
b.B = append(b.B, p...) | ||
return len(p), nil | ||
} | ||
|
||
func (b *Buffer) Bytes() []byte { | ||
return b.B | ||
} | ||
|
||
// Set sets ByteBuffer.B to p. | ||
func (b *Buffer) Set(p []byte) { | ||
b.B = append(b.B[:0], p...) | ||
} | ||
|
||
func (b *Buffer) SetString(s string) { | ||
b.B = append(b.B[:0], s...) | ||
} | ||
|
||
func (b *Buffer) Reset() { | ||
b.B = b.B[:0] | ||
} | ||
|
||
func (b *Buffer) release() { | ||
b.B = b.B[:0] | ||
bpool.Put(b) | ||
} | ||
|
||
func GetBuffer() *Buffer { | ||
buffer := bpool.Get().(*Buffer) | ||
runtime.SetFinalizer(buffer, (*Buffer).release) | ||
return buffer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pkill -9 main | ||
pkill -9 pprof |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/cloudwego/hertz/examples/bpool" | ||
"github.com/cloudwego/hertz/internal/bytesconv" | ||
"github.com/cloudwego/hertz/pkg/app" | ||
"github.com/cloudwego/hertz/pkg/app/server" | ||
) | ||
|
||
func queriesWithPool(ctx *app.RequestContext) map[string]string { | ||
m := make(map[string]string, ctx.QueryArgs().Len()) | ||
keybuf, valueBuf := bpool.GetBuffer(), bpool.GetBuffer() | ||
ctx.QueryArgs().VisitAll(func(key, value []byte) { | ||
keybuf.Set(key) | ||
valueBuf.Set(key) | ||
m[bytesconv.B2s(keybuf.Bytes())] = bytesconv.B2s(valueBuf.Bytes()) | ||
}) | ||
return m | ||
} | ||
|
||
func queries(ctx *app.RequestContext) map[string]string { | ||
m := make(map[string]string, ctx.QueryArgs().Len()) | ||
ctx.QueryArgs().VisitAll(func(key, value []byte) { | ||
m[bytesconv.B2s(key)] = bytesconv.B2s(value) | ||
}) | ||
return m | ||
} | ||
|
||
var port string | ||
|
||
func main() { | ||
flag.StringVar(&port, "p", "3000", "") | ||
flag.Parse() | ||
fmt.Println("port:", port) | ||
testSvr(port) | ||
} | ||
|
||
func testSvr(port string) { | ||
h := server.New( | ||
server.WithHostPorts(":"+port), | ||
server.WithDisablePrintRoute(true), | ||
) | ||
|
||
h.GET("/qp", qp) | ||
h.GET("/q", q) | ||
h.Spin() | ||
} | ||
|
||
func qp(_ context.Context, ctx *app.RequestContext) { | ||
//chunkedBodyWriter := resp.NewChunkedBodyWriter(&ctx.Response, ctx.GetWriter()) | ||
//resp.ReleaseChunkedBodyWriter(chunkedBodyWriter) | ||
queriesWithPool(ctx) | ||
} | ||
|
||
func q(_ context.Context, ctx *app.RequestContext) { | ||
queries(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
port: 3000 | ||
2023/11/13 12:03:07.263043 engine.go:394: [Info] HERTZ: Using network library=netpoll | ||
2023/11/13 12:03:07.264365 transport.go:115: [Info] HERTZ: HTTP server listening on address=[::]:3000 | ||
Fetching profile over HTTP from http://localhost:3000/debug/pprof/profile?seconds=60 | ||
Running 1m test @ http://localhost:3000 | ||
8 threads and 512 connections | ||
Thread Stats Avg Stdev Max +/- Stdev | ||
Latency 5.67ms 4.51ms 129.65ms 96.65% | ||
Req/Sec 11.79k 2.00k 19.96k 79.75% | ||
Latency Distribution | ||
50% 5.17ms | ||
75% 5.58ms | ||
90% 6.72ms | ||
99% 21.59ms | ||
5628074 requests in 1.00m, 483.06MB read | ||
Socket errors: connect 0, read 655, write 0, timeout 0 | ||
Requests/sec: 93644.13 | ||
Transfer/sec: 8.04MB | ||
Saved profile in /Users/skyenought/pprof/pprof.samples.cpu.034.pb.gz | ||
Serving web UI on http://localhost:3321 | ||
go tool pprof: signal: terminated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/bin/bash | ||
|
||
type=$1 | ||
pprof_port=$2 | ||
url="http://localhost:3000/qp?test1=$RANDOM" | ||
if [ -f nohup.out ]; then | ||
rm nohup.out | ||
fi | ||
|
||
echo "Building..." | ||
go build -o main main.go | ||
sleep 1 | ||
|
||
echo "server start..." | ||
nohup ./main & | ||
echo "PID of the server: $!" | ||
sleep 2 | ||
|
||
nohup go tool pprof -http=:${pprof_port} http://localhost:3000/debug/pprof/profile?seconds=60 & | ||
echo "PID of the ${type} pprof $!" | ||
|
||
if [ $type == "q" ]; then | ||
wrk -H 'Connection: keep-alive' --latency -c 512 -d 60 --timeout 8 -t 8 -s s2.lua "http://localhost:3000" >> nohup.out | ||
fi | ||
wrk -H 'Connection: keep-alive' --latency -c 512 -d 60 --timeout 8 -t 8 -s script.lua "http://localhost:3000" >> nohup.out | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
math.randomseed(os.time()) -- 初始化随机数种子 | ||
request = function() | ||
local test1 = math.random(100) -- 生成一个1到100之间的随机数 | ||
local path = "/q?test1=" .. test1 -- 拼接请求路径 | ||
return wrk.format(nil, path) -- 返回请求 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
math.randomseed(os.time()) -- 初始化随机数种子 | ||
request = function() | ||
local test1 = math.random(100) -- 生成一个1到100之间的随机数 | ||
local path = "/qp?test1=" .. test1 -- 拼接请求路径 | ||
return wrk.format(nil, path) -- 返回请求 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Running 5m test @ http://localhost:3000/qp?test1=1&test2=2&test3=3&test4=test4&test5=test3&test6=test3&test7=t | ||
8 threads and 256 connections | ||
Thread Stats Avg Stdev Max +/- Stdev | ||
Latency 24.38ms 8.21ms 62.02ms 81.71% | ||
Req/Sec 1.08k 116.09 1.22k 56.25% | ||
Latency Distribution | ||
50% 26.08ms | ||
75% 29.45ms | ||
90% 30.40ms | ||
99% 45.27ms | ||
1728 requests in 25.58s, 154.97KB read | ||
Socket errors: connect 0, read 1166437, write 6957, timeout 0 | ||
Non-2xx or 3xx responses: 52 | ||
Requests/sec: 67.54 | ||
Transfer/sec: 6.06KB |
Oops, something went wrong.