traceproftest

package
v1.49.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2023 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package traceproftest contains test cases and test helpers for testing cross-cutting tracer/profiler features such as code hotspots and endpoints. The package is not supposed to be imported by other packages.

Index

Constants

View Source
const (
	// HTTPWorkEndpointMethod is the http method used for the demo app.
	HTTPWorkEndpointMethod = "POST"
	// HTTPWorkEndpoint is the http endpoint used for the demo app.
	HTTPWorkEndpoint = "/work/:secret"
	// GRPCWorkEndpoint is the grpc endpoint used for the demo app.
	GRPCWorkEndpoint = "/testapp.TestApp/Work"
	// DirectEndpoint is the name of the expected endpoint when the root span
	// is created directly.
	DirectEndpoint = "workHandler"
)
View Source
const (
	// Direct directly executes requests logic without any transport overhead.
	Direct testAppType = "direct"
	// GRPC executes requests via GRPC.
	GRPC testAppType = "grpc"
	// HTTP executes requests via HTTP.
	HTTP testAppType = "http"
)

Variables

View Source
var CustomLabels = map[string]string{"user label": "user val"}

CustomLabels are the user-defined pprof labels to apply in the work endpoint to simulate user label interacting with our own labels.

Functions

func CPURusage

func CPURusage(t testing.TB) time.Duration

CPURusage returns the amount of On-CPU time for the process (sys+user) since it has been started. It uses getrusage(2).

func ValidSpanID

func ValidSpanID(id string) bool

ValidSpanID returns true if id is a valid span id (random.Uint64()).

Types

type App

type App struct {
	CPUProfiler

	pb.UnimplementedTestAppServer
	// contains filtered or unexported fields
}

App is an instance of the demo app.

func (*App) CPUProfile

func (a *App) CPUProfile(t testing.TB) *CPUProfile

CPUProfile stops the app and returns its CPU profile.

func (*App) Stop

func (a *App) Stop(t testing.TB)

Stop stops the app, tracer and cpu profiler in an idempotent fashion.

func (*App) Work

func (a *App) Work(ctx context.Context, req *pb.WorkReq) (*pb.WorkRes, error)

Work implements the request handler for the demo app. It's reused regardless of the config.AppType.

func (*App) WorkRequest

func (a *App) WorkRequest(t testing.TB, req *pb.WorkReq) *pb.WorkRes

WorkRequest sends the given req to the demo app and returns the response. The config.AppType determines how the request is made.

type AppConfig

type AppConfig struct {
	// Endpoints is passed to tracer.WithProfilerEndpoints()
	Endpoints bool
	// CodeHotspots is passed to tracer.WithProfilerCodeHotspots()
	CodeHotspots bool
	// ChildOf uses tracer.ChildOf() to declare the parent of cpuSpan instead of
	// tracer.StartSpanFromContext().
	ChildOf bool
	// AppType is the type of the test app that is being simulated.
	AppType testAppType
}

AppConfig defines the behavior and profiling options for the demo app.

func (AppConfig) Start

func (c AppConfig) Start(t testing.TB) *App

Start starts the demo app, including tracer and profiler.

type CPUProfile

type CPUProfile struct {
	// contains filtered or unexported fields
}

CPUProfile is a test utility to extract data from a CPU profile for testing.

func NewCPUProfile

func NewCPUProfile(data []byte) (*CPUProfile, error)

NewCPUProfile returns a new CPU profile for the given data.

func (*CPUProfile) Duration

func (c *CPUProfile) Duration() (d time.Duration)

Duration returns the total amont of CPU time in this profile.

func (*CPUProfile) LabelDuration

func (c *CPUProfile) LabelDuration(label, val string) (d time.Duration)

LabelDuration returns the CPU time for the given pprof label in this profile. The special val "*" can be used to match any label value.

func (*CPUProfile) Labels

func (c *CPUProfile) Labels() map[Label]int

Labels returns the number of samples per individual label in this profile.

func (*CPUProfile) LabelsDuration

func (c *CPUProfile) LabelsDuration(labels map[string]string) (d time.Duration)

LabelsDuration returns the CPU time for the given pprof labels in this profile. The special val "*" can be used to match any label value.

func (*CPUProfile) Samples

func (c *CPUProfile) Samples() int

Samples returns the number of samples in the CPU profile.

func (*CPUProfile) Size

func (c *CPUProfile) Size() int

Size returns the size of the pprof encoded CPU profile in bytes.

func (*CPUProfile) WriteFile

func (c *CPUProfile) WriteFile(path string) error

WriteFile writes the profile to the given path.

type CPUProfiler

type CPUProfiler struct {
	// contains filtered or unexported fields
}

CPUProfiler is a simplified implementation of the CPU profiler found in pkg profiler that retains essential performance characteristics but is more convenient for testing.

TODO(fg) Would be nice to figure out a clean way to use the actual profiler pkg for this in the future.

func StartCPUProfile

func StartCPUProfile(t testing.TB) *CPUProfiler

StartCPUProfile starts a new CPU profile.

func (*CPUProfiler) Stop

func (c *CPUProfiler) Stop(t testing.TB) *CPUProfile

Stop stops the CPU profiler and returns the CPU profile.

type Label

type Label struct {
	Key string
	Val string
}

Label represents a simplified pprof label where the value is a comma-separated string rather than a []string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL