2016-10-06 1 views
0

fasthttp 패키지로 Rest API를 구축 중입니다. 나는 그것을 테스트 함수에서 println("HERE")에 도착하는 10 초 이상 걸리는이 경로에 요청을 보내는 경우Golang fasthttp 요청이 매우 느림

package main 

import (
    "github.com/valyala/fasthttp" 
    "runtime" 
) 

func main() { 

    runtime.GOMAXPROCS(8) 
    m := func(ctx *fasthttp.RequestCtx) { 
     switch string(ctx.Path()) { 
     case "/test": 
      test(ctx) 
     default: 
      ctx.Error("not found", fasthttp.StatusNotFound) 
     } 
    } 

    fasthttp.ListenAndServe(":80", m) 
} 

func test(ctx *fasthttp.RequestCtx) { 
    println("HERE") 
} 

: 나는 성능을 측정하기 위해 사용하고 테스트 경로를 가지고있다.

Node.js에 필적할만한 종단점을 만들었으며이 기능과 경로에는 126 밀리 초가 걸립니다.
이동 경로에서이 경로가 가리키는 기능을 호출하는 데 오랜 시간이 걸리는 이유는 무엇입니까? 걸리는 나를 위해

+0

, 그것은에 도달 (1 초 이하) 거의 시간이 걸립니다 'println ("여기")'. 당신은 테스트를 얼마나 훌륭하게 설정 했습니까? –

+0

샘플을 게시하고 코드를 게시 (의견 추가) 코드 (2) 출력 –

답변

2

은 (77 % 부하에서 단 2 코어 CPU이 1 개, 2 코드를 실행하는, http.Head 당 79.454545us) 100000 http.Head에 대한 7.9454545s. 취득이와

100000 

2 :

package main 

import (
    "fmt" 

    "github.com/valyala/fasthttp" 
) 

func main() { 
    m := func(ctx *fasthttp.RequestCtx) { 
     switch string(ctx.Path()) { 
     case "/test": 
      test(ctx) 
     default: 
      fmt.Println(i) 
      ctx.Error("not found", fasthttp.StatusNotFound) 
     } 
    } 
    fasthttp.ListenAndServe(":80", m) 
} 

func test(ctx *fasthttp.RequestCtx) { 
    i++ 
} 

var i int = 0 

출력 :

이 시도 runtime.GOMAXPROCS(8)해야하고, println()

1 대신 fmt.Println()를 사용하지 않는

package main 

import (
    "fmt" 
    "net/http" 
    "time" 
) 

func main() { 
    t := time.Now() 
    for i := 0; i < 100000; i++ { 
     read(`http://localhost/test`) 
    } 
    fmt.Println(time.Since(t)) 
    read(`http://localhost/`) 
} 

func read(url string) { 
    _, err := http.Head(url) 
    if err != nil { 
     fmt.Println(err) 
    } 
} 

출력 :

7.9454545s 

3 출력 This code의 : 난 그냥 정확한 코드를 테스트했습니다

8.6294936s