github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/deck/static/prow/histogram_test.ts (about)

     1  import "jasmine";
     2  import {JobHistogram, JobSample} from "./histogram";
     3  
     4  describe('JobHistogram', () => {
     5    it('should do nothing with empty input', () => {
     6      const h = new JobHistogram();
     7      const buckets = h.buckets(0, 9, 1);
     8      expect(buckets.data.length).toEqual(1);
     9      expect(buckets.data[0].length).toEqual(0);
    10    });
    11    it('should filter entries outside start end', () => {
    12      const h = new JobHistogram();
    13      h.add(new JobSample(10, 100, "failure", 0));
    14      h.add(new JobSample(10, 100, "success", 1));
    15      h.add(new JobSample(200, 10, "failure", 2));
    16      h.add(new JobSample(201, 10, "failure", -1));
    17      expect(h.buckets(0, 9, 1).data).toEqual([[]]);
    18      expect(h.buckets(0, 9, 2).data).toEqual([[], []]);
    19    });
    20    it('should create chunks for each bucket', () => {
    21      const h = new JobHistogram();
    22      const samples = [
    23        new JobSample(10, 100, "failure", 0),
    24        new JobSample(10, 101, "success", 1),
    25        new JobSample(300, 200, "success", 2),
    26        new JobSample(200, 300, "success", 3),
    27        new JobSample(200, 300, "success", 4),
    28        new JobSample(0, 500, "success", 5),
    29        new JobSample(200, 11, "failure", 6),
    30        new JobSample(201, 10, "failure", 7),
    31      ];
    32      for (const sample of samples) {
    33        h.add(sample);
    34      }
    35      const buckets = h.buckets(0, 1000, 10);
    36      expect(buckets.linearChunks(buckets.data[0], 4)).toEqual([
    37        [samples[0], samples[1]],
    38        [],
    39        [],
    40        [samples[5]],
    41      ]);
    42      expect(buckets.linearChunks(buckets.data[1], 4)).toEqual([
    43        [],
    44        [],
    45        [samples[3]],
    46      ]);
    47      expect(buckets.linearChunks(buckets.data[2], 4)).toEqual([
    48        [samples[7], samples[6]],
    49        [],
    50        [samples[4]],
    51      ]);
    52      expect(buckets.linearChunks(buckets.data[3], 4)).toEqual([
    53        [],
    54        [samples[2]],
    55      ]);
    56    });
    57    it('should create limited chunks for each bucket', () => {
    58      const h = new JobHistogram();
    59      const samples = [
    60        new JobSample(10, 100, "failure", 0),
    61        new JobSample(10, 101, "success", 1),
    62        new JobSample(300, 200, "success", 2),
    63        new JobSample(200, 300, "success", 3),
    64        new JobSample(200, 300, "success", 4),
    65        new JobSample(0, 500, "success", 5),
    66        new JobSample(200, 11, "failure", 6),
    67        new JobSample(201, 10, "failure", 7),
    68      ];
    69      for (const sample of samples) {
    70        h.add(sample);
    71      }
    72      const buckets = h.buckets(0, 1000, 10);
    73      buckets.limitMaximum(300);
    74      expect(buckets.linearChunks(buckets.data[0], 4)).toEqual([
    75        [],
    76        [samples[0], samples[1]],
    77        [],
    78        [samples[5]],
    79      ]);
    80      expect(buckets.linearChunks(buckets.data[1], 4)).toEqual([
    81        [],
    82        [],
    83        [],
    84        [samples[3]],
    85      ]);
    86      expect(buckets.linearChunks(buckets.data[2], 4)).toEqual([
    87        [samples[7], samples[6]],
    88        [],
    89        [],
    90        [samples[4]],
    91      ]);
    92      expect(buckets.linearChunks(buckets.data[3], 4)).toEqual([
    93        [],
    94        [],
    95        [samples[2]],
    96      ]);
    97    });
    98    it('should create buckets that contain the correct results', () => {
    99      const h = new JobHistogram();
   100      const samples = [
   101        new JobSample(10, 100, "failure", 0),
   102        new JobSample(10, 101, "success", 1),
   103        new JobSample(200, 11, "failure", 2),
   104        new JobSample(201, 10, "failure", -1),
   105      ];
   106      for (const sample of samples) {
   107        h.add(sample);
   108      }
   109      expect(h.buckets(0, 10, 1).data).toEqual([
   110        [samples[0], samples[1]],
   111      ]);
   112      expect(h.buckets(0, 11, 1).data).toEqual([
   113        [samples[0], samples[1]],
   114      ]);
   115      expect(h.buckets(199, 200, 1).data).toEqual([
   116        [samples[2]],
   117      ]);
   118      expect(h.buckets(0, 201, 2).data).toEqual([
   119        [samples[0], samples[1]],
   120        [samples[3], samples[2]],
   121      ]);
   122      expect(h.buckets(0, 202, 2).data).toEqual([
   123        [samples[0], samples[1]],
   124        [samples[3], samples[2]],
   125      ]);
   126  
   127      const swap = samples[0];
   128      samples[0] = samples[1];
   129      samples[1] = swap;
   130      expect(h.buckets(0, 202, 2).data).toEqual([
   131        [samples[1], samples[0]],
   132        [samples[3], samples[2]],
   133      ]);
   134    });
   135  });