aggregation_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package costmodel
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/pkg/util"
  5. )
  6. func TestScaleHourlyCostData(t *testing.T) {
  7. costData := map[string]*CostData{}
  8. start := 1570000000
  9. oneHour := 60 * 60
  10. generateVectorSeries := func(start, count, interval int, value float64) []*util.Vector {
  11. vs := []*util.Vector{}
  12. for i := 0; i < count; i++ {
  13. v := &util.Vector{
  14. Timestamp: float64(start + (i * oneHour)),
  15. Value: value,
  16. }
  17. vs = append(vs, v)
  18. }
  19. return vs
  20. }
  21. costData["default"] = &CostData{
  22. RAMReq: generateVectorSeries(start, 100, oneHour, 1.0),
  23. RAMUsed: generateVectorSeries(start, 0, oneHour, 1.0),
  24. RAMAllocation: generateVectorSeries(start, 100, oneHour, 107.226),
  25. CPUReq: generateVectorSeries(start, 100, oneHour, 0.00317),
  26. CPUUsed: generateVectorSeries(start, 95, oneHour, 1.0),
  27. CPUAllocation: generateVectorSeries(start, 2, oneHour, 123.456),
  28. PVCData: []*PersistentVolumeClaimData{
  29. {Values: generateVectorSeries(start, 100, oneHour, 1.34)},
  30. },
  31. }
  32. compressedData := ScaleHourlyCostData(costData, 10)
  33. act, ok := compressedData["default"]
  34. if !ok {
  35. t.Errorf("compressed data should have key \"default\"")
  36. }
  37. // RAMReq
  38. if len(act.RAMReq) != 10 {
  39. t.Errorf("expected RAMReq to have length %d, was actually %d", 10, len(act.RAMReq))
  40. }
  41. for _, val := range act.RAMReq {
  42. if val.Value != 10.0 {
  43. t.Errorf("expected each RAMReq Vector to have Value %f, was actually %f", 10.0, val.Value)
  44. }
  45. }
  46. // RAMUsed
  47. if len(act.RAMUsed) != 0 {
  48. t.Errorf("expected RAMUsed to have length %d, was actually %d", 0, len(act.RAMUsed))
  49. }
  50. // RAMAllocation
  51. if len(act.RAMAllocation) != 10 {
  52. t.Errorf("expected RAMAllocation to have length %d, was actually %d", 10, len(act.RAMAllocation))
  53. }
  54. for _, val := range act.RAMAllocation {
  55. if val.Value != 1072.26 {
  56. t.Errorf("expected each RAMAllocation Vector to have Value %f, was actually %f", 1072.26, val.Value)
  57. }
  58. }
  59. // CPUReq
  60. if len(act.CPUReq) != 10 {
  61. t.Errorf("expected CPUReq to have length %d, was actually %d", 10, len(act.CPUReq))
  62. }
  63. for _, val := range act.CPUReq {
  64. if val.Value != 0.0317 {
  65. t.Errorf("expected each CPUReq Vector to have Value %f, was actually %f", 0.0317, val.Value)
  66. }
  67. }
  68. // CPUUsed
  69. if len(act.CPUUsed) != 10 {
  70. t.Errorf("expected CPUUsed to have length %d, was actually %d", 10, len(act.CPUUsed))
  71. }
  72. for _, val := range act.CPUUsed[:len(act.CPUUsed)-1] {
  73. if val.Value != 10.0 {
  74. t.Errorf("expected each CPUUsed Vector to have Value %f, was actually %f", 10.0, val.Value)
  75. }
  76. }
  77. if act.CPUUsed[len(act.CPUUsed)-1].Value != 5.0 {
  78. t.Errorf("expected each CPUUsed Vector to have Value %f, was actually %f", 5.0, act.CPUUsed[len(act.CPUUsed)-1].Value)
  79. }
  80. // CPUAllocation
  81. if len(act.CPUAllocation) != 1 {
  82. t.Errorf("expected CPUAllocation to have length %d, was actually %d", 1, len(act.CPUAllocation))
  83. }
  84. if act.CPUAllocation[0].Value != 246.912 {
  85. t.Errorf("expected each CPUAllocation Vector to have Value %f, was actually %f", 246.912, act.CPUAllocation[len(act.CPUAllocation)-1].Value)
  86. }
  87. // PVCData
  88. if len(act.PVCData[0].Values) != 10 {
  89. t.Errorf("expected PVCData[0] to have length %d, was actually %d", 10, len(act.PVCData[0].Values))
  90. }
  91. for _, val := range act.PVCData[0].Values {
  92. if val.Value != 13.4 {
  93. t.Errorf("expected each PVCData[0] Vector to have Value %f, was actually %f", 13.4, val.Value)
  94. }
  95. }
  96. costData["default"] = &CostData{
  97. RAMReq: generateVectorSeries(start, 100, oneHour, 1.0),
  98. RAMUsed: generateVectorSeries(start, 0, oneHour, 1.0),
  99. RAMAllocation: generateVectorSeries(start, 100, oneHour, 107.226),
  100. CPUReq: generateVectorSeries(start, 100, oneHour, 0.00317),
  101. CPUUsed: generateVectorSeries(start, 95, oneHour, 1.0),
  102. CPUAllocation: generateVectorSeries(start, 2, oneHour, 124.6),
  103. PVCData: []*PersistentVolumeClaimData{
  104. {Values: generateVectorSeries(start, 100, oneHour, 1.34)},
  105. },
  106. }
  107. scaledData := ScaleHourlyCostData(costData, 0.1)
  108. act, ok = scaledData["default"]
  109. if !ok {
  110. t.Errorf("scaled data should have key \"default\"")
  111. }
  112. // RAMReq
  113. if len(act.RAMReq) != 100 {
  114. t.Errorf("expected RAMReq to have length %d, was actually %d", 100, len(act.RAMReq))
  115. }
  116. for _, val := range act.RAMReq {
  117. if val.Value != 0.1 {
  118. t.Errorf("expected each RAMReq Vector to have Value %f, was actually %f", 0.1, val.Value)
  119. }
  120. }
  121. // RAMUsed
  122. if len(act.RAMUsed) != 0 {
  123. t.Errorf("expected RAMUsed to have length %d, was actually %d", 0, len(act.RAMUsed))
  124. }
  125. // RAMAllocation
  126. if len(act.RAMAllocation) != 100 {
  127. t.Errorf("expected RAMAllocation to have length %d, was actually %d", 100, len(act.RAMAllocation))
  128. }
  129. for _, val := range act.RAMAllocation {
  130. if val.Value != 10.7226 {
  131. t.Errorf("expected each RAMAllocation Vector to have Value %f, was actually %f", 10.7226, val.Value)
  132. }
  133. }
  134. // CPUReq
  135. if len(act.CPUReq) != 100 {
  136. t.Errorf("expected CPUReq to have length %d, was actually %d", 100, len(act.CPUReq))
  137. }
  138. for _, val := range act.CPUReq {
  139. if val.Value != 0.000317 {
  140. t.Errorf("expected each CPUReq Vector to have Value %f, was actually %f", 0.000317, val.Value)
  141. }
  142. }
  143. // CPUUsed
  144. if len(act.CPUUsed) != 95 {
  145. t.Errorf("expected CPUUsed to have length %d, was actually %d", 95, len(act.CPUUsed))
  146. }
  147. for _, val := range act.CPUUsed {
  148. if val.Value != 0.1 {
  149. t.Errorf("expected each CPUUsed Vector to have Value %f, was actually %f", 0.1, val.Value)
  150. }
  151. }
  152. // CPUAllocation
  153. if len(act.CPUAllocation) != 2 {
  154. t.Errorf("expected CPUAllocation to have length %d, was actually %d", 2, len(act.CPUAllocation))
  155. }
  156. for _, val := range act.CPUAllocation {
  157. if val.Value != 12.46 {
  158. t.Errorf("expected each CPUAllocation Vector to have Value %f, was actually %f", 12.46, val.Value)
  159. }
  160. }
  161. // PVCData
  162. if len(act.PVCData[0].Values) != 100 {
  163. t.Errorf("expected PVCData[0] to have length %d, was actually %d", 100, len(act.PVCData[0].Values))
  164. }
  165. for _, val := range act.PVCData[0].Values {
  166. if val.Value != .134 {
  167. t.Errorf("expected each PVCData[0] Vector to have Value %f, was actually %f", .134, val.Value)
  168. }
  169. }
  170. }