stopwatch

package module
v0.0.0-...-21e9481 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2017 License: MIT Imports: 2 Imported by: 4

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LatencyFuncHandler

func LatencyFuncHandler(resultChan chan int64, methods []string, nextFunc func(http.ResponseWriter, *http.Request)) http.Handler

LatencyFuncHandler is a middleware that measures latency given request handler function.

func LatencyHandler

func LatencyHandler(resultChan chan int64, methods []string, next http.Handler) http.Handler

LatencyHandler is a middleware that measures latency given http.Handler struct.

func Measure

func Measure(f func()) int64

Measure latency in nanoseconds

Types

This section is empty.

Directories

Path Synopsis
examples
closure command
middleware command

Jump to

Keyboard shortcuts

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