toolkit/components/satchel/test/unit/perf_autocomplete.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 var testnum = 0;
michael@0 6 var fh;
michael@0 7 var fac;
michael@0 8 var prefs;
michael@0 9
michael@0 10 function countAllEntries() {
michael@0 11 let stmt = fh.DBConnection.createStatement("SELECT COUNT(*) as numEntries FROM moz_formhistory");
michael@0 12 do_check_true(stmt.executeStep());
michael@0 13 let numEntries = stmt.row.numEntries;
michael@0 14 stmt.finalize();
michael@0 15 return numEntries;
michael@0 16 }
michael@0 17
michael@0 18 function do_AC_search(searchTerm, previousResult) {
michael@0 19 var duration = 0;
michael@0 20 var searchCount = 5;
michael@0 21 var tempPrevious = null;
michael@0 22 var startTime;
michael@0 23 for (var i = 0; i < searchCount; i++) {
michael@0 24 if (previousResult !== null)
michael@0 25 tempPrevious = fac.autoCompleteSearch("searchbar-history", previousResult, null, null);
michael@0 26 startTime = Date.now();
michael@0 27 results = fac.autoCompleteSearch("searchbar-history", searchTerm, null, tempPrevious);
michael@0 28 duration += Date.now() - startTime;
michael@0 29 }
michael@0 30 dump("[autoCompleteSearch][test " + testnum + "] for '" + searchTerm + "' ");
michael@0 31 if (previousResult !== null)
michael@0 32 dump("with '" + previousResult + "' previous result ");
michael@0 33 else
michael@0 34 dump("w/o previous result ");
michael@0 35 dump("took " + duration + " ms with " + results.matchCount + " matches. ");
michael@0 36 dump("Average of " + Math.round(duration / searchCount) + " ms per search\n");
michael@0 37 return results;
michael@0 38 }
michael@0 39
michael@0 40 function run_test() {
michael@0 41 try {
michael@0 42
michael@0 43 // ===== test init =====
michael@0 44 var testfile = do_get_file("formhistory_1000.sqlite");
michael@0 45 var profileDir = dirSvc.get("ProfD", Ci.nsIFile);
michael@0 46 var results;
michael@0 47
michael@0 48 // Cleanup from any previous tests or failures.
michael@0 49 var destFile = profileDir.clone();
michael@0 50 destFile.append("formhistory.sqlite");
michael@0 51 if (destFile.exists())
michael@0 52 destFile.remove(false);
michael@0 53
michael@0 54 testfile.copyTo(profileDir, "formhistory.sqlite");
michael@0 55
michael@0 56 fh = Cc["@mozilla.org/satchel/form-history;1"].
michael@0 57 getService(Ci.nsIFormHistory2);
michael@0 58 fac = Cc["@mozilla.org/satchel/form-autocomplete;1"].
michael@0 59 getService(Ci.nsIFormAutoComplete);
michael@0 60 prefs = Cc["@mozilla.org/preferences-service;1"].
michael@0 61 getService(Ci.nsIPrefBranch);
michael@0 62
michael@0 63 timeGroupingSize = prefs.getIntPref("browser.formfill.timeGroupingSize") * 1000 * 1000;
michael@0 64 maxTimeGroupings = prefs.getIntPref("browser.formfill.maxTimeGroupings");
michael@0 65 bucketSize = prefs.getIntPref("browser.formfill.bucketSize");
michael@0 66
michael@0 67 // ===== 1 =====
michael@0 68 // Check initial state is as expected
michael@0 69 testnum++;
michael@0 70 do_check_true(fh.hasEntries);
michael@0 71 do_check_eq(1000, countAllEntries());
michael@0 72 fac.autoCompleteSearch("searchbar-history", "zzzzzzzzzz", null, null); // warm-up search
michael@0 73 do_check_true(fh.nameExists("searchbar-history"));
michael@0 74
michael@0 75 // ===== 2 =====
michael@0 76 // Search for '' with no previous result
michael@0 77 testnum++;
michael@0 78 results = do_AC_search("", null);
michael@0 79 do_check_true(results.matchCount > 0);
michael@0 80
michael@0 81 // ===== 3 =====
michael@0 82 // Search for 'r' with no previous result
michael@0 83 testnum++;
michael@0 84 results = do_AC_search("r", null);
michael@0 85 do_check_true(results.matchCount > 0);
michael@0 86
michael@0 87 // ===== 4 =====
michael@0 88 // Search for 'r' with '' previous result
michael@0 89 testnum++;
michael@0 90 results = do_AC_search("r", "");
michael@0 91 do_check_true(results.matchCount > 0);
michael@0 92
michael@0 93 // ===== 5 =====
michael@0 94 // Search for 're' with no previous result
michael@0 95 testnum++;
michael@0 96 results = do_AC_search("re", null);
michael@0 97 do_check_true(results.matchCount > 0);
michael@0 98
michael@0 99 // ===== 6 =====
michael@0 100 // Search for 're' with 'r' previous result
michael@0 101 testnum++;
michael@0 102 results = do_AC_search("re", "r");
michael@0 103 do_check_true(results.matchCount > 0);
michael@0 104
michael@0 105 // ===== 7 =====
michael@0 106 // Search for 'rea' without previous result
michael@0 107 testnum++;
michael@0 108 results = do_AC_search("rea", null);
michael@0 109 let countREA = results.matchCount;
michael@0 110
michael@0 111 // ===== 8 =====
michael@0 112 // Search for 'rea' with 're' previous result
michael@0 113 testnum++;
michael@0 114 results = do_AC_search("rea", "re");
michael@0 115 do_check_eq(countREA, results.matchCount);
michael@0 116
michael@0 117 // ===== 9 =====
michael@0 118 // Search for 'real' with 'rea' previous result
michael@0 119 testnum++;
michael@0 120 results = do_AC_search("real", "rea");
michael@0 121 let countREAL = results.matchCount;
michael@0 122 do_check_true(results.matchCount <= countREA);
michael@0 123
michael@0 124 // ===== 10 =====
michael@0 125 // Search for 'real' with 're' previous result
michael@0 126 testnum++;
michael@0 127 results = do_AC_search("real", "re");
michael@0 128 do_check_eq(countREAL, results.matchCount);
michael@0 129
michael@0 130 // ===== 11 =====
michael@0 131 // Search for 'real' with no previous result
michael@0 132 testnum++;
michael@0 133 results = do_AC_search("real", null);
michael@0 134 do_check_eq(countREAL, results.matchCount);
michael@0 135
michael@0 136
michael@0 137 } catch (e) {
michael@0 138 throw "FAILED in test #" + testnum + " -- " + e;
michael@0 139 }
michael@0 140 }

mercurial