jeudi 12 novembre 2020

Why isn't my `go test -bench=. ` working?

I'm trying to run a benchmark test with fasthttp package, but go test -bench is not running a benchmark, giving error that no tests to run. There seems to be two major problems.

First, -run=none flag which disable testing mode did not work.

Second, even after adding some dummy testing code, still the benchmark functions failed to run.

I need some help with figuring out the cause of the failure, and right method to fix this.

What version of Go are you using (go version)?

$ go version
go version go1.15 windows/amd64

What operating system and processor architecture are you using (go env)?

$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\admin\AppData\Local\go-build
set GOENV=C:\Users\admin\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\admin\Documents\GoProjects\go-elasticsearch\_examples\fasthttp\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\admin\AppData\Local\Temp\go-build221346138=/tmp/go-build -gno-record-gcc-switches

What did you do?

ran a fasthttp benchmark as go-elasticsearch instructs

$ go test -run=none -bench=. -benchmem -benchtime=100x -v .\fasthttp_benchmark_test.go

fasthttp_benchmark_test.go looks like this

// Licensed to Elasticsearch B.V. under one or more agreements.
// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

package fasthttp_test

import (
    "testing"

    "github.com/elastic/go-elasticsearch/v8"
    "github.com/elastic/go-elasticsearch/v8/_examples/fasthttp"
)

func BenchmarkHTTPClient(b *testing.B) {
    b.ReportAllocs()

    client, err := elasticsearch.NewDefaultClient()
    if err != nil {
        b.Fatalf("ERROR: %s", err)
    }

    b.Run("Info()", func(b *testing.B) {
        b.ResetTimer()

        for i := 0; i < b.N; i++ {
            if res, err := client.Info(); err != nil {
                b.Errorf("Unexpected error when getting a response: %s", err)
            } else {
                res.Body.Close()
            }
        }
    })
}

func BenchmarkFastHTTPClient(b *testing.B) {
    b.ReportAllocs()

    client, err := elasticsearch.NewClient(
        elasticsearch.Config{Transport: &fasthttp.Transport{}},
    )
    if err != nil {
        b.Fatalf("ERROR: %s", err)
    }

    b.Run("Info()", func(b *testing.B) {
        b.ResetTimer()

        for i := 0; i < b.N; i++ {
            if res, err := client.Info(); err != nil {
                b.Errorf("Unexpected error when getting a response: %s", err)
            } else {
                res.Body.Close()
            }
        }
    })
}

What did you expect to see?

benchmark outputs

What did you see instead?

testing: warning: no tests to run
PASS
ok      github.com/elastic/go-elasticsearch/v8/_examples/fasthttp       0.777s [no tests to run]

Aucun commentaire:

Enregistrer un commentaire