testScaleSapleRates.spec.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {scaleSampleRates} from 'sentry/views/settings/dynamicSampling/utils/scaleSampleRates';
  2. function getAverageSampleRate(items: Array<{count: number; sampleRate: number}>) {
  3. const total = items.reduce((acc, item) => acc + item.count, 0);
  4. return items.reduce((acc, item) => acc + (item.count * item.sampleRate) / total, 0);
  5. }
  6. describe('scaleSampleRates', () => {
  7. it('scales the sample rate from 0', () => {
  8. const items = [
  9. {count: 2, sampleRate: 0},
  10. {count: 4, sampleRate: 0},
  11. ];
  12. const sampleRate = 0.4;
  13. const {scaledItems} = scaleSampleRates({items, sampleRate});
  14. expect(scaledItems[0].sampleRate).toEqual(0.4);
  15. expect(scaledItems[1].sampleRate).toEqual(0.4);
  16. });
  17. it('scales the sample rate from 100', () => {
  18. const items = [
  19. {count: 2, sampleRate: 0},
  20. {count: 4, sampleRate: 0},
  21. ];
  22. const sampleRate = 0.4;
  23. const {scaledItems} = scaleSampleRates({items, sampleRate});
  24. expect(scaledItems[0].sampleRate).toEqual(0.4);
  25. expect(scaledItems[1].sampleRate).toEqual(0.4);
  26. });
  27. it('scales the sample rate up', () => {
  28. const items = [
  29. {count: 500, sampleRate: 0.6},
  30. {count: 600, sampleRate: 0.2},
  31. {count: 500, sampleRate: 0.2},
  32. ];
  33. const sampleRate = 0.2;
  34. const {scaledItems} = scaleSampleRates({items, sampleRate});
  35. expect(getAverageSampleRate(scaledItems)).toBeCloseTo(sampleRate, 15);
  36. });
  37. it('handles reducing the sample rates', () => {
  38. const items = [
  39. {count: 100, sampleRate: 0.3},
  40. {count: 200, sampleRate: 0.6},
  41. {count: 200, sampleRate: 0.9},
  42. ];
  43. const sampleRate = 0.25;
  44. const {scaledItems} = scaleSampleRates({items, sampleRate});
  45. expect(getAverageSampleRate(scaledItems)).toBeCloseTo(sampleRate, 15);
  46. });
  47. it('does not decrease sample rates which are at 0', () => {
  48. const items = [
  49. {count: 200, sampleRate: 0.6},
  50. {count: 200, sampleRate: 0.9},
  51. {count: 100, sampleRate: 0},
  52. ];
  53. const sampleRate = 0.25;
  54. const {scaledItems} = scaleSampleRates({items, sampleRate});
  55. expect(items.every(item => item.sampleRate >= 0)).toBe(true);
  56. expect(getAverageSampleRate(scaledItems)).toBeCloseTo(sampleRate, 15);
  57. });
  58. it('does not increase sample rates which are at 1', () => {
  59. const items = [
  60. {count: 100, sampleRate: 1},
  61. {count: 200, sampleRate: 0.2},
  62. {count: 200, sampleRate: 0.1},
  63. ];
  64. const sampleRate = 0.8;
  65. const {scaledItems} = scaleSampleRates({items, sampleRate});
  66. expect(items.every(item => item.sampleRate <= 1)).toBe(true);
  67. expect(getAverageSampleRate(scaledItems)).toBeCloseTo(sampleRate, 15);
  68. });
  69. });