/ benchmarks / text-editor-large-file-construction.bench.js
text-editor-large-file-construction.bench.js
1 const { TextEditor, TextBuffer } = require('atom'); 2 3 const MIN_SIZE_IN_KB = 0 * 1024; 4 const MAX_SIZE_IN_KB = 10 * 1024; 5 const SIZE_STEP_IN_KB = 1024; 6 const LINE_TEXT = 'Lorem ipsum dolor sit amet\n'; 7 const TEXT = LINE_TEXT.repeat( 8 Math.ceil((MAX_SIZE_IN_KB * 1024) / LINE_TEXT.length) 9 ); 10 11 module.exports = async ({ test }) => { 12 const data = []; 13 14 document.body.appendChild(atom.workspace.getElement()); 15 16 atom.packages.loadPackages(); 17 await atom.packages.activate(); 18 19 for (let pane of atom.workspace.getPanes()) { 20 pane.destroy(); 21 } 22 23 for ( 24 let sizeInKB = MIN_SIZE_IN_KB; 25 sizeInKB < MAX_SIZE_IN_KB; 26 sizeInKB += SIZE_STEP_IN_KB 27 ) { 28 const text = TEXT.slice(0, sizeInKB * 1024); 29 console.log(text.length / 1024); 30 31 let t0 = window.performance.now(); 32 const buffer = new TextBuffer({ text }); 33 const editor = new TextEditor({ 34 buffer, 35 autoHeight: false, 36 largeFileMode: true 37 }); 38 atom.grammars.autoAssignLanguageMode(buffer); 39 atom.workspace.getActivePane().activateItem(editor); 40 let t1 = window.performance.now(); 41 42 data.push({ 43 name: 'Opening a large file', 44 x: sizeInKB, 45 duration: t1 - t0 46 }); 47 48 const tickDurations = []; 49 for (let i = 0; i < 20; i++) { 50 await timeout(50); 51 t0 = window.performance.now(); 52 await timeout(0); 53 t1 = window.performance.now(); 54 tickDurations[i] = t1 - t0; 55 } 56 57 data.push({ 58 name: 'Max time event loop was blocked after opening a large file', 59 x: sizeInKB, 60 duration: Math.max(...tickDurations) 61 }); 62 63 t0 = window.performance.now(); 64 editor.setCursorScreenPosition( 65 editor.element.screenPositionForPixelPosition({ 66 top: 100, 67 left: 30 68 }) 69 ); 70 t1 = window.performance.now(); 71 72 data.push({ 73 name: 'Clicking the editor after opening a large file', 74 x: sizeInKB, 75 duration: t1 - t0 76 }); 77 78 t0 = window.performance.now(); 79 editor.element.setScrollTop(editor.element.getScrollTop() + 100); 80 t1 = window.performance.now(); 81 82 data.push({ 83 name: 'Scrolling down after opening a large file', 84 x: sizeInKB, 85 duration: t1 - t0 86 }); 87 88 editor.destroy(); 89 buffer.destroy(); 90 await timeout(10000); 91 } 92 93 atom.workspace.getElement().remove(); 94 95 return data; 96 }; 97 98 function timeout(duration) { 99 return new Promise(resolve => setTimeout(resolve, duration)); 100 }