Giter VIP home page Giter VIP logo

Comments (4)

valyala avatar valyala commented on July 25, 2024

I cannot reproduce this bug on the master branch:

$ GOOS=linux GOARCH=arm go build -v github.com/valyala/fasthttp
runtime
errors
sync/atomic
unicode
unicode/utf8
container/list
math
crypto/subtle
sync
sort
io
syscall
hash
crypto/cipher
bytes
strings
bufio
crypto/hmac
internal/singleflight
time
internal/syscall/unix
strconv
math/rand
crypto/aes
crypto
reflect
crypto/sha512
crypto/md5
crypto/rc4
crypto/sha1
crypto/sha256
encoding/base64
os
encoding/pem
path/filepath
net
encoding/binary
fmt
io/ioutil
crypto/des
math/big
encoding/hex
log
mime
mime/quotedprintable
runtime/debug
crypto/elliptic
encoding/asn1
crypto/rand
crypto/rsa
crypto/dsa
crypto/x509/pkix
crypto/ecdsa
crypto/x509
net/textproto
mime/multipart
crypto/tls
github.com/valyala/fasthttp

Try updating fasthttp. This bug seems to be fixed in the issue #7 .

from fasthttp.

abacaj avatar abacaj commented on July 25, 2024

yep that fixed it, thanks!

from fasthttp.

abacaj avatar abacaj commented on July 25, 2024

@valyala Side note: still getting fairly low results using both apache2 benchmark utils and siege for testing rps.

I'm on a 8core 16gb machine, the average is only 1800rps using your sample code from the techempower example.

Full code for your reference:

const helloWorldStr = "hello world!"

func plainTextHandler(ctx *fasthttp.RequestCtx) {
  helloWorldStr := fmt.Sprintf("num of cpu: %v", runtime.NumCPU())
  helloWorldBytes := []byte(helloWorldStr)
  ctx.Success("text/plain", helloWorldBytes)
}

func mainHandler(ctx *fasthttp.RequestCtx) {
  path := ctx.Path()
  switch {
  case fasthttp.EqualBytesStr(path, "/"):
    plainTextHandler(ctx)

  default:
    ctx.Error("unexpected path", fasthttp.StatusBadRequest)
  }
}

func main() {
  runtime.GOMAXPROCS(runtime.NumCPU())
  var err error
  var s fasthttp.Server
  s.Handler = mainHandler
  s.Name = "fasthttp"

  ln, err := net.Listen("tcp", ":8002")
  if err != nil {
      log.Fatalf("error in net.Listen: %s", err)
  }

  if err = s.Serve(ln); err != nil {
    log.Fatalf(("error when serving incoming connections: %s"), err)
  }
}

from fasthttp.

valyala avatar valyala commented on July 25, 2024

The code above can be optimized. Substitute

  helloWorldStr := fmt.Sprintf("num of cpu: %v", runtime.NumCPU())
  helloWorldBytes := []byte(helloWorldStr)
  ctx.Success("text/plain", helloWorldBytes)

with

  fmt.Fprintf(ctx, "num of cpu: %d", runtime.NumCPU())
  ctx.SetContentType("text/plain")

This way you avoid two memory allocations and two memory copies - one for the string returned from fmt.Sprintf and another for the string to []byte conversion.

The program can be further optimized by calculating helloWorldBytes contents only once in a global scope:

var helloWorldBytes = []byte(fmt.Sprintf("num of cpu: %v", runtime.NumCPU()))

As for low results I suspect there are problems with network settings on your OS. Read this article (the first article I found when googling for linux network tuning).

Fasthttp achieves 1.4M rps in TechEmpower's plaintext benchmark on my laptop when using wrk with 16 connections and 256 pipelined requests.

from fasthttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.