feat: add load testing harness, coder loadtest command (#4853)

This commit is contained in:
Dean Sheather
2022-11-03 04:30:00 +10:00
committed by GitHub
parent b1c400a7df
commit e7dd3f9378
23 changed files with 2641 additions and 6 deletions

50
coderd/httpapi/json.go Normal file
View File

@ -0,0 +1,50 @@
package httpapi
import (
"encoding/json"
"time"
"golang.org/x/xerrors"
)
// Duration wraps time.Duration and provides better JSON marshaling and
// unmarshaling. The default time.Duration marshals as an integer and only
// accepts integers when unmarshaling, which is not very user friendly as users
// cannot write durations like "1h30m".
//
// This type marshals as a string like "1h30m", and unmarshals from either a
// string or an integer.
type Duration time.Duration
var _ json.Marshaler = Duration(0)
var _ json.Unmarshaler = (*Duration)(nil)
// MarshalJSON implements json.Marshaler.
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}
// UnmarshalJSON implements json.Unmarshaler.
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
err := json.Unmarshal(b, &v)
if err != nil {
return xerrors.Errorf("unmarshal JSON value: %w", err)
}
switch value := v.(type) {
case float64:
*d = Duration(time.Duration(value))
return nil
case string:
tmp, err := time.ParseDuration(value)
if err != nil {
return xerrors.Errorf("parse duration %q: %w", value, err)
}
*d = Duration(tmp)
return nil
}
return xerrors.New("invalid duration")
}