You can use jQuery.sap.measure to measure the performance of your JavaScript code.
For each measurement, the result is a time and a duration. The time are the milliseconds (ms) from starting the measurement till its end. The duration is the effective milliseconds, pause phases are not counted here.
Category |
Description |
---|---|
javascript (default) |
Default measurement category if no category is provided |
require |
Identifies the duration of jQuery.sap.require for lazy loading of JavaScipt classes including the loading and parsing times for a class |
xmlhttprequest |
Identifies the duration of an jQuery.ajax call |
render |
Used for all rendering-related measurements that trigger core rendering of controls within the RenderManager class With the render category there comes an additional set of categories to distinguish between different phases of rendering |
control |
Identifies the duration for HTML rendering provided with the ControlRender.render method |
after |
Identifies the duration for calls on the control's onAfterRendering method |
preserve |
Identifies the duration needed to find out whether rendering can be preserved |
1. Activate performance measurement
Use URL Parameter sap-ui-measure=true to measure an initial request.
Activate measurement in the browser's console by calling jQuery.sap.measure.setActive(true)
jQuery.sap.measure.setActive(true); jQuery.sap.measure.start("myId","Measurement of myId"); jQuery.sap.log.info("foo"); jQuery.sap.measure.end("myId");
Action |
Method |
---|---|
Start measurement |
jQuery.sap.measure.start(sId,sInfo,[categories]) |
End measurement |
jQuery.sap.measure.end(sId) |
Pause |
jQuery.sap.measure.pause(sId) |
Resume |
jQuery.sap.measure.resume(sId) |
Provide a URL Parameter with categories sap-ui-measure=category1,category2
// Measure only "require" category
jQuery.sap.measure.setActive(true,"require")
jQuery.sap.measure.start("myId","Measurement of myId", ["foo"]);
If you also use the start or average method, make sure that the same categories are passed on, otherwise no measurement is started.
2. Retrieve the results
Command |
Returns |
---|---|
jQuery.sap.measure.getAllMeasurements() |
Array of all measures (running and completed) |
jQuery.sap.measure.getAllMeasurements(true) |
Array of completed measures |
jQuery.sap.measure.getAllMeasurements(false) |
Array of running (not completed) measures |
jQuery.sap.measure.getMeasurement(string) |
One specific measurement by ID |
jQuery.sap.measure.filterMeasurements(func) |
Array of all measures based on the result of the filter function (running and completed) |
jQuery.sap.measure.filterMeasurements(func, true) |
Array of completed measures based on the result of the filter function |
jQuery.sap.measure.filterMeasurements(func, false) |
Array of running measures based on the result of the filter function |
console.table(jQuery.sap.measure.getAllMeasurements(true)) //table with completed measurements
3. Interpret the results
id: string
The unique ID of the measurement as provided in the start or average method
info: string
Additional information as provided in the start or average method
duration: float
Duration or average duration in ms
count: int
Number of calls counted of an average
average: boolean
Indicates whether the result is an average
categories: string[]
Categories as provided in the start or average method
4. Clear results
To clear all measurements call the jQuery.sap.measure.clear method.
Averages
jQuery.sap.measure.setActive(true); for (var i=0;i<1000;i++) { jQuery.sap.measure.average("myId","Average of myId"); jQuery.sap.log.info("Foo " + i); jQuery.sap.measure.end("myId"); }
jQuery.sap.log.info("1000 calls: " +jQuery.sap.measure.getMeasurement("myId").count === 1000); //true jQuery.sap.log.info("Average time: " + jQuery.sap.measure.getMeasurement("myId").duration);
Measurement of Object Methods
jQuery.sap.measure.registerMethod
jQuery.sap.measure.unregisterMethod
jQuery.sap.measure.unregisterAllMethods
jQuery.sap.require("sap.m.Button"); var oButton = new sap.m.Button(); jQuery.sap.measure.registerMethod("oButton.setText", oButton, "setText", ["instance"]); //register to oButton instance on method setText jQuery.sap.measure.setActive(true,["instance"]); //measure only category "instance" for (var i=0;i<1000;i++) { oButton.setText("MyButton" + i); } jQuery.sap.measure.unregisterMethod(oButton, "setText"); // or jQuery.sap.measure.unregisterAllMethods(); jQuery.sap.measure.getAllMeasurements();
jQuery.sap.require("sap.m.Button"); jQuery.sap.measure.registerMethod("oButton.setText", sap.m.Button.prototype, "setText",["class"]); //register to sap.m.Button class on method setText jQuery.sap.measure.setActive(true,["class"]); //measure only category "class" for (var i=0;i<1000;i++) { var oButton = new sap.m.Button(); oButton.setText("MyButton" + i); } jQuery.sap.measure.unregisterMethod(oButton, "setText"); //or jQuery.sap.measure.unregisterAllMethods(); jQuery.sap.measure.getAllMeasurements();
Filtering
You can also use the categories listed above as filters for the result list or to define measurements for one or more specific categories with the filterMeasurements method.
// Filter for category1 jQuery.sap.measure.filterMeasurements(function(oMeasurement) { return oMeasurement.categories.indexOf("category1") > -1; });
// Filter for duration > 500ms jQuery.sap.measure.filterMeasurements(function(oMeasurement) { return oMeasurement.duration > 500; });