Giter VIP home page Giter VIP logo

stopwatch's Introduction

stopwatch

This package offers a nice solution to take some measurements of the various states of your application. It is a non high-resolution timer that is designed to be fast giving you an accurate picture of how long your code paths are taking.

BuildStatus Go Report Card GoDoc

Usage

package main

import (
	"encoding/json"
	"fmt"
	"github.com/sendgrid/stopwatch"
	"time"
)

func main() {

	// Create a new StopWatch that starts off counting
	sw := stopwatch.New(0, true)

	// Optionally, format that time.Duration how you need it
	// sw.SetFormatter(func(duration time.Duration) string {
	// 	return fmt.Sprintf("%.3f", (duration.Seconds()*1000.0)/1000.0)
	// })

	// Take measurement of various states
	sw.Lap("Create File")

	// Simulate some time by sleeping
	time.Sleep(time.Millisecond * 300)
	sw.Lap("Edit File")

	time.Sleep(time.Second * 2)
	sw.Lap("Save File")

	time.Sleep(time.Second * 3)
	sw.Lap("Upload File")

	// Take a measurement with some additional metadata
	time.Sleep(time.Millisecond * 20)
	sw.LapWithData("Delete File", map[string]interface{}{
		"filename": "word.doc",
		"size":     "1024",
	})

	// Stop the timer
	sw.Stop()

	// Marshal to json
	if b, err := json.Marshal(sw); err == nil {
		fmt.Println(string(b))
	}
}

You can also use stopwatch inside different goroutines like so,

	// Create a new StopWatch that starts off counting
	sw := New(0, true)

	// Optionally, format that time.Duration how you need it
	sw.SetFormatter(func(duration time.Duration) string {
		return fmt.Sprintf("%.1f", duration.Seconds())
	})

	// Take measurement of various states
	sw.Lap("Create File")

	var wg sync.WaitGroup

	wg.Add(2)
	go func() {
		defer wg.Done()
		for i := 0; i < 2; i++ {
			time.Sleep(time.Millisecond * 200)
			task := fmt.Sprintf("task %d", i)
			sw.Lap(task)
		}
	}()

	go func() {
		defer wg.Done()
		time.Sleep(time.Second * 1)
		task := "task A"
		sw.LapWithData(task, map[string]interface{}{
			"filename": "word.doc",
		})
	}()

	// Simulate some time by sleeping
	time.Sleep(time.Second * 1)
	sw.Lap("Upload File")

	// Stop the timer
	wg.Wait()
	sw.Stop()

	// Marshal to json
	if b, err := json.Marshal(sw); err == nil {
		fmt.Println(string(b))
	}

	// Output:
	// [{"state":"Create File","time":"0.0"},{"state":"task 0","time":"0.2"},{"state":"task 1","time":"0.2"},{"state":"Upload File","time":"0.6"},{"state":"task A","time":"0.0","filename":"word.doc"}]

Sample Output in Json format

[
    {
        "state": "Create File",
        "time": "1.341"
    },
    {
        "state": "Edit File",
        "time": "300.48635"
    },
    {
        "state": "Save File",
        "time": "2.001098212"
    },
    {
        "state": "Upload File",
        "time": "3.000983896"
    },
    {
        "state": "Delete File",
        "time": "20.724059",
        "filename": "word.doc",
        "size": "1024"
    }
]

stopwatch's People

Contributors

vaskoz avatar ericychoi avatar alexus1024 avatar eddiezane avatar pbdeuchler avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.