Home | History | Annotate | Download | only in trace_model
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 'use strict';
      6 
      7 base.require('tracing.test_utils');
      8 base.require('tracing.trace_model.slice_group');
      9 
     10 base.unittest.testSuite('tracing.trace_model.slice_group', function() {
     11   var Slice = tracing.trace_model.Slice;
     12   var SliceGroup = tracing.trace_model.SliceGroup;
     13   var newSlice = tracing.test_utils.newSlice;
     14   var newSliceNamed = tracing.test_utils.newSliceNamed;
     15 
     16   test('basicBeginEnd', function() {
     17     var group = new SliceGroup();
     18     assertEquals(group.openSliceCount, 0);
     19     var sliceA = group.beginSlice('', 'a', 1, {a: 1});
     20     assertEquals(1, group.openSliceCount);
     21     assertEquals('a', sliceA.title);
     22     assertEquals(1, sliceA.start);
     23     assertEquals(1, sliceA.args.a);
     24 
     25     var sliceB = group.endSlice(3);
     26     assertEquals(sliceA, sliceB);
     27     assertEquals(2, sliceB.duration);
     28   });
     29 
     30   test('nestedBeginEnd', function() {
     31     var group = new SliceGroup();
     32     assertEquals(group.openSliceCount, 0);
     33     group.beginSlice('', 'a', 1);
     34     group.beginSlice('', 'b', 2);
     35     group.endSlice(2.5);
     36     group.endSlice(3);
     37 
     38     assertEquals(2, group.slices.length);
     39     assertEquals('b', group.slices[0].title);
     40     assertEquals(0.5, group.slices[0].duration);
     41 
     42     assertEquals('a', group.slices[1].title);
     43     assertEquals(2, group.slices[1].duration);
     44   });
     45 
     46   test('basicMerge', function() {
     47     var a = new SliceGroup();
     48     var b = new SliceGroup();
     49     a.beginSlice('', 'one', 1);
     50     a.endSlice(2);
     51     b.beginSlice('', 'two', 3);
     52     b.endSlice(5);
     53 
     54     var m = SliceGroup.merge(a, b);
     55     assertEquals(2, m.slices.length);
     56 
     57     assertEquals('one', m.slices[0].title);
     58     assertEquals(1, m.slices[0].start);
     59     assertEquals(1, m.slices[0].duration);
     60 
     61     assertEquals('two', m.slices[1].title);
     62     assertEquals(3, m.slices[1].start);
     63     assertEquals(2, m.slices[1].duration);
     64   });
     65 
     66   test('nestedMerge', function() {
     67     var a = new SliceGroup();
     68     var b = new SliceGroup();
     69     a.beginSlice('', 'one', 1);
     70     a.endSlice(4);
     71     b.beginSlice('', 'two', 2);
     72     b.endSlice(3);
     73 
     74     var m = SliceGroup.merge(a, b);
     75     assertEquals(2, m.slices.length);
     76 
     77     assertEquals('two', m.slices[0].title);
     78     assertEquals(2, m.slices[0].start);
     79     assertEquals(1, m.slices[0].duration);
     80 
     81     assertEquals('one', m.slices[1].title);
     82     assertEquals(1, m.slices[1].start);
     83     assertEquals(3, m.slices[1].duration);
     84   });
     85 
     86   test('startSplitMerge', function() {
     87     var a = new SliceGroup();
     88     var b = new SliceGroup();
     89     a.beginSlice('', 'one', 2);
     90     a.endSlice(4);
     91     b.beginSlice('', 'two', 1);
     92     b.endSlice(3);
     93 
     94     var m = SliceGroup.merge(a, b);
     95     assertEquals(3, m.slices.length);
     96 
     97     assertEquals('two', m.slices[0].title);
     98     assertEquals(1, m.slices[0].start);
     99     assertEquals(1, m.slices[0].duration);
    100 
    101     assertEquals('two (cont.)', m.slices[1].title);
    102     assertEquals(2, m.slices[1].start);
    103     assertEquals(1, m.slices[1].duration);
    104 
    105     assertEquals('one', m.slices[2].title);
    106     assertEquals(2, m.slices[2].start);
    107     assertEquals(2, m.slices[2].duration);
    108   });
    109 
    110   test('startSplitTwoMerge', function() {
    111     var a = new SliceGroup();
    112     var b = new SliceGroup();
    113     a.beginSlice('', 'one', 3);
    114     a.endSlice(6);
    115     b.beginSlice('', 'two', 1);
    116     b.beginSlice('', 'three', 2);
    117     b.endSlice(4);
    118     b.endSlice(5);
    119 
    120     var m = SliceGroup.merge(a, b);
    121     assertEquals(5, m.slices.length);
    122 
    123     assertEquals('three', m.slices[0].title);
    124     assertEquals(2, m.slices[0].start);
    125     assertEquals(1, m.slices[0].duration);
    126 
    127     assertEquals('two', m.slices[1].title);
    128     assertEquals(1, m.slices[1].start);
    129     assertEquals(2, m.slices[1].duration);
    130 
    131     assertEquals('three (cont.)', m.slices[2].title);
    132     assertEquals(3, m.slices[2].start);
    133     assertEquals(1, m.slices[2].duration);
    134 
    135     assertEquals('two (cont.)', m.slices[3].title);
    136     assertEquals(3, m.slices[3].start);
    137     assertEquals(2, m.slices[3].duration);
    138 
    139     assertEquals('one', m.slices[4].title);
    140     assertEquals(3, m.slices[4].start);
    141     assertEquals(3, m.slices[4].duration);
    142   });
    143 
    144   test('startSplitTwiceMerge', function() {
    145     var a = new SliceGroup();
    146     var b = new SliceGroup();
    147     a.beginSlice('', 'one', 2);
    148     a.beginSlice('', 'two', 3);
    149     a.endSlice(5);
    150     a.endSlice(6);
    151     b.beginSlice('', 'three', 1);
    152     b.endSlice(4);
    153 
    154     var m = SliceGroup.merge(a, b);
    155     assertEquals(5, m.slices.length);
    156 
    157     assertEquals('three', m.slices[0].title);
    158     assertEquals(1, m.slices[0].start);
    159     assertEquals(1, m.slices[0].duration);
    160 
    161     assertEquals('three (cont.)', m.slices[1].title);
    162     assertEquals(2, m.slices[1].start);
    163     assertEquals(1, m.slices[1].duration);
    164 
    165     assertEquals('three (cont.)', m.slices[2].title);
    166     assertEquals(3, m.slices[2].start);
    167     assertEquals(1, m.slices[2].duration);
    168 
    169     assertEquals('two', m.slices[3].title);
    170     assertEquals(3, m.slices[3].start);
    171     assertEquals(2, m.slices[3].duration);
    172 
    173     assertEquals('one', m.slices[4].title);
    174     assertEquals(2, m.slices[4].start);
    175     assertEquals(4, m.slices[4].duration);
    176   });
    177 
    178   test('endSplitMerge', function() {
    179     var a = new SliceGroup();
    180     var b = new SliceGroup();
    181     a.beginSlice('', 'one', 1);
    182     a.endSlice(3);
    183     b.beginSlice('', 'two', 2);
    184     b.endSlice(4);
    185 
    186     var m = SliceGroup.merge(a, b);
    187     assertEquals(3, m.slices.length);
    188 
    189     assertEquals('two', m.slices[0].title);
    190     assertEquals(2, m.slices[0].start);
    191     assertEquals(1, m.slices[0].duration);
    192 
    193     assertEquals('one', m.slices[1].title);
    194     assertEquals(1, m.slices[1].start);
    195     assertEquals(2, m.slices[1].duration);
    196 
    197     assertEquals('two (cont.)', m.slices[2].title);
    198     assertEquals(3, m.slices[2].start);
    199     assertEquals(1, m.slices[2].duration);
    200   });
    201 
    202   test('endSplitTwoMerge', function() {
    203     var a = new SliceGroup();
    204     var b = new SliceGroup();
    205     a.beginSlice('', 'one', 1);
    206     a.endSlice(4);
    207     b.beginSlice('', 'two', 2);
    208     b.beginSlice('', 'three', 3);
    209     b.endSlice(5);
    210     b.endSlice(6);
    211 
    212     var m = SliceGroup.merge(a, b);
    213     assertEquals(5, m.slices.length);
    214 
    215     assertEquals('three', m.slices[0].title);
    216     assertEquals(3, m.slices[0].start);
    217     assertEquals(1, m.slices[0].duration);
    218 
    219     assertEquals('two', m.slices[1].title);
    220     assertEquals(2, m.slices[1].start);
    221     assertEquals(2, m.slices[1].duration);
    222 
    223     assertEquals('one', m.slices[2].title);
    224     assertEquals(1, m.slices[2].start);
    225     assertEquals(3, m.slices[2].duration);
    226 
    227     assertEquals('three (cont.)', m.slices[3].title);
    228     assertEquals(4, m.slices[3].start);
    229     assertEquals(1, m.slices[3].duration);
    230 
    231     assertEquals('two (cont.)', m.slices[4].title);
    232     assertEquals(4, m.slices[4].start);
    233     assertEquals(2, m.slices[4].duration);
    234   });
    235 
    236   test('endSplitTwiceMerge', function() {
    237     var a = new SliceGroup();
    238     var b = new SliceGroup();
    239     a.beginSlice('', 'one', 1);
    240     a.beginSlice('', 'two', 2);
    241     a.endSlice(4);
    242     a.endSlice(5);
    243     b.beginSlice('', 'three', 3);
    244     b.endSlice(6);
    245 
    246     var m = SliceGroup.merge(a, b);
    247     assertEquals(5, m.slices.length);
    248 
    249     assertEquals('three', m.slices[0].title);
    250     assertEquals(3, m.slices[0].start);
    251     assertEquals(1, m.slices[0].duration);
    252 
    253     assertEquals('two', m.slices[1].title);
    254     assertEquals(2, m.slices[1].start);
    255     assertEquals(2, m.slices[1].duration);
    256 
    257     assertEquals('three (cont.)', m.slices[2].title);
    258     assertEquals(4, m.slices[2].start);
    259     assertEquals(1, m.slices[2].duration);
    260 
    261     assertEquals('one', m.slices[3].title);
    262     assertEquals(1, m.slices[3].start);
    263     assertEquals(4, m.slices[3].duration);
    264 
    265     assertEquals('three (cont.)', m.slices[4].title);
    266     assertEquals(5, m.slices[4].start);
    267     assertEquals(1, m.slices[4].duration);
    268   });
    269 
    270   // Input:
    271   // A:  |    one     |       |     two     |
    272   //
    273   // B:       |         three         |
    274   //
    275   // Output:
    276   //     |    one     | three |     two     |
    277   //          | three |       | three |
    278   test('splitTwiceMerge', function() {
    279     var a = new SliceGroup();
    280     var b = new SliceGroup();
    281     a.beginSlice('', 'one', 1);
    282     a.endSlice(3);
    283     a.beginSlice('', 'two', 4);
    284     a.endSlice(6);
    285     b.beginSlice('', 'three', 2);
    286     b.endSlice(5);
    287 
    288     var m = SliceGroup.merge(a, b);
    289     assertEquals(5, m.slices.length);
    290 
    291     assertEquals('three', m.slices[0].title);
    292     assertEquals(2, m.slices[0].start);
    293     assertEquals(1, m.slices[0].duration);
    294 
    295     assertEquals('one', m.slices[1].title);
    296     assertEquals(1, m.slices[1].start);
    297     assertEquals(2, m.slices[1].duration);
    298 
    299     assertEquals('three (cont.)', m.slices[2].title);
    300     assertEquals(3, m.slices[2].start);
    301     assertEquals(1, m.slices[2].duration);
    302 
    303     assertEquals('three (cont.)', m.slices[3].title);
    304     assertEquals(4, m.slices[3].start);
    305     assertEquals(1, m.slices[3].duration);
    306 
    307     assertEquals('two', m.slices[4].title);
    308     assertEquals(4, m.slices[4].start);
    309     assertEquals(2, m.slices[4].duration);
    310   });
    311 
    312   test('bounds', function() {
    313     var group = new SliceGroup();
    314     group.updateBounds();
    315     assertEquals(group.bounds.min, undefined);
    316     assertEquals(group.bounds.max, undefined);
    317 
    318     group.pushSlice(newSlice(1, 3));
    319     group.pushSlice(newSlice(7, 2));
    320     group.updateBounds();
    321     assertEquals(1, group.bounds.min);
    322     assertEquals(9, group.bounds.max);
    323   });
    324 
    325   test('boundsWithPartial', function() {
    326     var group = new SliceGroup();
    327     group.beginSlice('', 'a', 7);
    328     group.updateBounds();
    329     assertEquals(7, group.bounds.min);
    330     assertEquals(7, group.bounds.max);
    331   });
    332 
    333   test('boundsWithTwoPartials', function() {
    334     var group = new SliceGroup();
    335     group.beginSlice('', 'a', 0);
    336     group.beginSlice('', 'a', 1);
    337     group.updateBounds();
    338     assertEquals(0, group.bounds.min);
    339     assertEquals(1, group.bounds.max);
    340   });
    341 
    342   test('boundsWithBothPartialAndRegular', function() {
    343     var group = new SliceGroup();
    344     group.updateBounds();
    345     assertEquals(undefined, group.bounds.min);
    346     assertEquals(undefined, group.bounds.max);
    347 
    348     group.pushSlice(newSlice(1, 3));
    349     group.beginSlice('', 'a', 7);
    350     group.updateBounds();
    351     assertEquals(1, group.bounds.min);
    352     assertEquals(7, group.bounds.max);
    353   });
    354 
    355   test('autocloserBasic', function() {
    356     var group = new SliceGroup();
    357     assertEquals(group.openSliceCount, 0);
    358 
    359     group.pushSlice(newSliceNamed('a', 1, 0.5));
    360 
    361     group.beginSlice('', 'b', 2);
    362     group.beginSlice('', 'c', 2.5);
    363     group.endSlice(3);
    364 
    365     group.autoCloseOpenSlices();
    366     group.updateBounds();
    367 
    368     assertEquals(1, group.bounds.min);
    369     assertEquals(3, group.bounds.max);
    370     assertEquals(3, group.slices.length);
    371     assertEquals('a', group.slices[0].title);
    372     assertEquals('c', group.slices[1].title);
    373     assertEquals('b', group.slices[2].title);
    374     assertTrue(group.slices[2].didNotFinish);
    375     assertEquals(1, group.slices[2].duration);
    376   });
    377 
    378   test('autocloserWithSubTasks', function() {
    379     var group = new SliceGroup();
    380     assertEquals(group.openSliceCount, 0);
    381 
    382     group.beginSlice('', 'a', 1);
    383     group.beginSlice('', 'b1', 2);
    384     group.endSlice(3);
    385     group.beginSlice('', 'b2', 3);
    386 
    387     group.autoCloseOpenSlices();
    388     assertEquals(3, group.slices.length);
    389     assertEquals('b1', group.slices[0].title);
    390 
    391     assertEquals('b2', group.slices[1].title);
    392     assertTrue(group.slices[1].didNotFinish);
    393     assertEquals(0, group.slices[1].duration);
    394 
    395     assertEquals('a', group.slices[2].title);
    396     assertTrue(group.slices[2].didNotFinish);
    397     assertEquals(2, group.slices[2].duration);
    398   });
    399 
    400   // TODO: test cretion of subSlices
    401 });
    402