How to profile a library

I wrote a library [link] which I want to profile using pprof.

I wrote a small executable that would profile it [Playground]; however, when I view the call chart it terminates at “github.com/deathly809/parallels/vectors.DotFloat32.func1.1” and says that ~91% of my time is there.

I looked online and cannot seem to find any information and I am sure the answer is something simple that I am just overlooking.

Thanks for any replies!

250ms represents 96.15% of the total runtime. This is too short to meaningfully profile, the amount of time needs to be in seconds at least.

I strongly recommend you write your function as a testing.Benchmark the you get profiling for free and can adjust the amount of time each benchmark runs for.

http://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go

http://go-talks.appspot.com/github.com/davecheney/presentations/writing-high-performance-go.slide#1

1 Like

I started to write this but you haven’t provided a runnable sample so I couldn’t finish it. This should be pretty close

package vectors

import (
        "math/rand"
        "testing"

        "github.com/deathly809/gomath"
)

func genFloat32Array(n int, min, max float32) []float32 {
        A := make([]float32, n)
        for i := 0; i < n; i++ {
                A[i] = gomath.ScaleFloat32(min, max, 0, 1.0, rand.Float32())
        }
        return A
}

/* stuff */
const (
        N = 100000000
)

/* stuff */
var (
        A = genFloat32Array(N, 0.0, 1.0)
        B = genFloat32Array(N, 0.0, 1.0)
)

var result []float32

func BenchmarkDotFloat32(b *testing.B) {
        var r []float32
        for i := 0; i < b.N; i++ {
                r = DotFloat32(A, B)
        }
        result = r
}
1 Like

Thanks for the reply.

I already have some benchmarking code written, but I am not sure how I would profile it since the profiling tool requires an executable to run. Maybe the answer is in one of the links so I will check those out.

I think I figured out what is going on. The function that it shows does not call anymore functions. It was showing all the functions but I was just too confused to realize it.

Thanks again!

http://go-talks.appspot.com/github.com/davecheney/presentations/writing-high-performance-go.slide#21

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.