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/consarray.js tools/profile.js tools/profile_view.js 30 31 32 function createNode(name, time, opt_parent) { 33 var node = new ProfileView.Node(name, time, time, null); 34 if (opt_parent) { 35 opt_parent.addChild(node); 36 } 37 return node; 38 } 39 40 41 (function testSorting() { 42 // 43 // Build a tree: 44 // root +--c/5 45 // | | 46 // +--a/2 +--b/3--+--d/4 47 // | | | 48 // +--a/1--+--c/1 +--d/2 49 // | | 50 // +--c/1 +--b/2 51 // 52 // So we can sort it using 2 fields: name and time. 53 var root = createNode('root', 0); 54 createNode('a', 2, root); 55 var a1 = createNode('a', 1, root); 56 createNode('c', 1, root); 57 var b3 = createNode('b', 3, a1); 58 createNode('c', 1, a1); 59 createNode('b', 2, a1); 60 createNode('c', 5, b3); 61 createNode('d', 4, b3); 62 createNode('d', 2, b3); 63 64 var view = new ProfileView(root); 65 var flatTree = []; 66 67 function fillFlatTree(node) { 68 flatTree.push(node.internalFuncName); 69 flatTree.push(node.selfTime); 70 } 71 72 view.traverse(fillFlatTree); 73 assertEquals( 74 ['root', 0, 75 'a', 2, 'a', 1, 'c', 1, 76 'b', 3, 'c', 1, 'b', 2, 77 'c', 5, 'd', 4, 'd', 2], flatTree); 78 79 function cmpStrs(s1, s2) { 80 return s1 == s2 ? 0 : (s1 < s2 ? -1 : 1); 81 } 82 83 view.sort(function(n1, n2) { 84 return cmpStrs(n1.internalFuncName, n2.internalFuncName) || 85 (n1.selfTime - n2.selfTime); 86 }); 87 88 flatTree = []; 89 view.traverse(fillFlatTree); 90 assertEquals( 91 ['root', 0, 92 'a', 1, 'a', 2, 'c', 1, 93 'b', 2, 'b', 3, 'c', 1, 94 'c', 5, 'd', 2, 'd', 4], flatTree); 95 })(); 96