|
| 1 | +import { Canvas, ElementEvent, Rect, Group, CustomEvent } from '@antv/g'; |
| 2 | +import * as tinybench from 'tinybench'; |
| 3 | +import * as lil from 'lil-gui'; |
| 4 | +import { BenchmarkPanel, BenchmarkResult } from './benchmark-panel'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Custom Event Performance Test |
| 8 | + * Compare performance between sharing a single event instance and creating new event instances each time |
| 9 | + */ |
| 10 | +export async function customEvent(context: { canvas: Canvas; gui: lil.GUI }) { |
| 11 | + const { canvas, gui } = context; |
| 12 | + console.log(canvas); |
| 13 | + |
| 14 | + await canvas.ready; |
| 15 | + |
| 16 | + const { width, height } = canvas.getConfig(); |
| 17 | + const root = new Group(); |
| 18 | + let count = 1e4; |
| 19 | + let rects = []; |
| 20 | + |
| 21 | + // Shared event instance |
| 22 | + const sharedEvent = new CustomEvent(ElementEvent.BOUNDS_CHANGED); |
| 23 | + |
| 24 | + function render() { |
| 25 | + root.destroyChildren(); |
| 26 | + rects = []; |
| 27 | + |
| 28 | + for (let i = 0; i < count; i++) { |
| 29 | + const x = Math.random() * width; |
| 30 | + const y = Math.random() * height; |
| 31 | + const size = 10 + Math.random() * 40; |
| 32 | + |
| 33 | + const rect = new Rect({ |
| 34 | + style: { |
| 35 | + x, |
| 36 | + y, |
| 37 | + width: size, |
| 38 | + height: size, |
| 39 | + fill: 'white', |
| 40 | + stroke: '#000', |
| 41 | + lineWidth: 1, |
| 42 | + }, |
| 43 | + }); |
| 44 | + root.appendChild(rect); |
| 45 | + rects[i] = { x, y, size, el: rect }; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + render(); |
| 50 | + canvas.appendChild(root); |
| 51 | + |
| 52 | + // benchmark |
| 53 | + // ---------- |
| 54 | + const bench = new tinybench.Bench({ |
| 55 | + name: 'Custom Event Performance Comparison', |
| 56 | + time: 1e3, |
| 57 | + iterations: 100, |
| 58 | + }); |
| 59 | + |
| 60 | + // Test performance of shared event instance |
| 61 | + // Update event properties each time to simulate realistic usage |
| 62 | + bench.add('Shared Event Instance', () => { |
| 63 | + rects.forEach((rect) => { |
| 64 | + // Update event properties to simulate realistic usage |
| 65 | + sharedEvent.detail = { |
| 66 | + affectChildren: true, |
| 67 | + timestamp: performance.now(), |
| 68 | + }; |
| 69 | + rect.el.dispatchEvent(sharedEvent); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + // Test performance of creating new event instances each time |
| 74 | + bench.add('New Event Instance', () => { |
| 75 | + rects.forEach((rect) => { |
| 76 | + const event = new CustomEvent(ElementEvent.BOUNDS_CHANGED); |
| 77 | + event.detail = { |
| 78 | + affectChildren: true, |
| 79 | + timestamp: performance.now(), |
| 80 | + }; |
| 81 | + rect.el.dispatchEvent(event); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + // Test performance of creating same number of events but dispatching only once on root |
| 86 | + bench.add('Create Events, Dispatch Once on Root', () => { |
| 87 | + const events = []; |
| 88 | + // Create same number of event instances |
| 89 | + for (let i = 0; i < count; i++) { |
| 90 | + const event = new CustomEvent(ElementEvent.BOUNDS_CHANGED); |
| 91 | + event.detail = { |
| 92 | + affectChildren: true, |
| 93 | + timestamp: performance.now(), |
| 94 | + }; |
| 95 | + events.push(event); |
| 96 | + } |
| 97 | + // But dispatch only once on root |
| 98 | + root.dispatchEvent(events[0]); |
| 99 | + }); |
| 100 | + |
| 101 | + // Test performance of dispatching event on each element without sharing event instance |
| 102 | + bench.add('Dispatch on Each Element', () => { |
| 103 | + rects.forEach((rect) => { |
| 104 | + const event = new CustomEvent(ElementEvent.BOUNDS_CHANGED); |
| 105 | + event.detail = { |
| 106 | + affectChildren: true, |
| 107 | + timestamp: performance.now(), |
| 108 | + }; |
| 109 | + rect.el.dispatchEvent(event); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + // Create benchmark panel |
| 114 | + const benchmarkPanel = new BenchmarkPanel(bench.name); |
| 115 | + |
| 116 | + // Show initial status with object count |
| 117 | + benchmarkPanel.showRunningStatus(`Object Count: ${count}`); |
| 118 | + |
| 119 | + // ---------- |
| 120 | + |
| 121 | + // GUI |
| 122 | + const config = { |
| 123 | + objectCount: count, |
| 124 | + runBenchmark: async () => { |
| 125 | + benchmarkPanel.showRunningStatus(`Object Count: ${count}`); |
| 126 | + |
| 127 | + setTimeout(async () => { |
| 128 | + await bench.run(); |
| 129 | + console.log(bench.name); |
| 130 | + console.table(bench.table()); |
| 131 | + |
| 132 | + benchmarkPanel.updateResultsDisplay( |
| 133 | + bench.table() as unknown as BenchmarkResult[], |
| 134 | + ); |
| 135 | + }, 1e2); |
| 136 | + }, |
| 137 | + }; |
| 138 | + |
| 139 | + gui.add(config, 'objectCount', 100, 50000, 100).onChange((value) => { |
| 140 | + count = value; |
| 141 | + render(); |
| 142 | + }); |
| 143 | + |
| 144 | + gui.add(config, 'runBenchmark'); |
| 145 | +} |
0 commit comments