压力测试

同个接口先对micro api测试:

micro的资源占用比general-api还要高,内存占用不高。

image-20200805143140540

image-20200805143505360

image-20200805143344220

Round1,同个接口对gateway api库测试

CPU占用低,内存占用狂飙,RPS平均在640左右

image-20200806110058318

性能分析:

对内存分析:

go tool pprof -inuse_space http://127.0.0.1:9999/debug/pprof/heap

Fetching profile over HTTP from http://127.0.0.1:9999/debug/pprof/heap
Saved profile in /Users/ben/pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.033.pb.gz
Type: inuse_space
Time: Aug 6, 2020 at 10:57am (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top20
Showing nodes accounting for 93.58MB, 100% of 93.58MB total
Showing top 20 nodes out of 81
      flat  flat%   sum%        cum   cum%
   33.51MB 35.81% 35.81%    33.51MB 35.81%  gogs.xinyan.tech/beenest/common/gateway.getHeaderMd
   20.50MB 21.91% 57.71%    20.50MB 21.91%  net/textproto.(*Reader).ReadMIMEHeader
      11MB 11.76% 69.47%    17.09MB 18.27%  context.WithDeadline
       5MB  5.34% 74.81%     6.09MB  6.51%  time.AfterFunc
       5MB  5.34% 80.16%        5MB  5.34%  context.WithValue
    4.01MB  4.29% 84.45%     4.01MB  4.29%  bufio.NewWriterSize
    3.51MB  3.75% 88.20%     3.51MB  3.75%  bufio.NewReaderSize (inline)
       3MB  3.21% 91.41%        3MB  3.21%  go.uber.org/zap/zapcore.newCounters (inline)
       2MB  2.14% 93.55%     2.50MB  2.67%  gogs.xinyan.tech/beenest/common/wrapper/global.ParseResponse
    1.78MB  1.91% 95.45%     1.78MB  1.91%  github.com/henrylee2cn/pholcus/common/pinyin.init
    1.09MB  1.17% 96.62%     1.09MB  1.17%  time.startTimer
    1.06MB  1.14% 97.76%     1.06MB  1.14%  google.golang.org/grpc/internal/transport.newBufWriter
    0.60MB  0.64% 98.39%     0.60MB  0.64%  google.golang.org/protobuf/internal/strs.(*Builder).AppendFullName
    0.50MB  0.54% 98.93%     0.50MB  0.54%  io/ioutil.glob..func1

对应代码:

func(w http.ResponseWriter, r *http.Request) {
    md := getHeaderMd(r)
    ctx = metadata.NewContext(ctx, md)  // @TODO 这个应该要放在 withTimeoutCtx之后
        ctx = withTimeoutCtx(ctx, methodName)  
        body, err = ioutil.ReadAll(r.Body)
}

func withTimeoutCtx(ctx context.Context, methodName string) context.Context {
    timeoutSec, ok := MethodTimeoutMap[methodName]
    if !ok {
        timeoutSec = MethodTimeoutMap["*"] // @TODO 这里在Local环境默认是600s
    }

    if timeoutSec > 0 {
        ctx, _ = context.WithTimeout(ctx, time.Second * time.Duration(timeoutSec))
    }

    return ctx
}

Round2,同个接口对gateway api库测试

CPU占用比Round1略高,主要是timeout设置为了5s,要处理ctx清除

内存占用稳定,RPS平均在610左右

image-20200806111634981

性能分析:

go tool pprof -inuse_space http://127.0.0.1:9999/debug/pprof/heap
Fetching profile over HTTP from http://127.0.0.1:9999/debug/pprof/heap
Saved profile in /Users/ben/pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.035.pb.gz
Type: inuse_space
Time: Aug 6, 2020 at 11:14am (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top20
Showing nodes accounting for 21948.26kB, 100% of 21948.26kB total
Showing top 20 nodes out of 99
      flat  flat%   sum%        cum   cum%
 6682.03kB 30.44% 30.44%  6682.03kB 30.44%  bufio.NewWriterSize
 4140.35kB 18.86% 49.31%  4140.35kB 18.86%  bufio.NewReaderSize (inline)
 3073.03kB 14.00% 63.31%  3073.03kB 14.00%  go.uber.org/zap/zapcore.newCounters
 1825.78kB  8.32% 71.63%  1825.78kB  8.32%  github.com/henrylee2cn/pholcus/common/pinyin.init
 1089.33kB  4.96% 76.59%  1089.33kB  4.96%  google.golang.org/grpc/internal/transport.newBufWriter
  521.05kB  2.37% 78.97%   521.05kB  2.37%  github.com/rcrowley/go-metrics.newExpDecaySampleHeap (inline)
  516.01kB  2.35% 81.32%   516.01kB  2.35%  unicode.init
     514kB  2.34% 83.66%      514kB  2.34%  bytes.makeSlice
  513.69kB  2.34% 86.00%   513.69kB  2.34%  google.golang.org/protobuf/internal/filedesc.(*File).initDecls
  512.62kB  2.34% 88.33%   512.62kB  2.34%  regexp/syntax.(*compiler).inst
  512.10kB  2.33% 90.67%   512.10kB  2.33%  github.com/dgrijalva/jwt-go.RegisterSigningMethod
  512.09kB  2.33% 93.00%   512.09kB  2.33%  strings.Replace
  512.08kB  2.33% 95.33%   512.08kB  2.33%  net/http.(*Server).newConn
  512.07kB  2.33% 97.67%   512.07kB  2.33%  context.propagateCancel
  512.02kB  2.33%   100%   512.02kB  2.33%  github.com/micro/go-micro/v2/debug/trace/memory.(*Tracer).Start
         0     0%   100%  3084.01kB 14.05%  bufio.NewReader

对应改动:

func(w http.ResponseWriter, r *http.Request) {
    md := getHeaderMd(r)
    // ctx = metadata.NewContext(ctx, md)  
        ctx = withTimeoutCtx(ctx, methodName)  
        body, err = ioutil.ReadAll(r.Body)

        ctx = metadata.NewContext(ctx, md) // 放到withTimeoutCtx之后
}
Copyright © xinyan all right reserved,powered by Gitbook该文件修订时间: 2020-08-06 11:42:58

results matching ""

    No results matching ""