本文介绍了Golang:使用gobench测试几次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用,它可以帮助我们测试一个请求并采取一些有关性能。



我做了一个修改,用于在每次调用中发送我需要测试的url。但是我发现不可能调用函数两次或更多次。

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

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

之后,我意识到gobench文件上的问题在这个部分:

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

最后,这是我希望执行此调用的方式,并且这可以提供更多的请求。

 包裹主要

进口(
fmt
gobench


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

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

我怎么能一次调用我的GobeachMain?
;目前代码是这样的,它只能调用一次,因为gobench得到的结果会在os.exit(0)上出现,如果我删除这一行,程序就会停留在

解决方案

gobench 从未被设计为用作库。 b
$ b

只需调用二进制代码就可以了

  package main 


log
os / exec


func gobench(url string){
cmd:= exec.Command( gobench,url)
err:= cmd.Run()
if err!= nil {
log.Fatalf(Command 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;我< LEN(searchRoutes); i ++ {
gobench(searchRoutes [i])
}
}


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

解决方案

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])
    }
}

这篇关于Golang:使用gobench测试几次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 10:18