Skip to main content
Sign in
Snippets Groups Projects
Select Git revision
  • e1efe21897b39de3e7ec37e2b17917ab8ca03dd5
  • master default protected
  • rednatco-v2 protected
  • base-pairs-ladder
  • rednatco
  • test
  • ntc-tube-uniform-color
  • ntc-tube-missing-atoms
  • restore-vertex-array-per-program
  • watlas2
  • dnatco_new
  • cleanup-old-nodejs
  • webmmb
  • fix_auth_seq_id
  • update_deps
  • ext_dev
  • ntc_balls
  • nci-2
  • plugin
  • bugfix-0.4.5
  • nci
  • v0.5.0-dev.1
  • v0.4.5
  • v0.4.4
  • v0.4.3
  • v0.4.2
  • v0.4.1
  • v0.4.0
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • v0.3.3
  • v0.3.2
  • v0.3.1
  • v0.3.0
41 results

performance-monitor.ts

Blame
  • user avatar
    David Sehnal authored
    fdbb1fa4
    History
    performance-monitor.ts 1.70 KiB
    /*
     * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * Adapted from LiteMol
     * Copyright (c) 2016 - now David Sehnal, licensed under Apache 2.0, See LICENSE file for more info.
     */
    
    import { now } from 'mol-util/now';
    
    export class PerformanceMonitor {
        private starts = new Map<string, number>();
        private ends = new Map<string, number>();
    
        static currentTime() {
            return now();
        }
    
        start(name: string) {
            this.starts.set(name, now());
        }
    
        end(name: string) {
            this.ends.set(name, now());
        }
    
        static format(t: number) {
            if (isNaN(t)) return 'n/a';
    
            let h = Math.floor(t / (60 * 60 * 1000)),
                m = Math.floor(t / (60 * 1000) % 60),
                s = Math.floor(t / 1000 % 60),
                ms = Math.floor(t % 1000).toString();
    
            while (ms.length < 3) ms = '0' + ms;
    
            if (h > 0) return `${h}h${m}m${s}.${ms}s`;
            if (m > 0) return `${m}m${s}.${ms}s`;
            if (s > 0) return `${s}.${ms}s`;
            return `${t.toFixed(0)}ms`;
        }
    
        formatTime(name: string) {
            return PerformanceMonitor.format(this.time(name));
        }
    
        formatTimeSum(...names: string[]) {
            return PerformanceMonitor.format(this.timeSum(...names));
        }
    
        /** Returns the time in milliseconds and removes them from the cache. */
        time(name: string): number {
            let start = this.starts.get(name)!, end = this.ends.get(name)!;
    
            this.starts.delete(name);
            this.ends.delete(name);
    
            return end - start;
        }
    
        timeSum(...names: string[]) {
            let t = 0;
            for (let m of names.map(n => this.ends.get(n)! - this.starts.get(n)!)) t += m;
            return t;
        }
    }