2
0

profile.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package timeutil
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. )
  7. type ProfileDataSeries struct {
  8. Name string
  9. Series []*ProfileDatum
  10. }
  11. func NewProfileDataSeries(name string, steps int) *ProfileDataSeries {
  12. return &ProfileDataSeries{
  13. Name: name,
  14. Series: make([]*ProfileDatum, 0, steps+2),
  15. }
  16. }
  17. func (pds *ProfileDataSeries) Start() {
  18. pds.Series = append(pds.Series, &ProfileDatum{
  19. Name: "start",
  20. Time: time.Now().UTC(),
  21. })
  22. }
  23. func (pds *ProfileDataSeries) Step(name string) {
  24. pds.Series = append(pds.Series, &ProfileDatum{
  25. Name: name,
  26. Time: time.Now().UTC(),
  27. })
  28. }
  29. func (pds *ProfileDataSeries) Stop() {
  30. pds.Series = append(pds.Series, &ProfileDatum{
  31. Name: "stop",
  32. Time: time.Now().UTC(),
  33. })
  34. }
  35. func (pds *ProfileDataSeries) String() string {
  36. if pds == nil || len(pds.Series) < 2 {
  37. return "--"
  38. }
  39. var sb strings.Builder
  40. sb.WriteString(fmt.Sprintf("%s %v", pds.Name, pds.Series[len(pds.Series)-1].Time.Sub(pds.Series[0].Time)))
  41. for i := 1; i < len(pds.Series); i++ {
  42. pd := pds.Series[i]
  43. sb.WriteString(fmt.Sprintf(" [%s %v]", pd.Name, pds.Series[i].Time.Sub(pds.Series[i-1].Time)))
  44. }
  45. return sb.String()
  46. }
  47. type ProfileDatum struct {
  48. Name string
  49. Time time.Time
  50. }