Package testing
https://golang.org/pkg/testing/

Benchmarks

The benchmark function must run the target code b.N times. During benchmark 
execution, b.N is adjusted until the benchmark function lasts long enough 
to be timed reliably.

The first thing to do is to reduce the program to the simplest program that 
exhibits the behavior. For example,

package main

import (
    "sync/atomic"
    "testing"
)

func BenchmarkTotal(b *testing.B) {
    var total uint64
    b.Run("Count", func(b *testing.B) {
        b.RunParallel(func(pb *testing.PB) {
            var count uint64
            for pb.Next() {
                count++
            }
            b.Logf("count %d", count)
            atomic.AddUint64(&total, count)
        })
    })
    b.Logf("total %d", total)
}

$ go test -run=! -bench=. -v -cpu=1 total_test.go
goos: linux
goarch: amd64
BenchmarkTotal/Count             1000000000             2.17 ns/op
--- BENCH: BenchmarkTotal/Count
    total_test.go:16: count 1
    total_test.go:16: count 100
    total_test.go:16: count 10000
    total_test.go:16: count 1000000
    total_test.go:16: count 100000000
    total_test.go:16: count 1000000000
--- BENCH: BenchmarkTotal
    total_test.go:20: total 1101010101
PASS
ok      command-line-arguments    2.391s

1000000000 is the final total and 1101010101 is the sum of all totals.

Peter

On Wednesday, August 9, 2017 at 11:43:34 PM UTC-4, d...@dgraph.io wrote:
>
> Hi
>
> I am trying to benchmark a key-value store written in Go. I have the code 
> as shown below. There are a few lines that are specific to the store 
> itself, other than that it is all related to benchmarking. I am trying to 
> find the no. of keys that have valid values versus the no. of keys that 
> dont have values.
>
> Running the code below as 
>
> go test -v --bench BenchmarkReadRandomBadger  --timeout 10m --benchtime 
> 3m 
>
> results in this output
>
> ===
> BenchmarkReadRandomBadger/read-random-badger-128                20000000   
>           11842 ns/op
> --- BENCH: BenchmarkReadRandomBadger
>         bench_test.go:101: badger 13277801 keys had valid values.
>         bench_test.go:102: badger 7732300 keys had no values
>         bench_test.go:103: badger 0 keys had errors
> PASS
> ok      github.com/dgraph-io/badger-bench       256.448s
> ===
>
> Now, based on my understanding, the values above should add up to the 
> number of iterations (20000000), but instead add up to 13277801+7732300 
> = 21010101
>
> Can somebody help me understand this discrepancy.
>
> Code:
>
> ===
> func BenchmarkReadRandomBadger(b *testing.B) {
>         // store setup
>         bdb, err := getBadger()
> y.Check(err)
> defer bdb.Close()
>
> var totalFound uint64
> var totalErr uint64
> var totalNotFound uint64
> b.Run("read-random-badger", func(b *testing.B) {
> b.RunParallel(func(pb *testing.PB) {
> var found, errored, notFound uint64
> for pb.Next() {
> key := newKey()
> var val badger.KVItem
>                                 // get value from store
> if err := bdb.Get(key, &val); err == nil && val.Value() != nil {
> found++
> } else if err != nil {
> errored++
> } else {
> notFound++
> }
> }
> atomic.AddUint64(&totalFound, found)
> atomic.AddUint64(&totalErr, errored)
> atomic.AddUint64(&totalNotFound, notFound)
> })
> })
> b.Logf("badger %d keys had valid values.", totalFound)
> b.Logf("badger %d keys had no values", totalNotFound)
> b.Logf("badger %d keys had errors", totalErr)
> }
> ===
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to