Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ module github.com/cloudwego/hertz-benchmark
go 1.16

require (
github.com/cloudwego/hertz v0.3.1
github.com/cloudwego/netpoll v0.2.6
github.com/apache/thrift v0.13.0
github.com/cloudwego/hertz v0.8.0
github.com/cloudwego/kitex v0.8.0
github.com/cloudwego/netpoll v0.5.1
github.com/fasthttp/router v1.4.3
github.com/gin-gonic/gin v1.7.7
github.com/gofiber/fiber/v2 v2.38.1
github.com/kr/pretty v0.1.0 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/montanaflynn/stats v0.6.6
github.com/stretchr/testify v1.8.4 // indirect
github.com/valyala/fasthttp v1.40.0
golang.org/x/sys v0.13.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/apache/thrift => github.com/apache/thrift v0.13.0
374 changes: 348 additions & 26 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions scripts/benchmark_server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ n=3000000
body=(1024 2048 4096 8192 16384 32768 65536)
concurrent=(100)
header=(1024)
repo=("hertz" "fasthttp" "gin" "fiber")
ports=(8001 8002 8003 8004)
repo=("hertz" "fasthttp" "gin" "fiber" "hex")
ports=(8001 8002 8003 8004 8005)
serverIP="http://127.0.0.1"

. ./scripts/build_all.sh
Expand Down
4 changes: 2 additions & 2 deletions scripts/benchmark_wrk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ t=30
body=(1024 2048 4096 8192 16384 32768 65536)
concurrent=(100)
header=(1024)
repo=("hertz" "fasthttp" "gin" "fiber")
ports=(8001 8002 8003 8004)
repo=("hertz" "fasthttp" "gin" "fiber" "hex")
ports=(8001 8002 8003 8004 8005)
serverIP="http://127.0.0.1"

. ./scripts/build_all.sh
Expand Down
1 change: 1 addition & 0 deletions scripts/build_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ $GOEXEC build -v -o output/bin/hertz_server ./server/hertz/hertz.go
$GOEXEC build -v -o output/bin/gin_server ./server/gin/gin.go
$GOEXEC build -v -o output/bin/fasthttp_server ./server/fasthttp/fasthttp.go
$GOEXEC build -v -o output/bin/fiber_server ./server/fiber/fiber.go
$GOEXEC build -v -o output/bin/hex_server ./server/hex/.
31 changes: 31 additions & 0 deletions server/hex/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2023 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"context"
example "github.com/cloudwego/hertz-benchmark/server/hex/kitex_gen/hello/example"
)

// HelloServiceImpl implements the last service interface defined in the IDL.
type HelloServiceImpl struct{}

// HelloMethod implements the HelloServiceImpl interface.
func (s *HelloServiceImpl) HelloMethod(ctx context.Context, request *example.HelloReq) (resp *example.HelloResp, err error) {

return resp, err
}
47 changes: 47 additions & 0 deletions server/hex/hex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
package main
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main


import (
"github.com/cloudwego/hertz-benchmark/perf"
"github.com/cloudwego/hertz/pkg/common/hlog"
"net"

"github.com/cloudwego/hertz-benchmark/server/hex/kitex_gen/hello/example/helloservice"

"github.com/cloudwego/kitex/pkg/klog"

"github.com/cloudwego/kitex/server"
)

func main() {

// start pprof server
go func() {
err := perf.ServeMonitor(":18005")
if err != nil {
hlog.Error(err)
}
}()

opts := kitexInit()

svr := helloservice.NewServer(new(HelloServiceImpl), opts...)

err := svr.Run()
if err != nil {
klog.Error(err.Error())
}
}

func kitexInit() (opts []server.Option) {
opts = append(opts, server.
WithTransHandlerFactory(&mixTransHandlerFactory{nil}))

// address
addr, err := net.ResolveTCPAddr("tcp", ":8005")
if err != nil {
panic(err)
}
opts = append(opts, server.WithServiceAddr(addr))

return
}
107 changes: 107 additions & 0 deletions server/hex/hex_trans_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
package main
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main


import (
"context"
"errors"
"fmt"
"github.com/cloudwego/hertz-benchmark/perf"
"github.com/cloudwego/hertz-benchmark/runner"
"github.com/cloudwego/kitex/pkg/remote/trans/gonet"
"net"
"regexp"
"unsafe"

"github.com/cloudwego/hertz/pkg/app"
hertzServer "github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/network"
"github.com/cloudwego/hertz/pkg/route"
"github.com/cloudwego/kitex/pkg/endpoint"
"github.com/cloudwego/kitex/pkg/remote"
"github.com/cloudwego/kitex/pkg/remote/trans/detection"
"github.com/cloudwego/kitex/pkg/remote/trans/nphttp2"
)

type mixTransHandlerFactory struct {
originFactory remote.ServerTransHandlerFactory
}

type transHandler struct {
remote.ServerTransHandler
}

// SetInvokeHandleFunc is used to set invoke handle func.
func (t *transHandler) SetInvokeHandleFunc(inkHdlFunc endpoint.Endpoint) {
t.ServerTransHandler.(remote.InvokeHandleFuncSetter).SetInvokeHandleFunc(inkHdlFunc)
}

func (m mixTransHandlerFactory) NewTransHandler(opt *remote.ServerOption) (remote.ServerTransHandler, error) {
var kitexOrigin remote.ServerTransHandler
var err error

if m.originFactory != nil {
kitexOrigin, err = m.originFactory.NewTransHandler(opt)
} else {
// if no customized factory just use the default factory under detection pkg.
kitexOrigin, err = detection.NewSvrTransHandlerFactory(gonet.NewSvrTransHandlerFactory(), nphttp2.NewSvrTransHandlerFactory()).NewTransHandler(opt)
}
if err != nil {
return nil, err
}
return &transHandler{ServerTransHandler: kitexOrigin}, nil
}

var httpReg = regexp.MustCompile(`^(?:GET |POST|PUT|DELE|HEAD|OPTI|CONN|TRAC|PATC)$`)

func (t *transHandler) OnRead(ctx context.Context, conn net.Conn) error {
c, ok := conn.(network.Conn)
if ok {
pre, _ := c.Peek(4)
if httpReg.Match(pre) {
//klog.Info("using Hertz to process request")
err := hertzEngine.Serve(ctx, c)
if err != nil {
err = errors.New(fmt.Sprintf("HERTZ: %s", err.Error()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <S1028> reported by reviewdog 🐶
should use fmt.Errorf(...) instead of errors.New(fmt.Sprintf(...))

}
return err
}
}
return t.ServerTransHandler.OnRead(ctx, conn)
}

func initHertz() *route.Engine {
h := hertzServer.New(hertzServer.WithIdleTimeout(0))

h.POST("/", echoHandler)
err := h.Engine.Init()
if err != nil {
panic(err)
}

err = h.MarkAsRunning()
if err != nil {
panic(err)
}

return h.Engine
}

var (
recorder = perf.NewRecorder("Hex@Server")
actionQuery = "action"
)

func echoHandler(c context.Context, ctx *app.RequestContext) {
runner.ProcessRequest(recorder, b2s(ctx.QueryArgs().Peek(actionQuery)))
ctx.SetContentType("text/plain; charset=utf8")
ctx.Response.SetBody(ctx.Request.Body())
}

func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

var hertzEngine *route.Engine

func init() {
hertzEngine = initHertz()
}
Loading