gitea源码

init.ts 1.1KB

123456789101112131415161718192021222324252627
  1. export class InitPerformanceTracer {
  2. results: {name: string, dur: number}[] = [];
  3. recordCall(name: string, func: ()=>void) {
  4. const start = performance.now();
  5. func();
  6. this.results.push({name, dur: performance.now() - start});
  7. }
  8. printResults() {
  9. this.results = this.results.sort((a, b) => b.dur - a.dur);
  10. for (let i = 0; i < 20 && i < this.results.length; i++) {
  11. console.info(`performance trace: ${this.results[i].name} ${this.results[i].dur.toFixed(3)}`);
  12. }
  13. }
  14. }
  15. export function callInitFunctions(functions: (() => any)[]): InitPerformanceTracer | null {
  16. // Start performance trace by accessing a URL by "https://localhost/?_ui_performance_trace=1" or "https://localhost/?key=value&_ui_performance_trace=1"
  17. // It is a quick check, no side effect so no need to do slow URL parsing.
  18. const perfTracer = !window.location.search.includes('_ui_performance_trace=1') ? null : new InitPerformanceTracer();
  19. if (perfTracer) {
  20. for (const func of functions) perfTracer.recordCall(func.name, func);
  21. } else {
  22. for (const func of functions) func();
  23. }
  24. return perfTracer;
  25. }