douzuizhuo0587 2014-06-02 23:16 采纳率: 0%
浏览 92
已采纳

Golang:使用gobench进行多次测试

I'm working with gobench, wich help us to test a request and have several measures about the performance.

I did a modification for send the url that I need test in each call. But I found that it's not posible call the function twice or more times.

func GobenchMain(currectRoute *string) {
    startTime := time.Now()

    var done sync.WaitGroup
    results := make(map[int]*Result)
...
}

After that I realize that the problem on gobench file is on this part:

signalChannel := make(chan os.Signal, 2)
    signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
    go func() {
        _ = <-signalChannel
        printResults(results, startTime)
        os.Exit(0)
    }()

Finally this is the way that I expect to do the call, and with this make more that one request.

package main

import (
    "fmt"
    "gobench"
)

var searchRoutes = []string{
    "http:www.myurl.com/request1",
    "http:www.myurl.com/request2",
    "http:www.myurl.com/request3",
    "http:www.myurl.com/request4",
}

func main() {
    for i := 0; i < len(searchRoutes); i++ {
        gobench.GobenchMain(&searchRoutes[i])
    }
...
}

How can I call more that one time my GobeachMain? ;Currently with the code like that It's only possible call once, because after gobench get the result goes on os.exit(0), and if I remove this line, the program just goes to stand by

  • 写回答

1条回答 默认 最新

  • douyu7879 2014-06-03 07:09
    关注

    gobench was never designed to be used as a library.

    Just call the binary something like this

    package main
    
    import (
        "log"
        "os/exec"
    )
    
    func gobench(url string) {
        cmd := exec.Command("gobench", url)
        err := cmd.Run()
        if err != nil {
            log.Fatalf("Command finished with error: %v", err)
        }
    }
    
    var searchRoutes = []string{
        "http:www.myurl.com/request1",
        "http:www.myurl.com/request2",
        "http:www.myurl.com/request3",
        "http:www.myurl.com/request4",
    }
    
    func main() {
        for i := 0; i < len(searchRoutes); i++ {
            gobench(searchRoutes[i])
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助