apdex.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package internal
  2. import "time"
  3. // ApdexZone is a transaction classification.
  4. type ApdexZone int
  5. // https://en.wikipedia.org/wiki/Apdex
  6. const (
  7. ApdexNone ApdexZone = iota
  8. ApdexSatisfying
  9. ApdexTolerating
  10. ApdexFailing
  11. )
  12. // ApdexFailingThreshold calculates the threshold at which the transaction is
  13. // considered a failure.
  14. func ApdexFailingThreshold(threshold time.Duration) time.Duration {
  15. return 4 * threshold
  16. }
  17. // CalculateApdexZone calculates the apdex based on the transaction duration and
  18. // threshold.
  19. //
  20. // Note that this does not take into account whether or not the transaction
  21. // had an error. That is expected to be done by the caller.
  22. func CalculateApdexZone(threshold, duration time.Duration) ApdexZone {
  23. if duration <= threshold {
  24. return ApdexSatisfying
  25. }
  26. if duration <= ApdexFailingThreshold(threshold) {
  27. return ApdexTolerating
  28. }
  29. return ApdexFailing
  30. }
  31. func (zone ApdexZone) label() string {
  32. switch zone {
  33. case ApdexSatisfying:
  34. return "S"
  35. case ApdexTolerating:
  36. return "T"
  37. case ApdexFailing:
  38. return "F"
  39. default:
  40. return ""
  41. }
  42. }