1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/satchel/test/unit/perf_autocomplete.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,140 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +var testnum = 0; 1.9 +var fh; 1.10 +var fac; 1.11 +var prefs; 1.12 + 1.13 +function countAllEntries() { 1.14 + let stmt = fh.DBConnection.createStatement("SELECT COUNT(*) as numEntries FROM moz_formhistory"); 1.15 + do_check_true(stmt.executeStep()); 1.16 + let numEntries = stmt.row.numEntries; 1.17 + stmt.finalize(); 1.18 + return numEntries; 1.19 +} 1.20 + 1.21 +function do_AC_search(searchTerm, previousResult) { 1.22 + var duration = 0; 1.23 + var searchCount = 5; 1.24 + var tempPrevious = null; 1.25 + var startTime; 1.26 + for (var i = 0; i < searchCount; i++) { 1.27 + if (previousResult !== null) 1.28 + tempPrevious = fac.autoCompleteSearch("searchbar-history", previousResult, null, null); 1.29 + startTime = Date.now(); 1.30 + results = fac.autoCompleteSearch("searchbar-history", searchTerm, null, tempPrevious); 1.31 + duration += Date.now() - startTime; 1.32 + } 1.33 + dump("[autoCompleteSearch][test " + testnum + "] for '" + searchTerm + "' "); 1.34 + if (previousResult !== null) 1.35 + dump("with '" + previousResult + "' previous result "); 1.36 + else 1.37 + dump("w/o previous result "); 1.38 + dump("took " + duration + " ms with " + results.matchCount + " matches. "); 1.39 + dump("Average of " + Math.round(duration / searchCount) + " ms per search\n"); 1.40 + return results; 1.41 +} 1.42 + 1.43 +function run_test() { 1.44 + try { 1.45 + 1.46 + // ===== test init ===== 1.47 + var testfile = do_get_file("formhistory_1000.sqlite"); 1.48 + var profileDir = dirSvc.get("ProfD", Ci.nsIFile); 1.49 + var results; 1.50 + 1.51 + // Cleanup from any previous tests or failures. 1.52 + var destFile = profileDir.clone(); 1.53 + destFile.append("formhistory.sqlite"); 1.54 + if (destFile.exists()) 1.55 + destFile.remove(false); 1.56 + 1.57 + testfile.copyTo(profileDir, "formhistory.sqlite"); 1.58 + 1.59 + fh = Cc["@mozilla.org/satchel/form-history;1"]. 1.60 + getService(Ci.nsIFormHistory2); 1.61 + fac = Cc["@mozilla.org/satchel/form-autocomplete;1"]. 1.62 + getService(Ci.nsIFormAutoComplete); 1.63 + prefs = Cc["@mozilla.org/preferences-service;1"]. 1.64 + getService(Ci.nsIPrefBranch); 1.65 + 1.66 + timeGroupingSize = prefs.getIntPref("browser.formfill.timeGroupingSize") * 1000 * 1000; 1.67 + maxTimeGroupings = prefs.getIntPref("browser.formfill.maxTimeGroupings"); 1.68 + bucketSize = prefs.getIntPref("browser.formfill.bucketSize"); 1.69 + 1.70 + // ===== 1 ===== 1.71 + // Check initial state is as expected 1.72 + testnum++; 1.73 + do_check_true(fh.hasEntries); 1.74 + do_check_eq(1000, countAllEntries()); 1.75 + fac.autoCompleteSearch("searchbar-history", "zzzzzzzzzz", null, null); // warm-up search 1.76 + do_check_true(fh.nameExists("searchbar-history")); 1.77 + 1.78 + // ===== 2 ===== 1.79 + // Search for '' with no previous result 1.80 + testnum++; 1.81 + results = do_AC_search("", null); 1.82 + do_check_true(results.matchCount > 0); 1.83 + 1.84 + // ===== 3 ===== 1.85 + // Search for 'r' with no previous result 1.86 + testnum++; 1.87 + results = do_AC_search("r", null); 1.88 + do_check_true(results.matchCount > 0); 1.89 + 1.90 + // ===== 4 ===== 1.91 + // Search for 'r' with '' previous result 1.92 + testnum++; 1.93 + results = do_AC_search("r", ""); 1.94 + do_check_true(results.matchCount > 0); 1.95 + 1.96 + // ===== 5 ===== 1.97 + // Search for 're' with no previous result 1.98 + testnum++; 1.99 + results = do_AC_search("re", null); 1.100 + do_check_true(results.matchCount > 0); 1.101 + 1.102 + // ===== 6 ===== 1.103 + // Search for 're' with 'r' previous result 1.104 + testnum++; 1.105 + results = do_AC_search("re", "r"); 1.106 + do_check_true(results.matchCount > 0); 1.107 + 1.108 + // ===== 7 ===== 1.109 + // Search for 'rea' without previous result 1.110 + testnum++; 1.111 + results = do_AC_search("rea", null); 1.112 + let countREA = results.matchCount; 1.113 + 1.114 + // ===== 8 ===== 1.115 + // Search for 'rea' with 're' previous result 1.116 + testnum++; 1.117 + results = do_AC_search("rea", "re"); 1.118 + do_check_eq(countREA, results.matchCount); 1.119 + 1.120 + // ===== 9 ===== 1.121 + // Search for 'real' with 'rea' previous result 1.122 + testnum++; 1.123 + results = do_AC_search("real", "rea"); 1.124 + let countREAL = results.matchCount; 1.125 + do_check_true(results.matchCount <= countREA); 1.126 + 1.127 + // ===== 10 ===== 1.128 + // Search for 'real' with 're' previous result 1.129 + testnum++; 1.130 + results = do_AC_search("real", "re"); 1.131 + do_check_eq(countREAL, results.matchCount); 1.132 + 1.133 + // ===== 11 ===== 1.134 + // Search for 'real' with no previous result 1.135 + testnum++; 1.136 + results = do_AC_search("real", null); 1.137 + do_check_eq(countREAL, results.matchCount); 1.138 + 1.139 + 1.140 + } catch (e) { 1.141 + throw "FAILED in test #" + testnum + " -- " + e; 1.142 + } 1.143 +}