Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"time"
"github.com/agatan/accessprof"
"github.com/agatan/timejump"
)
func main() {
// Use timejump package to mock `time.Now`.
timejump.Activate()
defer timejump.Deactivate()
timejump.Stop()
timejump.Jump(time.Date(2017, 12, 2, 0, 0, 0, 0, time.UTC))
var a accessprof.AccessProf
exampleHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("Path: %s\n", r.URL.Path)))
io.Copy(w, r.Body)
r.Body.Close()
})
handler := a.Wrap(exampleHandler, "")
server := httptest.NewServer(handler)
defer server.Close()
http.Get(server.URL)
http.Get(server.URL + "/test/123")
http.Get(server.URL + "/test/456")
http.Post(server.URL+"/test/123", "application/json", strings.NewReader("{}"))
http.Post(server.URL+"/test/789", "application/json", strings.NewReader(`{"key": "value"}`))
report := handler.Report([]*regexp.Regexp{
regexp.MustCompile(`/test/\d+`),
})
fmt.Print(report.String())
}
Output: +--------+--------+-----------+-------+-----+-----+-----+-----+-----------+-----------+-----------+-----------+ | STATUS | METHOD | PATH | COUNT | MIN | MAX | SUM | AVG | MIN(BODY) | MAX(BODY) | SUM(BODY) | AVG(BODY) | +--------+--------+-----------+-------+-----+-----+-----+-----+-----------+-----------+-----------+-----------+ | 200 | GET | / | 1 | 0s | 0s | 0s | 0s | 8 | 8 | 8 | 8.000 | | 200 | GET | /test/\d+ | 2 | 0s | 0s | 0s | 0s | 16 | 16 | 32 | 16.000 | | 200 | POST | /test/\d+ | 2 | 0s | 0s | 0s | 0s | 18 | 32 | 50 | 25.000 | +--------+--------+-----------+-------+-----+-----+-----+-----+-----------+-----------+-----------+-----------+
Index ¶
- Constants
- type AccessLog
- type AccessProf
- type Handler
- type Report
- type ReportSegment
- func (seg *ReportSegment) AggregationPath() string
- func (seg *ReportSegment) AvgBody() float64
- func (seg *ReportSegment) AvgResponseTime() time.Duration
- func (seg *ReportSegment) Count() int
- func (seg *ReportSegment) MaxBody() int
- func (seg *ReportSegment) MaxResponseTime() time.Duration
- func (seg *ReportSegment) MinBody() int
- func (seg *ReportSegment) MinResponseTime() time.Duration
- func (seg *ReportSegment) SumBody() int
- func (seg *ReportSegment) SumResponseTime() time.Duration
Examples ¶
Constants ¶
View Source
const (
DefaultFlushThreshold = 1000
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AccessProf ¶
type AccessProf struct {
// LogFile is a filepath of the log file. (if empty, accessprof holds all logs on memory)
LogFile string
FlushThreshold int
// contains filtered or unexported fields
}
func (*AccessProf) Count ¶
func (a *AccessProf) Count() int
func (*AccessProf) LoadAccessLogs ¶
func (a *AccessProf) LoadAccessLogs() ([]*AccessLog, error)
func (*AccessProf) Reset ¶
func (a *AccessProf) Reset()
type Handler ¶
type Handler struct {
// Handler is the base handler to wrap
Handler http.Handler
// ReportPath is a path of HTML reporting endpoint (ignored if empty)
ReportPath string
*AccessProf
}
type Report ¶
type Report struct {
Segments []*ReportSegment
Aggregates []*regexp.Regexp
Since time.Time
}
func (*Report) RequestCount ¶
type ReportSegment ¶
type ReportSegment struct {
Method string
Path string
PathRegexp *regexp.Regexp
Status int
AccessLogs []*AccessLog
}
func (*ReportSegment) AggregationPath ¶
func (seg *ReportSegment) AggregationPath() string
func (*ReportSegment) AvgBody ¶
func (seg *ReportSegment) AvgBody() float64
func (*ReportSegment) AvgResponseTime ¶
func (seg *ReportSegment) AvgResponseTime() time.Duration
func (*ReportSegment) Count ¶
func (seg *ReportSegment) Count() int
func (*ReportSegment) MaxBody ¶
func (seg *ReportSegment) MaxBody() int
func (*ReportSegment) MaxResponseTime ¶
func (seg *ReportSegment) MaxResponseTime() time.Duration
func (*ReportSegment) MinBody ¶
func (seg *ReportSegment) MinBody() int
func (*ReportSegment) MinResponseTime ¶
func (seg *ReportSegment) MinResponseTime() time.Duration
func (*ReportSegment) SumBody ¶
func (seg *ReportSegment) SumBody() int
func (*ReportSegment) SumResponseTime ¶
func (seg *ReportSegment) SumResponseTime() time.Duration
Click to show internal directories.
Click to hide internal directories.