elapsing

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: MIT Imports: 9 Imported by: 0

README

elapsing

Go Report Testing

A simple Golang library for measuring elapsed time in application, function calls, and even for goroutine!


Features

  • Easy to use
  • Pretty output with colors into terminal
  • Able to measure elapsed time for individual steps
  • Able to measure elapsed time for function calls
  • Able to measure elapsed time for goroutines
  • Concurrent safe

Install

go get github.com/nekomeowww/elapsing

Usage

It is just simple as:

package main

import (
    "fmt"

    "github.com/nekomeowww/elapsing"
)

func main() {
    elapsing := elapsing.New()

    time.Sleep(100 * time.Millisecond)
    elapsing.StepEnds()
    fmt.Println(elapsing.Stats())
}

If you would like to measure the elapsed time of a function call, you can use elapsing.ForFunc() to get a new FuncCall instance to measure individually:

package main

import (
    "fmt"

    "github.com/nekomeowww/elapsing"
)

func func1(elapsingFunc *elapsing.FuncCall) {
    defer elapsingFunc.Return() // remember to call this!

    time.Sleep(100 * time.Millisecond)
    elapsingFunc.StepEnds()
}

func main() {
    elapsing := elapsing.New()

    time.Sleep(100 * time.Millisecond)
    elapsing.StepEnds()

    func1(elapsing.ForFunc())

    fmt.Println(elapsing.Stats())
}

Examples

Example 1 [Try it]

Example 1 contains the use of elapsing.ForFunc() to measure the elapsed time of a function call.

It will output the following result with colors:

screenshot with colors

[Source Code: cmd/examples/example1]

Example 2 [Try it]

Example 2 contains the use of CJK names and the output will auto padding the spaces to make the output looks better.

It will output the following result with colors:

screenshot with colors

[Source Code: cmd/examples/example2]

Performance

Benchmark result as below.

Benchmark on MacBook Pro (14-inches, M1 Pro, 2022)
goos: darwin
goarch: arm64
pkg: github.com/nekomeowww/elapsing
BenchmarkStepEnds
BenchmarkStepEnds-10                     7178438        177.1 ns/op      180 B/op        2 allocs/op
BenchmarkStepEndsWithName
BenchmarkStepEndsWithName-10            11180500        122.8 ns/op      156 B/op        1 allocs/op
BenchmarkStepEndsWithTime
BenchmarkStepEndsWithTime-10             8067342        150.5 ns/op      169 B/op        2 allocs/op
BenchmarkForFunc
BenchmarkForFunc-10                       495000         4119 ns/op    16858 B/op        6 allocs/op
BenchmarkForFuncStepEnds
BenchmarkForFuncStepEnds-10              6566698        164.5 ns/op      168 B/op        2 allocs/op
BenchmarkForFuncStepEndsWithName
BenchmarkForFuncStepEndsWithName-10     10403928        113.7 ns/op      163 B/op        1 allocs/op
BenchmarkForFuncStepEndsWithTime
BenchmarkForFuncStepEndsWithTime-10      7167212        170.7 ns/op      180 B/op        2 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Elapsing

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

func New

func New() *Elapsing

New creates a new elapsing with the name of the caller function

func (*Elapsing) ForFunc

func (e *Elapsing) ForFunc() *FuncCall

func (*Elapsing) On

func (e *Elapsing) On() time.Time

func (*Elapsing) Stats

func (e *Elapsing) Stats() string

func (*Elapsing) StepEnds

func (e *Elapsing) StepEnds(callOpts ...StepCallOptions)

StepEnds adds a new step to the elapsing.

func (*Elapsing) TotalElapsed

func (e *Elapsing) TotalElapsed() time.Duration

TotalElapsed returns the total elapsed time since the elapsing is created

func (*Elapsing) Type

func (e *Elapsing) Type() StepType

type FuncCall

type FuncCall struct {
	*Elapsing
	// contains filtered or unexported fields
}

func (*FuncCall) Return

func (f *FuncCall) Return()

func (*FuncCall) StepEnds

func (f *FuncCall) StepEnds(callOpts ...StepCallOptions)

type Step

type Step interface {
	Type() StepType
	On() time.Time
}

type StepCallOptions added in v1.1.0

type StepCallOptions struct {
	Name string
	On   time.Time
}

StepCallOptions

func WithName

func WithName(name string) StepCallOptions

func WithTime

func WithTime(t time.Time) StepCallOptions

type StepType

type StepType int
const (
	StepTypePoint StepType
	StepTypeElapsing
)

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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