Documentation
¶
Overview ¶
Package httpcost measures the CPU, memory and time cost of serving a request. Its entry point is New(Options), which returns a Handler that stores a *Metric on the Context; read it back with Get.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultOptions = Options{
CostPerSecond: 0.01,
CostPerMB: 0.001,
CostPerCPU: 0.02,
}
DefaultOptions contains the default cost configuration for the package:
- CostPerSecond: 0.01
- CostPerMB: 0.001
- CostPerCPU: 0.02
Functions ¶
func Compute ¶
func Compute(job func()) float64
Compute calculates and returns the cost of the given job.
It uses the DefaultOptions to initialize a new Computer, starts a running process measurement, executes the provided job function, and then stops the measurement. It returns the total cost as a float64.
func New ¶
New returns an Iris Handler that measures request cost using the provided Options. It starts a RunningProcess before ctx.Next() and stops it afterward, storing the resulting Metric into the request context. It should be registered before the handlers you want to measure.
Usage:
app.Use(httpcost.New(httpcost.DefaultOptions))
Types ¶
type Computer ¶
type Computer struct {
// contains filtered or unexported fields
}
Computer is responsible for calculating the cost of a job.
It holds the Options and uses them to start and stop RunningProcess measurements.
func NewComputer ¶
NewComputer returns a new Computer instance, or panics if all Options are disabled (i.e. zero cost metrics).
Usage:
computer := httpcost.NewComputer(httpcost.DefaultOptions)
func (*Computer) Start ¶
func (c *Computer) Start() *RunningProcess
Start initializes a new RunningProcess and records the current time, CPU usage, and memory usage if the respective cost metrics are enabled.
type Metric ¶
type Metric struct {
StartTime types.ISO8601 `pg:"type=timestamp" json:"start_time"`
Duration time.Duration `pg:"type=interval" json:"duration"`
MemoryUsed uint64 `pg:"type=int8" json:"memory_used"` // bytes.
NetworkUsed uint64 `pg:"type=int8" json:"network_used"` // bytes.
CpuTime float64 `pg:"type=decimal" json:"cpu_time"` // seconds.
Cost float64 `pg:"type=decimal" json:"cost"`
}
Metric holds the final measurement data and total cost.
type Options ¶
type Options struct {
// The cost per second of time to execute the job.
// Zero disables the time duration cost. DefaultOptions sets 0.01.
CostPerSecond float64 `json:"cost_per_second" yaml:"CostPerSecond" toml:"CostPerSecond"`
// The cost per MB of memory used (RAM (and Network)).
// Zero disables the memory cost. DefaultOptions sets 0.001.
CostPerMB float64 `json:"cost_per_mb" yaml:"CostPerMB" toml:"CostPerMB"`
// The cost per second of CPU time.
// Zero disables the CPU time cost. DefaultOptions sets 0.02.
CostPerCPU float64 `json:"cost_per_cpu" yaml:"CostPerCPU" toml:"CostPerCPU"`
}
Options contains the cost configuration for time, memory, CPU, and network usage.
CostPerSecond sets the cost per elapsed second. CostPerMB sets the cost per MB of used (RAM/Network) memory. CostPerCPU sets the cost per CPU-second.
A zero field disables that cost; there are no per-field defaults. DefaultOptions carries the usual values (0.01, 0.001 and 0.02). At least one cost must be above zero, or New and NewComputer panic.
type RunningProcess ¶
type RunningProcess struct {
Options *Options
StartTime time.Time // timestamp of the beginning.
StartCpuUsage float64 // seconds to perform.
StartMemoryUsage uint64 // bytes.
}
RunningProcess represents an ongoing measurement of time, CPU, and memory usage.
func (*RunningProcess) Stop ¶
func (proc *RunningProcess) Stop(netBytes int) *Metric
Stop finishes the measurement and calculates the cost.
netBytes is the total number of bytes written to the network.