1 /* 2 * Copyright (c) 2002-2025 Gargoyle Software Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * https://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 package org.htmlunit.javascript.host.performance; 16 17 import org.htmlunit.corejs.javascript.Scriptable; 18 import org.htmlunit.javascript.JavaScriptEngine; 19 import org.htmlunit.javascript.configuration.JsxClass; 20 import org.htmlunit.javascript.configuration.JsxConstructor; 21 import org.htmlunit.javascript.configuration.JsxFunction; 22 import org.htmlunit.javascript.configuration.JsxGetter; 23 import org.htmlunit.javascript.host.event.EventTarget; 24 25 /** 26 * A JavaScript object for {@code Performance}. 27 * 28 * @author Ahmed Ashour 29 * @author Ronald Brill 30 */ 31 @JsxClass 32 public class Performance extends EventTarget { 33 private PerformanceTiming timing_; 34 35 /** 36 * JavaScript constructor. 37 */ 38 @Override 39 @JsxConstructor 40 public void jsConstructor() { 41 super.jsConstructor(); 42 } 43 44 /** 45 * Returns the {@code navigation} property. 46 * @return the {@code navigation} property 47 */ 48 @JsxGetter 49 public PerformanceNavigation getNavigation() { 50 final PerformanceNavigation navigation = new PerformanceNavigation(); 51 navigation.setParentScope(getParentScope()); 52 navigation.setPrototype(getPrototype(navigation.getClass())); 53 return navigation; 54 } 55 56 /** 57 * Returns the {@code timing} property. 58 * @return the {@code timing} property 59 */ 60 @JsxGetter 61 public PerformanceTiming getTiming() { 62 if (timing_ == null) { 63 final PerformanceTiming timing = new PerformanceTiming(); 64 timing.setParentScope(getParentScope()); 65 timing.setPrototype(getPrototype(timing.getClass())); 66 timing_ = timing; 67 } 68 69 return timing_; 70 } 71 72 /** 73 * @return a timestamp 74 */ 75 @JsxFunction 76 public double now() { 77 return System.nanoTime() / 1_000_000d; 78 } 79 80 /** 81 * @return a list of all PerformanceEntry objects for the page. 82 * The list's members (entries) can be created by making performance marks 83 * or measures (for example by calling the mark() method) at explicit points in time. 84 * If you are only interested in performance entries of certain types or that have 85 * certain names, see getEntriesByType() and getEntriesByName(). 86 */ 87 @JsxFunction 88 public Scriptable getEntries() { 89 return JavaScriptEngine.newArray(this, 0); 90 } 91 92 /** 93 * @return a list of all PerformanceEntry objects for the page. 94 * The list's members (entries) can be created by making performance marks 95 * or measures (for example by calling the mark() method) at explicit points in time. 96 * If you are only interested in performance entries of certain types or that have 97 * certain names, see getEntriesByType() and getEntriesByName(). 98 */ 99 @JsxFunction 100 public Scriptable getEntriesByName() { 101 return JavaScriptEngine.newArray(this, 0); 102 } 103 104 /** 105 * @return a list of PerformanceEntry objects for a given type. The list's 106 * members (entries) can be created by making performance marks or measures 107 * (for example by calling the mark() method) at explicit points in time. 108 */ 109 @JsxFunction 110 public Scriptable getEntriesByType() { 111 return JavaScriptEngine.newArray(this, 0); 112 } 113 }