tracer

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

Tracer Package

Runtime call tracing library for hierarchical performance profiling.

Usage

Basic Example
import "github.com/LFDT-Panurus/panurus/tools/profiler/tracer"

func main() {
    tracer.Enable()
    defer tracer.Disable()
    
    myFunction()
    
    tracer.Print()
}

func myFunction() {
    defer tracer.Enter("myFunction")()
    // ... function body ...
}
In Benchmarks
func BenchmarkMyCode(b *testing.B) {
    tracer.Enable()
    defer tracer.Disable()
    
    for i := 0; i < b.N; i++ {
        MyCode()
    }
    
    tracer.PrintWithOptions(tracer.PrintOptions{
        RootFunction:   "MyCode",
        ShowPercent:    true,
        ShowAbsolute:   true,
        AggregateLoops: true,
    })
}

API

Control
Enable()              // Start profiling
EnableDiscovery()     // Start discovery mode
Disable()             // Stop and clear
IsEnabled() bool      // Check if active
Tracing
// Record function entry/exit
Enter(funcName string) func()

// Usage in every instrumented function:
defer tracer.Enter("FunctionName")()
Output
// Simple output
Print()

// Customized output
PrintWithOptions(opts PrintOptions)

// Top N functions
PrintSummary(topN int)
PrintSummaryWithRoot(topN int, rootFunc string)
Options
type PrintOptions struct {
    RootFunction   string  // Start from this function
    ShowPercent    bool    // Show % of root time
    ShowAbsolute   bool    // Show duration
    AggregateLoops bool    // Combine repeated calls (x2, x5)
    MinPercentage  float64 // Hide functions below this %
}

Output Format

Call Hierarchy
=== Call Hierarchy ===
└── RootFunction [1.5s, 100.00%]
    ├── ChildA [0.9s, 60.00%]
    │   └── GrandchildA x3 [0.6s, 40.00%]
    └── ChildB [0.6s, 40.00%]
  • Tree shows parent-child relationships
  • Times are cumulative (include all children)
  • x3 = function called 3 times (total time shown)
  • Percentages relative to root
Performance Summary
=== Performance Summary (Top Functions) ===
Function                                                         Total Time  % of Root
---------------------------------------------------------------------------------------
RootFunction                                                     1.5s        100.00%
ChildA                                                           0.9s         60.00%
GrandchildA                                                      0.6s         40.00%

Discovery Mode

Find which packages are called:

tracer.EnableDiscovery()
defer tracer.Disable()

myFunction()

packages := tracer.GetDiscoveredPackages()
for _, pkg := range packages {
    fmt.Println(pkg)
}

Best Practices

  1. Always use defer: defer tracer.Enter("FuncName")()
  2. Enable/Disable in pairs: Use defer tracer.Disable()
  3. Include receiver type: (*Type).Method for methods
  4. Focus analysis: Use RootFunction option
  5. Filter noise: Use MinPercentage option

Thread Safety

All functions are thread-safe. Concurrent calls will interleave in the tree but times remain accurate.

Examples

See parent directory's README.md for complete examples of manual instrumentation in benchmarks and tests.

For automated profiling without manual instrumentation, use the profile.sh script in the parent directory.

Documentation

Overview

Package tracer provides hierarchical call tracing and performance profiling for Go applications.

The tracer supports two modes:

  • Discovery mode: Captures function calls to identify code paths for instrumentation
  • Profiling mode: Records detailed timing information and builds call hierarchies

Usage:

tracer.Enable()
defer tracer.Disable()
// ... your code with tracer.Enter() calls ...
tracer.PrintWithOptions(tracer.PrintOptions{
    RootFunction: "MyFunction",
    ShowPercent: true,
    ShowAbsolute: true,
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Disable

func Disable()

Disable deactivates the tracer and clears its state. Call this after profiling is complete, typically in a defer statement.

func Enable

func Enable()

Enable activates the tracer in profiling mode. Call this before the code you want to profile, and Disable() when done.

func EnableDiscovery

func EnableDiscovery()

EnableDiscovery activates the tracer in discovery mode. In this mode, the tracer captures function names without detailed timing, useful for identifying which packages need instrumentation.

func Enter

func Enter(funcName string) func()

Enter records entry into a function and returns a function to call on exit. This should be called at the beginning of instrumented functions using defer:

func MyFunction() {
    defer tracer.Enter("MyFunction")()
    // ... function body ...
}

The function name should include the receiver type for methods:

defer tracer.Enter("(*MyType).MyMethod")()

func GetCallHierarchy

func GetCallHierarchy() string

func GetDiscoveredPackages

func GetDiscoveredPackages() []string

GetDiscoveredPackages returns all unique packages discovered during tracing

func IsDiscoveryMode

func IsDiscoveryMode() bool

IsDiscoveryMode returns true if the tracer is in discovery mode.

func IsEnabled

func IsEnabled() bool

IsEnabled returns true if the tracer is currently active.

func Print

func Print()

func PrintSummary

func PrintSummary(topN int)

func PrintSummaryWithOptions

func PrintSummaryWithOptions(topN int, rootFunctionName string, opts PrintOptions)

PrintSummaryWithOptions prints a performance summary with configurable display options.

func PrintSummaryWithRoot

func PrintSummaryWithRoot(topN int, rootFunctionName string)

func PrintWithOptions

func PrintWithOptions(opts PrintOptions)

Types

type CallNode

type CallNode struct {
	Name      string        // Function name (e.g., "(*Type).Method")
	StartTime time.Time     // When the function was entered
	Duration  time.Duration // Total time spent in this function
	Children  []*CallNode   // Child function calls
	Parent    *CallNode     // Parent function call
	CallCount int           // Number of times this function was called (for aggregation)
}

CallNode represents a single function call in the call hierarchy. It tracks timing, parent-child relationships, and call counts for aggregation.

type FunctionStats

type FunctionStats struct {
	Name     string
	Duration time.Duration
	Percent  float64
}

FunctionStats holds timing statistics for a function

type PrintOptions

type PrintOptions struct {
	RootFunction   string  // Start printing from this function (empty = use actual root)
	ShowPercent    bool    // Show percentage of time relative to root
	ShowAbsolute   bool    // Show absolute time duration
	AggregateLoops bool    // Combine multiple calls to same function
	MinPercentage  float64 // Hide functions below this percentage threshold
}

PrintOptions configures the output format for call hierarchy printing.

Jump to

Keyboard shortcuts

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