Skip to content

rtntubmt97/profiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

required go 1.14 or above

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))
}