Home | History | Annotate | Download | only in tools
      1 // Copyright 2009 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 // Load source code files from <project root>/tools.
     29 // Files: tools/codemap.js tools/consarray.js tools/profile.js
     30 // Files: tools/profile_view.js
     31 
     32 
     33 function createNode(name, time, opt_parent) {
     34   var node = new ProfileView.Node(name, time, time, null);
     35   if (opt_parent) {
     36     opt_parent.addChild(node);
     37   }
     38   return node;
     39 }
     40 
     41 
     42 (function testSorting() {
     43    //
     44    // Build a tree:
     45    //   root             +--c/5
     46    //    |               |
     47    //    +--a/2  +--b/3--+--d/4
     48    //    |       |       |
     49    //    +--a/1--+--c/1  +--d/2
     50    //    |       |
     51    //    +--c/1  +--b/2
     52    //
     53    // So we can sort it using 2 fields: name and time.
     54    var root = createNode('root', 0);
     55    createNode('a', 2, root);
     56    var a1 = createNode('a', 1, root);
     57    createNode('c', 1, root);
     58    var b3 = createNode('b', 3, a1);
     59    createNode('c', 1, a1);
     60    createNode('b', 2, a1);
     61    createNode('c', 5, b3);
     62    createNode('d', 4, b3);
     63    createNode('d', 2, b3);
     64 
     65    var view = new ProfileView(root);
     66    var flatTree = [];
     67 
     68    function fillFlatTree(node) {
     69      flatTree.push(node.internalFuncName);
     70      flatTree.push(node.selfTime);
     71    }
     72 
     73    view.traverse(fillFlatTree);
     74    assertEquals(
     75      ['root', 0,
     76       'a', 2, 'a', 1, 'c', 1,
     77       'b', 3, 'c', 1, 'b', 2,
     78       'c', 5, 'd', 4, 'd', 2], flatTree);
     79 
     80    function cmpStrs(s1, s2) {
     81      return s1 == s2 ? 0 : (s1 < s2 ? -1 : 1);
     82    }
     83 
     84    view.sort(function(n1, n2) {
     85      return cmpStrs(n1.internalFuncName, n2.internalFuncName) ||
     86          (n1.selfTime - n2.selfTime);
     87    });
     88 
     89    flatTree = [];
     90    view.traverse(fillFlatTree);
     91    assertEquals(
     92      ['root', 0,
     93       'a', 1, 'a', 2, 'c', 1,
     94       'b', 2, 'b', 3, 'c', 1,
     95       'c', 5, 'd', 2, 'd', 4], flatTree);
     96 })();
     97