profiler

module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2020 License: MIT

README

example

package main

import (
	"fmt"
	"html"
	"math/rand"
	"net/http"
	"time"

	app "github.com/rtntubmt97/profiler/pkg/applications"
	k "github.com/rtntubmt97/profiler/pkg/kernel"
)

// this default profiler will run on localhost:9081
var profiler k.Profiler = app.HttpPageProfiler()

func main() {
	http.HandleFunc("/foo", FooHandler)
	http.ListenAndServe(":9080", nil)
}

func FooHandler(rsp http.ResponseWriter, request *http.Request) {
	mark := k.CreateMark()
	defer profiler.Record("FooHandler", mark)

	// doing something in handler
	processTimeMillis := rand.Int() % 100
	time.Sleep(time.Microsecond * time.Duration(processTimeMillis))
	fmt.Fprintf(rsp, "Hello, %q", html.EscapeString(request.URL.Path))

	// process something in db
	n := rand.Int() % 5
	for i := 0; i < n; i++ {
		dbProcess()
	}
}

func dbProcess() {
	mark := k.CreateMark()
	defer profiler.Record("dbProcess", mark)

	processTimeMillis := rand.Int() % 100
	time.Sleep(time.Microsecond * time.Duration(processTimeMillis))
}

Directories

Path Synopsis
examples
pkg

Jump to

Keyboard shortcuts

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