4 var nsICache = Components.interfaces.nsICache;
6 function getEventQueue()
8 var nsIEventQueueService = Components.interfaces.nsIEventQueueService;
9 var CID = Components.classes["@mozilla.org/event-queue-service;1"];
10 var service = CID.getService(nsIEventQueueService);
11 return service.getSpecialEventQueue(nsIEventQueueService.CURRENT_THREAD_EVENT_QUEUE);
14 var eventQueue = getEventQueue();
16 function getCacheService()
18 var nsCacheService = Components.classes["@mozilla.org/network/cache-service;1"];
19 var service = nsCacheService.getService(Components.interfaces.nsICacheService);
23 function createCacheSession(clientID, storagePolicy, streamable)
25 var service = getCacheService();
26 var session = service.createSession(clientID, storagePolicy, streamable);
30 function openCacheEntry(url, mode)
32 var session = createCacheSession(clientID, nsICache.STORE_ON_DISK, true);
33 var entry = session.openCacheEntry(url, mode);
39 var leakDetector = Components.classes["@mozilla.org/xpcom/leakdetector;1"].getService(Components.interfaces.nsILeakDetector);
40 leakDetector.dumpLeaks();
43 function wrapInputStream(input)
45 var nsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream;
46 var factory = Components.classes["@mozilla.org/scriptableinputstream;1"];
47 var wrapper = factory.createInstance(nsIScriptableInputStream);
52 function download(key)
54 var url = new java.net.URL(key);
56 var buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 65536);
57 var stream = url.openStream();
59 var count = stream.read(buffer);
62 var str = new java.lang.String(buffer, 0, count);
69 function write(url, data)
71 var key = url.toString();
72 var outputEntry = openCacheEntry(key, nsICache.ACCESS_WRITE);
73 var output = outputEntry.transport.openOutputStream(0, -1, 0);
74 var count = output.write(data, data.length);
76 // store some metadata.
77 outputEntry.setMetaDataElement("size", data.length);
80 outputEntry.markValid();
86 function CacheListener()
91 CacheListener.prototype = {
92 QueryInterface : function(iid)
94 if (iid.equals(Components.interfaces.nsICacheListener))
96 throw Components.results.NS_NOINTERFACE;
99 onCacheEntryAvailable : function(/* in nsICacheEntryDescriptor */ descriptor,
100 /* in nsCacheAccessMode */ accessGranted,
101 /* in nsresult */ status)
103 this.descriptor = descriptor;
104 this.status = status;
109 function asyncOpenCacheEntry(url, mode)
111 var session = createCacheSession(clientID, nsICache.STORE_ON_DISK, true);
112 var listener = new CacheListener();
113 session.asyncOpenCacheEntry(url, mode, listener);
114 while (!listener.done)
115 eventQueue.processPendingEvents();
116 return listener.descriptor;
119 function asyncWrite(key, data)
121 var outputEntry = asyncOpenCacheEntry(key, nsICache.ACCESS_WRITE);
123 var output = outputEntry.transport.openOutputStream(0, -1, 0);
124 var count = output.write(data, data.length);
126 // store some metadata.
127 outputEntry.setMetaDataElement("size", data.length);
130 outputEntry.markValid();
136 function StreamListener()
143 StreamListener.prototype = {
144 QueryInterface : function(iid)
146 if (iid.equals(Components.interfaces.nsIStreamListener) ||
147 iid.equals(Components.interfaces.nsIStreamObserver))
149 throw Components.results.NS_NOINTERFACE;
152 onStartRequest : function(request, context)
156 onStopRequest : function(request, context, statusCode, statusText)
158 this.statusCode = statusCode;
162 onDataAvailable : function(request, context, input, offset, count)
164 if (this.wrapper == null)
165 this.wrapper = wrapInputStream(input);
166 input = this.wrapper;
167 this.data += input.read(count);
171 function asyncRead(key)
173 var inputEntry = asyncOpenCacheEntry(key, nsICache.ACCESS_READ);
174 var listener = new StreamListener();
175 inputEntry.transport.asyncRead(listener, null, 0, inputEntry.dataSize, 0);
176 while (!listener.done)
177 eventQueue.processPendingEvents();
179 return listener.data;
184 var inputEntry = openCacheEntry(key, nsICache.ACCESS_READ);
185 var input = wrapInputStream(inputEntry.transport.openInputStream(0, -1, 0));
186 var data = input.read(input.available());
192 function readMetaData(key, element)
194 var inputEntry = openCacheEntry(key, nsICache.ACCESS_READ);
195 var metadata = inputEntry.getMetaDataElement(element);
202 var key = url.toString();
203 var doomedEntry = openCacheEntry(key, nsICache.ACCESS_READ_WRITE);
208 // two keys which are known to collide right now.
209 var key1 = "http://a772.g.akamai.net/7/772/51/7648437e551b56/www.apple.com/t/2001/us/en/i/2.gif";
210 var key2 = "http://a772.g.akamai.net/7/772/51/70601300d0bde6/www.apple.com/t/2001/us/en/i/1right.gif";
214 // 1. generate a collision, and close the colliding entry first.
215 var entry1 = asyncOpenCacheEntry(key1, nsICache.ACCESS_READ_WRITE);
217 entry1.setMetaDataElement("size", data1.length);
219 var output1 = entry1.transport.openOutputStream(0, -1, 0);
220 output1.write(data1, data1.length);
222 var entry2 = asyncOpenCacheEntry(key2, nsICache.ACCESS_READ_WRITE);
224 entry2.setMetaDataElement("size", data2.length);
226 var output2 = entry2.transport.openOutputStream(0, -1, 0);
227 output2.write(data2, data2.length);
236 function median(array)
238 var cmp = function(x, y) { return x - y; }
240 var middle = Math.floor(array.length / 2);
241 return array[middle];
247 var len = array.length;
248 for (var i = 0; i < len; ++i)
256 var System = java.lang.System;
257 var url = new java.net.URL("http://www.mozilla.org");
258 var key = url.toString();
259 var downloadTimes = new Array();
260 for (var i = 0; i < N; ++i) {
261 var begin = System.currentTimeMillis();
263 var end = System.currentTimeMillis();
264 downloadTimes.push(end - begin);
266 var downloadTotal = sum(downloadTimes);
267 var downloadMean = downloadTotal / N;
268 var downloadMedian = median(downloadTimes);
269 print("" + N + " downloads took " + downloadTotal + " milliseconds.");
270 print("mean = " + downloadMean + " milliseconds.");
271 print("median = " + downloadMedian + " milliseconds.");
273 var readTimes = new Array();
274 for (var i = 0; i < N; ++i) {
275 var begin = System.currentTimeMillis();
277 var end = System.currentTimeMillis();
278 readTimes.push(end - begin);
280 var readTotal = sum(readTimes);
281 var readMean = readTotal / N;
282 var readMedian = median(readTimes);
283 print("" + N + " reads took " + readTotal + " milliseconds.");
284 print("mean = " + readMean + " milliseconds.");
285 print("median = " + readMedian + " milliseconds.");
288 // load the cache service before doing anything with Java...
292 print("cache service loaded.");
294 print("running disk cache test.");
296 print("disk cache test complete.");