| OLD | NEW |
| (Empty) | |
| 1 'use strict'; |
| 2 |
| 3 class LineLayoutPerfTest { |
| 4 constructor(container) { |
| 5 this.container = container; |
| 6 this.spanCount = 0; |
| 7 this.lineCount = 10000; |
| 8 this.wordCountPerLine = 20; |
| 9 this.uniqueWordCount = 100; |
| 10 this.wordLength = 8; |
| 11 this.wordSeparator = ' '; |
| 12 } |
| 13 |
| 14 run(description) { |
| 15 // Unblock onload event by scheduling with zero delay, |
| 16 // in order to avoid perf bot flakiness where the bot checks |
| 17 // for timeouts reaching onload event, see crbug.com/457194 |
| 18 window.setTimeout(() => { |
| 19 PerfTestRunner.measureTime({ |
| 20 description: description, |
| 21 run: this.measure.bind(this) |
| 22 }); |
| 23 }, 0); |
| 24 } |
| 25 |
| 26 measure() { |
| 27 var fragment = this.createFragment(); |
| 28 PerfTestRunner.forceLayout(); |
| 29 |
| 30 var now = PerfTestRunner.now(); |
| 31 this.container.appendChild(fragment); |
| 32 PerfTestRunner.forceLayout(); |
| 33 var resultTime = PerfTestRunner.now() - now; |
| 34 |
| 35 while (this.container.firstChild) |
| 36 this.container.removeChild(this.container.lastChild); |
| 37 return resultTime; |
| 38 } |
| 39 |
| 40 createFragment() { |
| 41 return TextGenerator.createFragment(this.spanCount, this.lineCount, |
| 42 this.wordCountPerLine, this.wordSeparator, |
| 43 this.createWordGenerator()); |
| 44 } |
| 45 |
| 46 createWordGenerator() { |
| 47 return TextGenerator.createWordPoolGenerator(this.uniqueWordCount, this.word
Length); |
| 48 } |
| 49 } |
| 50 |
| 51 class LongWordPerfTest extends LineLayoutPerfTest { |
| 52 constructor(container, wordLength) { |
| 53 super(container); |
| 54 this.lineCount = 1; |
| 55 this.wordCountPerLine = 1; |
| 56 this.wordLength = wordLength; |
| 57 } |
| 58 |
| 59 createWordGenerator() { |
| 60 return () => TextGenerator.createWord(this.wordLength); |
| 61 } |
| 62 } |
| 63 |
| 64 class TextGenerator { |
| 65 static createFragment(spanCount, lineCount, wordCountPerLine, wordSeparator, n
extWord) { |
| 66 if (spanCount <= 0) |
| 67 return document.createTextNode(TextGenerator.createLines(lineCount, wordCo
untPerLine, wordSeparator, nextWord)); |
| 68 |
| 69 var fragment = document.createDocumentFragment(); |
| 70 for (var elementIndex = 0; elementIndex < spanCount; elementIndex++) { |
| 71 var child = document.createElement('span'); |
| 72 child.textContent = TextGenerator.createLines(lineCount, wordCountPerLine,
wordSeparator, nextWord); |
| 73 fragment.appendChild(child); |
| 74 } |
| 75 return fragment; |
| 76 } |
| 77 |
| 78 static createLines(lineCount, wordCountPerLine, wordSeparator, nextWord) { |
| 79 var lines = []; |
| 80 for (var lineIndex = 0; lineIndex < lineCount; lineIndex++) |
| 81 lines.push(this.createLine(wordCountPerLine, wordSeparator, nextWord)); |
| 82 return lines.join('\n'); |
| 83 } |
| 84 |
| 85 static createLine(wordCountPerLine, wordSeparator, nextWord) { |
| 86 let words = []; |
| 87 for (var wordIndex = 0; wordIndex < wordCountPerLine; wordIndex++) |
| 88 words.push(nextWord()); |
| 89 return words.join(wordSeparator); |
| 90 } |
| 91 |
| 92 static createWordPoolGenerator(wordCount, wordLength) { |
| 93 var words = []; |
| 94 for (var i = 0; i < wordCount; i++) |
| 95 words.push(TextGenerator.createWord(wordLength)); |
| 96 return () => { |
| 97 return words[Math.floor(Math.random() * words.length)]; |
| 98 }; |
| 99 } |
| 100 |
| 101 static createWord(length) { |
| 102 var pieces = []; |
| 103 while (length > 0) { |
| 104 var piece = Math.random().toString(36).slice(2); |
| 105 pieces.push(piece.slice(0, length)); |
| 106 length -= piece.length; |
| 107 } |
| 108 return pieces.join(''); |
| 109 } |
| 110 } |
| OLD | NEW |