代码之家  ›  专栏  ›  技术社区  ›  Kamath

从主/游乐场进行基准跑

go
  •  2
  • Kamath  · 技术社区  · 6 年前

    我试图创建一套基准测试

    https://play.golang.org/p/uWWITU-WKaL

    包装主体

    import (
        "fmt"
        "testing"
    )
    
    func runall(a, b string) (bool, error) {
        return true, nil
    }
    
    func main() {
        bench := []testing.InternalBenchmark{
            {
                F: Benchmark_Dev,
            },
        }
    
        tests := []testing.InternalTest{
            {
                F: Test_Dev,
            },
        }
        testing.Main(runall, tests, bench, nil)
    }
    
    func Test_Dev(t *testing.T) {
        fmt.Println("Test_Dev")
    }
    
    func Benchmark_Dev(b *testing.B) {
        fmt.Println("Benchmark_Dev")
        b.ReportAllocs()
        for i := 0; i < b.N; i++ {
            res := i % 10
            fmt.Println(res)
        }
    }
    

    我看到测试运行得很好,但是基准测试从来没有运行过。

    1 回复  |  直到 6 年前
        1
  •  5
  •   Alex Yu    6 年前

    如果你读 "About" on Go Playground :

    如果程序包含测试或示例而没有主函数,则服务将运行测试。基准测试可能不受支持,因为程序运行在沙盒环境中,资源有限。

    你会找到答案的