Java&Go 三种 HTTP 客户端性能测试

服务端

服务端依旧采取moco_FunTester框架的 moco 服务,代码如下:

class Share extends MocoServer {

    static void main(String[] args) {
        def util = new ArgsUtil(args)
        def server = getServerNoLog(util.getIntOrdefault(0,12345))
        server.response("Have Fun ~ Tester !")
//        server.response(delay(textRes("Have Fun ~ Tester !"),10))
        def run = run(server)
        waitForKey("fan")
        run.stop()
    }
}

本次测试两种服务状态,一种无延迟 HTTP 服务,另外一种是低延迟(5ms 以及 10ms),总计三种 HTTP 服务。由于 Go 语言 HTTP 的库自带了 HTTP 服务开发功能,后面我会再写一篇文章,对比一下三种 HTTP 服务的性能:Java netty、Go(net/http)以及 Go(/valyala/fasthttp)。

测试用例

FunTester

FunTester 测试框架用的是 Java HttpClient,对 HttpClient API 做了封装,然后配合 FunTester 性能测试框架完成本次测试。实测中封装和框架对性能影响可忽略。

class HttpClientTest extends FunLibrary {

    static final String uri = "http://localhost:12345/test/fun"
    static final HttpRequestBase get = FunLibrary.getHttpGet(uri)

    static final int thread = 10
    static final int times = 10000

    public static void main(String[] args) {
        RUNUP_TIME = 0
        def tester = new FunTester()
        new Concurrent(tester, thread, DEFAULT_STRING).start()
    }

    private static class FunTester extends FixedThread<HttpRequestBase> {

        FunTester() {
            super(get, times, true)
        }

        @Override
        protected void doing() throws Exception {
            FunLibrary.executeOnly(get)
        }

        @Override
        FixedThread clone() {
            return new FunTester()
        }
    }

}

Go(net/http)

这里我写了一个测试方法,使用了 Go 语言的协程和 chan 知识点,比较粗糙,但能用。代码时有改动,后台回复 git 可以获取多个项目的 git 仓库地址,包含本项目。

var key bool = false

const (
 url    = "http://localhost:8001/test/fun"
 thread = 20
 times  = 10000
)

func TestPer(t *testing.T) {
 get := funtester.Get(url, nil)
 c := make(chan int)

 start := time.Now().UnixMilli()
 for i := 0; i < thread; i++ {
  go func() {
   sum := 0
   for i := 0; i < times; i++ {
    if key {
     break
    }
    funtester.Response(get)
    sum++
   }
   key = true
   c <- sum
  }()
 }
 total := 0
 for i := 0; i < thread; i++ {
  num := <-c
  total += num
 }
 end := time.Now().UnixMilli()
 diff := end - start
 log.Printf("总耗时: %f", float64(diff)/1000)

 log.Printf("请求总数: %d", total)
 log.Printf("QPS: %f", float64(total)/float64(diff)*1000.0)

}

Go(/valyala/fasthttp)

与 net/http 类似,不同之处是/valyala/fasthttp 无法使用同一个对象进行压测。java培训所以每次都要创建一个对象,但是实测居然效率更高,fasthttp 对象池果然牛。而且传说中 10 倍于 net/http,着实有点吹牛了。

var key bool = false

const (
 url    = "http://localhost:8001/test/fun"
 thread = 20
 times  = 10000
)

func TestPerFast(t *testing.T) {
 c := make(chan int)
 start := time.Now().UnixMilli()
 for i := 0; i < thread; i++ {
  go func() {
   sum := 0
   for i := 0; i < times; i++ {
    if key {
     break
    }
    get := funtester.FastGet(url, nil)
    funtester.FastResponse(get)
    sum++

   }
   key = true
   c <- sum
  }()
 }
 total := 0
 for i := 0; i < thread; i++ {
  num := <-c
  total += num
 }
 end := time.Now().UnixMilli()
 diff := end - start
 //total := thread * times
 log.Printf("总耗时: %f", float64(diff)/1000)

 log.Printf("请求总数: %d", total)
 log.Printf("QPS: %f", float64(total)/float64(diff)*1000.0)

}

测试

无延迟服务

截止到此,CPU 已经基本满荷运行,实际 80%,不知道是不是 macOS 的限制,现在 CPU 只能跑到 80%左右。

实际测试结果非常明显,总体 CPU 指标,FunTester/valyala/fasthttp相差不多,/valyala/fasthttp在 CPU 方面有些许优势,但是在内存上,简直无法理解。见鬼了一样。相比之下 net/http 逊色很多,在低并发的时候除了内存表现较好意外,CPU 和 QPS 均低于FunTester/valyala/fasthttp,但是在 10 线程的情况下,CPU 跑满,内存直线飙升,着实无法理解。等我再深入学习之后,估计能明白这个问题了。

延迟 5ms 服务

结论跟无延迟服务差不多。总体讲/valyala/fasthttp > FunTester > net/http。即使 Go 语言加成,net/http除了内存以外,其他两项指标均不如 Java 写的 FunTester。

结论

/valyala/fasthttp真心牛逼,建议使用 Go 语言进行 HTTP 性能测试的,直接跳过net/http

PS:下一次我将测试三种 HTTP 服务端的性能,敬请期待。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值