Golang:使用gobench进行多次测试。

huangapple go评论85阅读模式
英文:

Golang: Testing several times using gobench

问题

我正在使用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)
}()

最后,这是我期望进行调用的方式,并且可以进行多次请求。

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

如何多次调用我的GobenchMain函数呢?
目前,由于代码中的os.Exit(0),只能调用一次,如果我删除这行代码,程序将进入待机状态。

英文:

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() {
		_ = &lt;-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 (
	&quot;fmt&quot;
	&quot;gobench&quot;
)

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

func main() {
	for i := 0; i &lt; len(searchRoutes); i++ {
		gobench.GobenchMain(&amp;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

得分: 3

gobench从未设计成用作库。

只需像这样调用二进制文件

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])
	}
}
英文:

gobench was never designed to be used as a library.

Just call the binary something like this

package main

import (
	&quot;log&quot;
	&quot;os/exec&quot;
)

func gobench(url string) {
	cmd := exec.Command(&quot;gobench&quot;, url)
	err := cmd.Run()
	if err != nil {
		log.Fatalf(&quot;Command finished with error: %v&quot;, err)
	}
}

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

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

huangapple
  • 本文由 发表于 2014年6月3日 07:16:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/24004783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定