Skip to content

Commit

Permalink
bombardier: update simplebenchserver and switch from fasthttp.HostCli…
Browse files Browse the repository at this point in the history
…ent to fasthttp.Client

fasthttp's Client, HostClient behave slightly differently,
but all the tests still pass and requests generated seem to be correct.

cmd/utils/simplebenchserver now has --std-http mode in which it
uses HTTP server provided by standard library's net/http package.

Might help with unexpected behaviour encountered in #75.
  • Loading branch information
codesenberg committed Apr 29, 2023
1 parent 4a9acba commit 95c11e5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
27 changes: 13 additions & 14 deletions clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ type clientOpts struct {
}

type fasthttpClient struct {
client *fasthttp.HostClient
client *fasthttp.Client

headers *fasthttp.RequestHeader
host, requestURI, method string
headers *fasthttp.RequestHeader
host, requestURI, method, scheme string

body *string
bodProd bodyStreamProducer
Expand All @@ -55,10 +55,9 @@ func newFastHTTPClient(opts *clientOpts) client {
}
c.host = u.Host
c.requestURI = u.RequestURI()
c.client = &fasthttp.HostClient{
Addr: u.Host,
IsTLS: u.Scheme == "https",
MaxConns: int(opts.maxConns),
c.scheme = u.Scheme
c.client = &fasthttp.Client{
MaxConnsPerHost: int(opts.maxConns),
ReadTimeout: opts.timeout,
WriteTimeout: opts.timeout,
DisableHeaderNamesNormalizing: true,
Expand All @@ -85,13 +84,9 @@ func (c *fasthttpClient) do() (
if len(req.Header.Host()) == 0 {
req.Header.SetHost(c.host)
}
req.Header.SetMethod(c.method)
if c.client.IsTLS {
req.URI().SetScheme("https")
} else {
req.URI().SetScheme("http")
}
req.SetRequestURI(c.requestURI)
req.Header.SetMethod(c.method)
req.URI().SetScheme(c.scheme)
if c.body != nil {
req.SetBodyString(*c.body)
} else {
Expand All @@ -105,7 +100,11 @@ func (c *fasthttpClient) do() (
// fire the request
start := time.Now()
err = c.client.Do(req, resp)
code = resp.StatusCode()
if err != nil {
code = -1
} else {
code = resp.StatusCode()
}
usTaken = uint64(time.Since(start).Nanoseconds() / 1000)

// release resources
Expand Down
34 changes: 24 additions & 10 deletions cmd/utils/simplebenchserver/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"bytes"
"log"
"strings"
"net/http"

"github.com/alecthomas/kingpin"
"github.com/valyala/fasthttp"
Expand All @@ -16,19 +17,32 @@ var responseSize = kingpin.Flag("size", "size of response in bytes").
Default("1024").
Short('s').
Uint()
var stdHTTP = kingpin.Flag("std-http", "use standard http library").
Default("false").
Bool()

func main() {
kingpin.Parse()
response := strings.Repeat("a", int(*responseSize))
response := bytes.Repeat([]byte("a"), int(*responseSize))
addr := "localhost:" + *serverPort
log.Println("Starting HTTP server on:", addr)
err := fasthttp.ListenAndServe(addr, func(c *fasthttp.RequestCtx) {
_, werr := c.WriteString(response)
if werr != nil {
log.Println(werr)
}
})
if err != nil {
log.Println(err)
var lserr error
if *stdHTTP {
lserr = http.ListenAndServe(addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, werr := w.Write(response)
if werr != nil {
log.Println(werr)
}
}))
} else {
lserr = fasthttp.ListenAndServe(addr, func(c *fasthttp.RequestCtx) {
_, werr := c.Write(response)
if werr != nil {
log.Println(werr)
}
})
}
if lserr != nil {
log.Println(lserr)
}
}

0 comments on commit 95c11e5

Please sign in to comment.