interval_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tsdb
  15. import (
  16. "testing"
  17. "time"
  18. . "github.com/smartystreets/goconvey/convey"
  19. )
  20. func TestInterval(t *testing.T) {
  21. Convey("Default interval", t, func() {
  22. calculator := NewIntervalCalculator(&IntervalOptions{})
  23. Convey("for 5min", func() {
  24. tr := NewTimeRange("5m", "now")
  25. interval := calculator.Calculate(tr, time.Millisecond*1)
  26. So(interval.Text, ShouldEqual, "200ms")
  27. })
  28. Convey("for 15min", func() {
  29. tr := NewTimeRange("15m", "now")
  30. interval := calculator.Calculate(tr, time.Millisecond*1)
  31. So(interval.Text, ShouldEqual, "500ms")
  32. })
  33. Convey("for 30min", func() {
  34. tr := NewTimeRange("30m", "now")
  35. interval := calculator.Calculate(tr, time.Millisecond*1)
  36. So(interval.Text, ShouldEqual, "1s")
  37. })
  38. Convey("for 1h", func() {
  39. tr := NewTimeRange("1h", "now")
  40. interval := calculator.Calculate(tr, time.Millisecond*1)
  41. So(interval.Text, ShouldEqual, "2s")
  42. })
  43. Convey("Round interval", func() {
  44. So(roundInterval(time.Millisecond*30), ShouldEqual, time.Millisecond*20)
  45. So(roundInterval(time.Millisecond*45), ShouldEqual, time.Millisecond*50)
  46. })
  47. Convey("Format value", func() {
  48. So(FormatDuration(time.Second*61), ShouldEqual, "1m")
  49. So(FormatDuration(time.Millisecond*30), ShouldEqual, "30ms")
  50. So(FormatDuration(time.Hour*23), ShouldEqual, "23h")
  51. So(FormatDuration(time.Hour*24), ShouldEqual, "1d")
  52. So(FormatDuration(time.Hour*24*367), ShouldEqual, "1y")
  53. })
  54. })
  55. }