profiling

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterSQLDriver

func RegisterSQLDriver(name string, driver driver.Driver, profiler *Profiler)

RegisterSQLDriver registers a wrapped SQL driver with profiling support

Types

type Bottleneck

type Bottleneck struct {
	Type        string        `json:"type"` // "cpu", "memory", "io", "database", "http"
	Description string        `json:"description"`
	Impact      float64       `json:"impact"` // 0-1 scale of impact
	Duration    time.Duration `json:"duration"`
	Suggestion  string        `json:"suggestion"`
}

Bottleneck represents a performance bottleneck

type FlameGraph

type FlameGraph struct {
	Root        *FlameGraphNode `json:"root"`
	TotalTime   int64           `json:"total_time"`
	SampleCount int64           `json:"sample_count"`
}

FlameGraph generates flame graph data from CPU profile

func GenerateFlameGraph

func GenerateFlameGraph(profileData []byte) (*FlameGraph, error)

GenerateFlameGraph generates a flame graph from CPU profile data

func (*FlameGraph) ConvertToD3Format

func (fg *FlameGraph) ConvertToD3Format() map[string]interface{}

ConvertToD3Format converts the flame graph to D3.js compatible format

func (*FlameGraph) GenerateTextFlameGraph

func (fg *FlameGraph) GenerateTextFlameGraph() string

GenerateTextFlameGraph generates a text-based flame graph (folded stack format)

func (*FlameGraph) GetHotSpots

func (fg *FlameGraph) GetHotSpots(threshold float64) []HotSpot

GetHotSpots identifies the hottest code paths

type FlameGraphNode

type FlameGraphNode struct {
	Name     string            `json:"name"`
	Value    int64             `json:"value"` // Time spent in nanoseconds
	Children []*FlameGraphNode `json:"children"`
}

FlameGraphNode represents a node in the flame graph

type HTTPCallMetric

type HTTPCallMetric struct {
	Method   string        `json:"method"`
	URL      string        `json:"url"`
	Duration time.Duration `json:"duration"`
	Status   int           `json:"status"`
	Size     int64         `json:"size"`
}

HTTPCallMetric represents metrics for an HTTP call

type HotSpot

type HotSpot struct {
	Path       []string `json:"path"`
	Name       string   `json:"name"`
	Time       int64    `json:"time"`
	Percentage float64  `json:"percentage"`
}

HotSpot represents a performance hot spot

type Metrics

type Metrics struct {
	RequestID        string                   `json:"request_id"`
	StartTime        time.Time                `json:"start_time"`
	EndTime          time.Time                `json:"end_time"`
	Duration         time.Duration            `json:"duration"`
	CPUTime          time.Duration            `json:"cpu_time"`
	MemoryAlloc      uint64                   `json:"memory_alloc"`
	MemoryTotalAlloc uint64                   `json:"memory_total_alloc"`
	NumGoroutines    int                      `json:"num_goroutines"`
	NumGC            uint32                   `json:"num_gc"`
	GCPauseTotal     time.Duration            `json:"gc_pause_total"`
	FunctionTimings  map[string]time.Duration `json:"function_timings,omitempty"`
	SQLQueries       []SQLQueryMetric         `json:"sql_queries,omitempty"`
	HTTPCalls        []HTTPCallMetric         `json:"http_calls,omitempty"`
	Bottlenecks      []Bottleneck             `json:"bottlenecks,omitempty"`
	CPUProfile       []byte                   `json:"-"` // Raw CPU profile data
	HeapProfile      []byte                   `json:"-"` // Raw heap profile data
}

Metrics contains performance metrics for a request

type ProfileType

type ProfileType uint32

ProfileType represents the type of profiling to perform

const (
	// ProfileNone disables all profiling
	ProfileNone ProfileType = 0
	// ProfileCPU enables CPU profiling
	ProfileCPU ProfileType = 1 << iota
	// ProfileMemory enables memory profiling
	ProfileMemory
	// ProfileGoroutine enables goroutine tracking
	ProfileGoroutine
	// ProfileBlocking enables blocking profiling
	ProfileBlocking
	// ProfileAll enables all profiling types
	ProfileAll = ProfileCPU | ProfileMemory | ProfileGoroutine | ProfileBlocking
)

type ProfileWriter

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

ProfileWriter captures CPU profiles

func (*ProfileWriter) Write

func (pw *ProfileWriter) Write(p []byte) (n int, err error)

type Profiler

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

Profiler handles performance profiling for requests

func NewProfiler

func NewProfiler(maxMetrics int) *Profiler

NewProfiler creates a new profiler instance

func (*Profiler) ClearMetrics

func (p *Profiler) ClearMetrics()

ClearMetrics clears all stored metrics

func (*Profiler) EndProfiling

func (p *Profiler) EndProfiling(ctx context.Context) *Metrics

EndProfiling ends profiling for a request

func (*Profiler) GetAllMetrics

func (p *Profiler) GetAllMetrics() []*Metrics

GetAllMetrics retrieves all stored metrics

func (*Profiler) GetMetrics

func (p *Profiler) GetMetrics(requestID string) (*Metrics, bool)

GetMetrics retrieves metrics for a request

func (*Profiler) RecordFunction

func (p *Profiler) RecordFunction(ctx context.Context, name string, fn func() error) error

RecordFunction records the timing of a function

func (*Profiler) RecordHTTPCall

func (p *Profiler) RecordHTTPCall(ctx context.Context, method, url string, duration time.Duration, status int, size int64)

RecordHTTPCall records metrics for an HTTP call

func (*Profiler) RecordSQLQuery

func (p *Profiler) RecordSQLQuery(ctx context.Context, query string, duration time.Duration, rows int, err error)

RecordSQLQuery records metrics for a SQL query

func (*Profiler) SetEnabled

func (p *Profiler) SetEnabled(enabled bool)

SetEnabled enables or disables profiling

func (*Profiler) SetProfileType

func (p *Profiler) SetProfileType(pt ProfileType)

SetProfileType sets the types of profiling to perform

func (*Profiler) SetThreshold

func (p *Profiler) SetThreshold(threshold time.Duration)

SetThreshold sets the minimum duration to trigger profiling

func (*Profiler) SetTracer

func (p *Profiler) SetTracer(ctx context.Context, tracer interface{})

SetTracer associates a tracer with the profiling context

func (*Profiler) StartProfiling

func (p *Profiler) StartProfiling(ctx context.Context, requestID string) context.Context

StartProfiling starts profiling for a request

type SQLHook

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

SQLHook provides hooks for SQL operations

func NewSQLHook

func NewSQLHook(profiler *Profiler) *SQLHook

NewSQLHook creates a new SQL hook

func (*SQLHook) WrapDriver

func (h *SQLHook) WrapDriver(d driver.Driver) driver.Driver

WrapDriver wraps a SQL driver with profiling hooks

type SQLQueryMetric

type SQLQueryMetric struct {
	Query    string        `json:"query"`
	Duration time.Duration `json:"duration"`
	Rows     int           `json:"rows"`
	Error    string        `json:"error,omitempty"`
}

SQLQueryMetric represents metrics for a SQL query

Jump to

Keyboard shortcuts

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