head_localdatabaselibraryperf.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
31 var SB_NS = "http://songbirdnest.com/data/1.0#";
32 
33 // Make sure the db engine isn't taking any shortcuts
34 var dbe = Cc["@songbirdnest.com/Songbird/DatabaseEngine;1"]
35  .getService(Ci.sbIDatabaseEngine);
36 var localeCollationPreviouslyEnabled = dbe.localeCollationEnabled;
37 dbe.localeCollationEnabled = true;
38 
39 
40 function runPerfTest(aName, aTestFunc) {
41 
42  var environment = Cc["@mozilla.org/process/environment;1"]
43  .getService(Ci.nsIEnvironment);
44  var resultFile = environment.get("SB_PERF_RESULTS");
45  if (!resultFile) {
46  log("DBPERF: " + aName + " ignored, since SB_PERF_RESULTS is not set.");
47  return;
48  }
49 
50  var library = getLibrary();
51  var timer = new Timer();
52  aTestFunc.apply(this, [library, timer]);
53 
54  log("DBPERF: " + aName + " " + library.databaseGuid + " " +
55  library.length + " " + timer.elapsed() + "ms");
56 
57  var outputFile = Cc["@mozilla.org/file/local;1"]
58  .createInstance(Ci.nsILocalFile);
59  outputFile.initWithPath(resultFile);
60 
61  var fos = Cc["@mozilla.org/network/file-output-stream;1"]
62  .createInstance(Ci.nsIFileOutputStream);
63  var s = aName + "\t" + library.databaseGuid + "\t" +
64  library.length + "\t" + timer.elapsed() + "\n";
65  // Open writeonly, createfile, append, with rw permissions for everyone
66  fos.init(outputFile, 0x02 | 0x08 | 0x10, 0666, 0);
67  fos.write(s, s.length);
68  fos.close();
69 }
70 
71 function getLibrary() {
72  var environment = Cc["@mozilla.org/process/environment;1"]
73  .getService(Ci.nsIEnvironment);
74 
75  var libraryFile;
76  if (!environment.exists("SB_PERF_LIBRARY")) {
77  // If no library specified, just use the main library.
78  Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
79  return LibraryUtils.mainLibrary;
80  }
81 
82  libraryFile = environment.get("SB_PERF_LIBRARY");
83 
84  var file = Components.classes["@mozilla.org/file/local;1"]
85  .createInstance(Ci.nsILocalFile);
86  file.initWithPath(libraryFile);
87 
88  var libraryFactory =
89  Cc["@songbirdnest.com/Songbird/Library/LocalDatabase/LibraryFactory;1"]
90  .getService(Ci.sbILibraryFactory);
91  var hashBag = Cc["@mozilla.org/hash-property-bag;1"].
92  createInstance(Ci.nsIWritablePropertyBag2);
93  hashBag.setPropertyAsInterface("databaseFile", file);
94  return libraryFactory.createLibrary(hashBag);
95 }
96 
97 function Timer() {
98 }
99 
100 Timer.prototype = {
101  _startTime: null,
102  _stopTime: null,
103  start: function() {
104  this._startTime = Date.now();
105  },
106  stop: function() {
107  this._stopTime = Date.now();
108  },
109  elapsed: function() {
110  return this._stopTime - this._startTime;
111  }
112 }
113 
114 function newGuidArray(aLibrary) {
115  var ldbl = aLibrary.QueryInterface(Ci.sbILocalDatabaseLibrary);
116  var array = Cc["@songbirdnest.com/Songbird/Library/LocalDatabase/GUIDArray;1"]
117  .createInstance(Ci.sbILocalDatabaseGUIDArray);
118  array.databaseGUID = ldbl.databaseGuid;
119  array.propertyCache = ldbl.propertyCache;
120  array.baseTable = "media_items";
121  array.fetchSize = 1000;
122 
123  return array;
124 }
125 
126 function getFile(fileName) {
127  var file = Cc["@mozilla.org/file/directory_service;1"]
128  .getService(Ci.nsIProperties)
129  .get("resource:app", Ci.nsIFile);
130  file = file.clone();
131  file.append("testharness");
132  file.append("localdatabaselibrary");
133  file.append(fileName);
134  return file;
135 }
136 
137 function StringArrayEnumerator(aArray) {
138  this._array = aArray;
139  this._current = 0;
140 }
141 
142 StringArrayEnumerator.prototype.hasMore = function() {
143  return this._current < this._array.length;
144 }
145 
146 StringArrayEnumerator.prototype.getNext = function() {
147  return this._array[this._current++];
148 }
149 
150 StringArrayEnumerator.prototype.QueryInterface = function(iid) {
151  if (!iid.equals(Components.interfaces.nsIStringEnumerator) &&
152  !iid.equals(Components.interfaces.nsISupports))
153  throw Components.results.NS_ERROR_NO_INTERFACE;
154  return this;
155 };
function start(ch)
const Cc
function getLibrary()
function stop(ch, cx, status, data)
inArray array
function log(s)
sidebarFactory createInstance
Definition: nsSidebar.js:351
function runPerfTest(aName, aTestFunc)
function getFile(fileName)
function Timer()
return null
Definition: FeedWriter.js:1143
_updateCookies aName
var localeCollationPreviouslyEnabled
function StringArrayEnumerator(aArray)
const Ci
Javascript wrappers for common library tasks.
var SB_NS
Some globally useful stuff for the local database library tests.
var file