在Heroku上运行Golang测试

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

Running golang tests on heroku

问题

我大致按照这些说明将我的应用部署到了Heroku上,一切都运行良好。

我有一些基准测试,我想在Heroku上运行它们,以测试本地机器和Heroku服务器之间的性能差异。不幸的是,我无法找到如何运行它们(Heroku找不到Go可执行文件)。这个有可能吗?

谢谢!

英文:

I more or less followed these instructions to deploy my app to heroku. Everything works well.

http://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku.html

I've got some benchmarking tests that I'd like to run on heroku to test whether or not there's a difference in performance between my local machine and the heroku box. Unfortunately I can't figure out how to run them (heroku can't find the go executable). Is it possible to do this?

Thanks!

答案1

得分: 1

我还没有使用过Heroku。可能是Heroku在使用go test命令运行基准测试时出现了问题,可能是临时目录有问题。

尝试在程序中运行基准测试。例如:

package main

import (
	"fmt"
	"math"
	"testing"
)

// 要进行基准测试的函数
func Area(r float64) float64 {
	return math.Pi * r * r
}

// 基准测试函数
func BenchmarkArea(b *testing.B) {
	r := 42.0
	for i := 0; i < b.N; i++ {
		_ = Area(r)
	}
}

func main() {
	br := testing.Benchmark(BenchmarkArea)
	fmt.Println(br.String() + br.MemString())
}

输出结果:

2000000000         1.21 ns/op       0 B/op         0 allocs/op

你可以尝试在本地运行这个程序进行基准测试。

参考链接:

英文:

I haven't used Heroku. It's possible that Heroku has a problem running a benchmark using the go test command; there may be a problem with the temporary directory.

> Command go
>
> Test packages
>
> 'Go test' recompiles each package along with any files with names
> matching the file pattern "*test.go". Files whose names begin with
> "
" (including "_test.go") or "." are ignored. These additional files
> can contain test functions, benchmark functions, and example
> functions. See 'go help testfunc' for more. Each listed package causes
> the execution of a separate test binary.
>
> The package is built in a temporary directory so it does not interfere
> with the non-test installation.

Try running the benchmark tests in a program. For example,

package main

import (
	&quot;fmt&quot;
	&quot;math&quot;
	&quot;testing&quot;
)

// a function to be benchmarked
func Area(r float64) float64 {
	return math.Pi * r * r
}

// benchmark function
func BenchmarkArea(b *testing.B) {
	r := 42.0
	for i := 0; i &lt; b.N; i++ {
		_ = Area(r)
	}
}

func main() {
	br := testing.Benchmark(BenchmarkArea)
	fmt.Println(br.String() + br.MemString())
}

Output:

2000000000	         1.21 ns/op       0 B/op	       0 allocs/op

> Package testing
>
> func Benchmark
>
> func Benchmark(f func(b *B)) BenchmarkResult
>
> Benchmark benchmarks a single function. Useful for creating custom
> benchmarks that do not use the "go test" command.

huangapple
  • 本文由 发表于 2014年9月6日 02:15:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/25691715.html
匿名

发表评论

匿名网友

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

确定