Skip to content

Instantly share code, notes, and snippets.

@brancz
Created December 11, 2020 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brancz/02608bf0ddf63dc540bd4747cff7a3cb to your computer and use it in GitHub Desktop.
Save brancz/02608bf0ddf63dc540bd4747cff7a3cb to your computer and use it in GitHub Desktop.
Small tests of pprof merges being associative. Not exhaustive, but running this multiple times showed no failures.
package main
import (
"bytes"
"io/ioutil"
"math/rand"
"reflect"
"testing"
"time"
"github.com/google/pprof/profile"
)
func MustParse(profileBytes []byte) *profile.Profile {
p, err := profile.Parse(bytes.NewBuffer(profileBytes))
if err != nil {
panic(err)
}
return p
}
func MustReadFile(file string) []byte {
b, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
return b
}
func TestMergeAssociativity(t *testing.T) {
profiles := []*profile.Profile{
MustParse(MustReadFile("1.pb.gz")),
MustParse(MustReadFile("2.pb.gz")),
MustParse(MustReadFile("3.pb.gz")),
MustParse(MustReadFile("4.pb.gz")),
}
expected, err := profile.Merge(profiles)
if err != nil {
t.Fatal(err)
}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(profiles), func(i, j int) { profiles[i], profiles[j] = profiles[j], profiles[i] })
res, err := profile.Merge(profiles)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, res) {
t.Fatal("Not deep equal")
}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(profiles), func(i, j int) { profiles[i], profiles[j] = profiles[j], profiles[i] })
res, err = profile.Merge([]*profile.Profile{mustMerge(profiles[0:2]), mustMerge(profiles[2:])})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, res) {
t.Fatal("Not deep equal")
}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(profiles), func(i, j int) { profiles[i], profiles[j] = profiles[j], profiles[i] })
res, err = profile.Merge([]*profile.Profile{mustMerge(profiles[0:1]), mustMerge(profiles[1:])})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, res) {
t.Fatal("Not deep equal")
}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(profiles), func(i, j int) { profiles[i], profiles[j] = profiles[j], profiles[i] })
res, err = profile.Merge([]*profile.Profile{mustMerge(profiles[0:3]), mustMerge(profiles[3:])})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, res) {
t.Fatal("Not deep equal")
}
}
func mustMerge(profiles []*profile.Profile) *profile.Profile {
p, err := profile.Merge(profiles)
if err != nil {
panic(err)
}
return p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment