GitXplorerGitXplorer
d

stopwatch

public
35 stars
2 forks
0 issues

Commits

List of commits on branch master.
Unverified
21e9481bdc8b45a191d62fee98440b2e6e5b1346

Update readme

ddidip committed 7 years ago
Unverified
29676da95165de0fd5ebfdd8f80ea566b84d0a2f

Use defer for cleaner code.

ddidip committed 9 years ago
Unverified
79435afdab4d80be1efd9edb7edd0b30e4c4103e

better graphite example

ddidip committed 9 years ago
Unverified
28c01492b295baa3d7db1c74d70d4b5c533727e3

missing argument

ddidip committed 9 years ago
Unverified
3d7894e81e3336a5f60ca56934b18a644b0354d6

provide ability to choose which request method to instrument.

ddidip committed 9 years ago
Unverified
94eb0675617f4e903eec5b23d713db535d47f4f8

Example on how to report latency to graphite.

ddidip committed 9 years ago

README

The README file for this repository.

GoDoc license

Stopwatch

A small library to measure latency of things.

It can measure:

  1. Arbitrary closure's latency.

  2. Request latency via middleware pattern.

Five Minutes Tutorial

1. Closure

package main

import (
    "fmt"
    "github.com/didip/stopwatch"
)

func main() {
    a := 1
    f := func() {
        for i := 1; i <= 10; i++ {
            a = a + 1
        }
    }

    latency := stopwatch.Measure(f)

    fmt.Printf("Latency in nanoseconds: %v, Result: %v\n", latency, a)
}

2. Middleware

package main

import (
    "fmt"
    "github.com/didip/stopwatch"
    "net/http"
)

func HelloHandler(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("Hello, World!"))
}

func main() {
    // 1. Create a channel to receive latency result
    helloHandlerLatencyChan := make(chan int64)

    // 2. Pull latency result asynchronously.
    go func() {
        for {
            select {
            case latency := <-helloHandlerLatencyChan:
                fmt.Printf("Latency of HelloHandler in nanoseconds: %v\n", latency)
            }
        }
    }()

    fmt.Println("Starting HTTP server on :12345")
    http.Handle("/", stopwatch.LatencyFuncHandler(helloHandlerLatencyChan, []string{"GET"}, HelloHandler))
    http.ListenAndServe(":12345", nil)
}

My other Go libraries

  • Tollbooth: Simple middleware to rate-limit HTTP requests.

  • Gomet: Simple HTTP client & server long poll library for Go. Useful for receiving live updates without needing Websocket.