stopw

package module
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: MIT Imports: 5 Imported by: 4

README

stopw Go Reference build Coverage Code to Test Ratio Test Execution Time

A stopwatch library in Go for nested time measurement.

Usage

package main

import (
	"encoding/json"
	"fmt"

	"github.com/k1LoW/stopw"
)

func main() {
	stopw.Start()
	stopw.Start("sub span A")
	// do something for `sub span A`
	stopw.Start("sub span B")
	// do something for `sub span A` or `sub span B`
	stopw.Start("sub span A", "sub sub span a")
	// do something for `sub span A` or `sub span B` or `sub sub span a`
	stopw.Stop("sub span A", "sub sub span a")
	// do something for `sub span A` or `sub span B`
	stopw.Stop("sub span span A")
	// do something for `sub span B`
	stopw.Stop()

	r := stopw.Result()
	b, _ := json.MarshalIndent(r, "", "  ")
	fmt.Println(string(b))

	// Output:
	// {
	//   "id": "cbt1386v9mc80ofooblg",
	//   "started_at": "2009-11-10T23:00:00.436022+09:00",
	//   "stopped_at": "2009-11-10T23:00:00.436883+09:00",
	//   "elapsed": 860375,
	//   "breakdown": [
	//     {
	//       "id": "sub span A",
	//       "started_at": "2009-11-10T23:00:00.436153+09:00",
	//       "stopped_at": "2009-11-10T23:00:00.436594+09:00",
	//       "elapsed": 441292,
	//       "breakdown": [
	//         {
	//           "id": "sub sub span a",
	//           "started_at": "2009-11-10T23:00:00.436449+09:00",
	//           "stopped_at": "2009-11-10T23:00:00.436594+09:00",
	//           "elapsed": 145500
	//         }
	//       ]
	//     },
	//     {
	//       "id": "sub span B",
	//       "started_at": "2009-11-10T23:00:00.436303+09:00",
	//       "stopped_at": "2009-11-10T23:00:00.436883+09:00",
	//       "elapsed": 580083
	//     }
	//   ]
	// }
}
Measure elapsed time of block
func () {
	defer stopw.Start().Stop()
	// do something
}()
r := stopw.Result()
[...]
Measure separately
a := stopw.New()
b := stopw.New()

a.Start()
// do something for a
a.Stop()

b.Start()
// do something for b
b.Stop()

ra := a.Result()
rb := b.Result()

Documentation

Overview

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/k1LoW/stopw"
)

func main() {
	stopw.Start()
	stopw.Start("sub span A")
	// do something for `sub span A`
	stopw.Start("sub span B")
	// do something for `sub span A` or `sub span B`
	stopw.Start("sub span A", "sub sub span a")
	// do something for `sub span A` or `sub span B` or `sub sub span a`
	stopw.Stop("sub span A", "sub sub span a")
	// do something for `sub span A` or `sub span B`
	stopw.Stop("sub span span A")
	// do something for `sub span B`
	stopw.Stop()

	r := stopw.Result()
	b, _ := json.MarshalIndent(r, "", "  ")
	fmt.Println(string(b))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reset

func Reset()

Reset measurement result

func Stop

func Stop(ids ...any)

Stop stopwatch

func StopAt added in v0.2.0

func StopAt(end time.Time, ids ...any)

StopAt stop stopwatch by specifying the time

Types

type Span added in v0.4.0

type Span struct {
	ID        any       `json:"id,omitempty"`
	StartedAt time.Time `json:"started_at"`
	StoppedAt time.Time `json:"stopped_at"`
	Breakdown spans     `json:"breakdown,omitempty"`
	// contains filtered or unexported fields
}

func Copy added in v0.9.0

func Copy() *Span

Copy stopwatch

func Disable added in v0.6.0

func Disable() *Span

Disable stopwatch

func Enable added in v0.6.0

func Enable() *Span

Enable stopwatch

func New

func New(ids ...any) *Span

New return a new root Span

func Result

func Result() *Span

Result returns measurement result

func Start

func Start(ids ...any) *Span

Start stopwatch

func StartAt added in v0.2.0

func StartAt(start time.Time, ids ...any) *Span

StartAt start stopwatch by specifying the time

func (*Span) Copy added in v0.9.0

func (s *Span) Copy() *Span

Copy returns a copy of the stopwatch.

func (*Span) Disable added in v0.6.0

func (s *Span) Disable() *Span

Disable stopwatch

func (*Span) Elapsed added in v0.4.0

func (s *Span) Elapsed() time.Duration

func (*Span) Enable added in v0.6.0

func (s *Span) Enable() *Span

Enable stopwatch

func (*Span) IDs added in v0.4.0

func (s *Span) IDs() []any

IDs returns ID list

func (*Span) MarshalJSON added in v0.8.0

func (s *Span) MarshalJSON() ([]byte, error)

func (*Span) New added in v0.4.0

func (s *Span) New(ids ...any) *Span

New return a new breakdown span

func (*Span) Repair added in v0.7.1

func (s *Span) Repair()

func (*Span) Reset added in v0.4.0

func (s *Span) Reset()

Reset measurement result of span

func (*Span) Result added in v0.4.0

func (s *Span) Result() *Span

Result returns the result of the stopwatch.

func (*Span) Start added in v0.4.0

func (s *Span) Start(ids ...any) *Span

Start stopwatch of span

func (*Span) StartAt added in v0.4.0

func (s *Span) StartAt(start time.Time, ids ...any) *Span

StartAt start stopwatch of span by specifying the time

func (*Span) Stop added in v0.4.0

func (s *Span) Stop(ids ...any)

Stop stopwatch of span

func (*Span) StopAt added in v0.4.0

func (s *Span) StopAt(end time.Time, ids ...any)

StopAt stop stopwatch of span by specifying the time

Jump to

Keyboard shortcuts

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