prettyTimer

package module
v0.0.0-...-c96793f Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2023 License: AGPL-3.0 Imports: 4 Imported by: 3

README

PrettyTimer

demo

Example code

package main

import (
	"fmt"
	"time"

	"github.com/8ff/prettyTimer"
)

func main() {
	ts := prettyTimer.NewTimingStats()

	// Using Start and Finish to record timing
	ts.Start()
	time.Sleep(1 * time.Second) // Simulating some work
	ts.Finish()

	// Using RecordTiming to manually record timings
	start := time.Now()
	ts.RecordTiming(time.Since(start))
	ts.RecordTiming(time.Since(start))
	ts.RecordTiming(time.Since(start))

	// Print stats to console
	ts.PrintStats()

	// Return stats and get single value
	stats := ts.GetStats()
	fmt.Printf("99th percentile: %s\n", stats.Percent99)
}

Example from examples/main.go

$ examples % go run main.go 
Min Time: 1s, Max Time: 3s, Avg Time: 2s, Count: 3
50th: 2s, 90th: 3s, 99th: 3s

Documentation

Index

Constants

View Source
const (
	Reset  = "\033[0m"
	Red    = "\033[31m"
	Green  = "\033[32m"
	Yellow = "\033[33m"
	Blue   = "\033[34m"
	Purple = "\033[35m"
)

ANSI color codes

Variables

This section is empty.

Functions

This section is empty.

Types

type Stats

type Stats struct {
	MinTime   time.Duration
	MaxTime   time.Duration
	AvgTime   time.Duration
	Count     int
	Percent50 time.Duration
	Percent90 time.Duration
	Percent99 time.Duration
}

Return stats as struct

type TimingStats

type TimingStats struct {
	Count     int
	TotalTime time.Duration
	MinTime   time.Duration
	MaxTime   time.Duration
	Timings   []time.Duration // To keep track of individual timings for percentile calculation
	// contains filtered or unexported fields
}

func NewTimingStats

func NewTimingStats() *TimingStats

Initialize a TimingStats instance with reasonable defaults

func (*TimingStats) CalculatePercentile

func (t *TimingStats) CalculatePercentile(percentile float64) time.Duration

Calculate percentile

func (*TimingStats) Finish

func (t *TimingStats) Finish()

Finish the timer and record the timing

func (*TimingStats) GetStats

func (t *TimingStats) GetStats() Stats

Return stats as struct

func (*TimingStats) PrintStats

func (t *TimingStats) PrintStats()

Print stats to stdout

func (*TimingStats) RecordTiming

func (t *TimingStats) RecordTiming(duration time.Duration)

Record a new timing

func (*TimingStats) Start

func (t *TimingStats) Start()

Start the timer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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