|
jQuerySelectorCache
The Cache Mechanism of jQuery Selector.
IntroductionjQuery selector improves the speed performance in IE. jQuery cache is a mechanism to further improve the speed by reusing the found DOM reference for a given selector. But you should keep in mind that the jQuery locating time may be just a small portion of the round trip time from Tellurium core to Selenium core, thus, the improvement of jQuery cache over regular jQuery selector has upper bound. However, jQuery cache is fundamental to Tellurium because it is the first step for us to create our own Tellurium Engine. The Cache MechanismTellurium Core Cache SupportWhen you choose to use jQuery Cache mechanism, Tellurium core will pass the jQuery selector and a meta command to Tellurium Engine, which is embedded inside Selenium core at the current stage. The format is as follows taking the Tellurium issue searching UI as an example, jquerycache={
"locator":"form[method=get]:has(#can, span:contains(for), input[type=text][name=q], input[value=Search][type=submit]) #can",
"optimized":"#can",
"uid":"issueSearch.issueType",
"cacheable":true,
"unique":true
}where locator is the regular jQuery selector and optimized is the optimized jQuery selector by the jQuery selector optimizer in Tellurium core. The locator is used for child UI element to derive a partial jQuery selector from its parent. The optimized will be used for actual DOM searching. The rest three parameters are meta commands. uid is the UID for the corresponding jQuery selector and cacheable tells the Engine whether to cache the selector or not. The reason is that some UI element on the web is dynamic, for instance, the UI element inside a data grid. As a result, it may not be useful to cache the jQuery selector. The last one, unique, tells the Engine whether the jQuery selector expects multiple elements or not. This is very useful to handle the case that jQuery selector is expected to return one element, but actually returns multiple ones. In such a case, a Selenium Error will be thrown. To make the cache option configurable in tellurium UI module, Tellurium introduces an attribute cacheable to UI object, which is set to be true by default. For a Container type object, it has an extra attribute noCacheForChildren to control whether to cache its children. One example UI module is defined as follows, ui.Table(uid: "issueResultWithCache", cacheable: "true", noCacheForChildren: "true", clocator: [id: "resultstable", class: "results"], group: "true") {
......
//define table elements
//for the border column
TextBox(uid: "row: *, column: 1", clocator: [:])
TextBox(uid: "row: *, column: 8", clocator: [:])
TextBox(uid: "row: *, column: 10", clocator: [:])
//For the rest, just UrlLink
UrlLink(uid: "all", clocator: [:])
}Where the cacheable can overwrite the default value in the UI object and noCacheForChildren in the above example will force Tellurium to not cache its children. Tellurium Engine Cache DefinitionOn the Engine side the cache is defined as //global flag to decide whether to cache jQuery selectors
this.cacheSelector = false;
//cache for jQuery selectors
this.sCache = new Hashtable();
this.maxCacheSize = 50;
this.cachePolicy = discardOldCachePolicy;The cache system includes a global flag to decide whether to use the cache capability, a hash table to store the cached data, a cache size limit, and a cache eviction policy once the cache is filled up. The cached data structure is defined as //Cached Data, use uid as the key to reference it
function CacheData(){
//jQuery selector associated with the DOM reference, which is a whole selector
//without optimization so that it is easier to find the reminding selector for its children
this.selector = null;
//optimized selector for actual DOM search
this.optimized = null;
//jQuery object for DOM reference
this.reference = null;
//number of reuse
this.count = 0;
//last use time
this.timestamp = Number(new Date());
};Cache Eviction PoliciesTellurium Engine provides the following cache eviction policies:
Cache Data ValidationIt is important to know when the cached data become invalid. There are three mechanisms we can utilize here:
Right now, Tellurium Engine uses the 3rd mechanism to check if the cached data is valid or not. The first two mechanisms are still under development. Cache Work FlowWhenever Tellurium Core passes a locator to the Engine, the Engine will first look at the meta command cacheable. If this flag is true, it will first try to look up the DOM reference from the cache. If no cached DOM reference is available, do a fresh jQuery search and cache the DOM reference, otherwise, validate the cached DOM reference and use it directly. If the cacheable flag is false, the Engine will look for the UI element's ancestor by its UID and do a jQuery search starting from its ancestor if possible. jQuery Selector Cache APIsTellurium Core provides the following methods for jQuery Selector cache control,
PerformanceThe Cache Eviction PoliciesTo see how the cache eviction policies will affect the test speed, we run tests based on the Tellurium Issue Page using different cache policies. The results are illustrated in the following chart.
It looks like the DiscardOldPolicy cache policy performans the best and thus, the Engine sets the default policy as DiscardOldPolicy. For different web tests, you may like to select different cache eviction policy to fit for your need. Speed Performance of jQuery Selector CacheWe choose Ajaxslt, regular jQuery selector without cache, and jQuery selector with cache for speed comparison. The tests are categorized into four:
Where the last one is an extreme case to test the speed performance for reusable jQuery selectors. All tests are run in IE 6. The test results are listed in the following table (Unit: second),
And the results can be charted as follows,
We can observe the following facts
ConclusionsAs we said in the introduction section, the jQuery DOM search may be only a small portion of selenium command round trip time, thus, the speed improvement of jQuery selector cache is limited by that upper bound. However, jQuery selector cache is critical to Tellurium Engine and it lays the ground for Tellurium new test driving Engine. For further speed improvement, please wait for our new Tellurium Engine.
|
Sign in to add a comment