github.com/lrita/cache@v1.0.1/pprof_test.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cache
     6  
     7  import (
     8  	"bytes"
     9  	"runtime"
    10  	"runtime/pprof"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestProfile(t *testing.T) {
    16  	var (
    17  		c Cache
    18  		b bytes.Buffer
    19  	)
    20  	pp := pprof.Lookup("github.com/lrita/cache")
    21  	if pp == nil {
    22  		t.Fatal("runtime/pprof.Lookup got nil")
    23  	}
    24  	if name := pp.Name(); name != "github.com/lrita/cache" {
    25  		t.Fatalf("profile.Name got %#v, want %v", name, "github.com/lrita/cache")
    26  	}
    27  	if count := pp.Count(); count != 0 {
    28  		t.Fatalf("profile.Count got %#v, want %v", count, 0)
    29  	}
    30  
    31  	SetProfileFraction(1)
    32  
    33  	c.Get()
    34  	c.Get()
    35  	if count := pp.Count(); count != 2 {
    36  		t.Fatalf("profile.Count got %#v, want %v", count, 2)
    37  	}
    38  
    39  	if err := pp.WriteTo(&b, 1); err != nil {
    40  		t.Fatalf("profile.WriteTo failed: %v", err)
    41  	}
    42  	content := b.String()
    43  
    44  	SetProfileFraction(0)
    45  	c.Get()
    46  	c.Get()
    47  	if count := pp.Count(); count != 0 {
    48  		t.Fatalf("profile.Count got %#v, want %v", count, 0)
    49  	}
    50  	if !strings.Contains(content, "cycles/second=1") {
    51  		t.Fatalf("got %q, want contains %q", content, "cycles/second=1")
    52  	}
    53  	if !strings.Contains(content, "github.com/lrita/cache.TestProfile") {
    54  		t.Fatalf("got %q, want contains %q", content, "github.com/lrita/cache.TestProfile")
    55  	}
    56  	_, file, _, _ := runtime.Caller(1)
    57  	if !strings.Contains(content, file) {
    58  		t.Fatalf("got %q, want contains %q", content, file)
    59  	}
    60  }