3 var clientID
= "javascript";
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(url
)
55 var buffer
= java
.lang
.reflect
.Array
.newInstance(java
.lang
.Byte
.TYPE
, 65536);
56 var stream
= url
.getContent();
58 var count
= stream
.read(buffer
);
61 var str
= new java
.lang
.String(buffer
, 0, count
);
68 function write(url
, data
)
70 var key
= url
.toString();
71 var outputEntry
= openCacheEntry(key
, nsICache
.ACCESS_WRITE
);
72 var output
= outputEntry
.transport
.openOutputStream(0, -1, 0);
73 var count
= output
.write(data
, data
.length
);
75 // store some metadata.
76 outputEntry
.setMetaDataElement("size", data
.length
);
79 outputEntry
.markValid();
85 function CacheListener()
90 CacheListener
.prototype = {
91 QueryInterface : function(iid
)
93 if (iid
.equals(Components
.interfaces
.nsICacheListener
))
95 throw Components
.results
.NS_NOINTERFACE
;
98 onCacheEntryAvailable : function(/* in nsICacheEntryDescriptor */ descriptor
,
99 /* in nsCacheAccessMode */ accessGranted
,
100 /* in nsresult */ status
)
102 this.descriptor
= descriptor
;
103 this.status
= status
;
108 function asyncOpenCacheEntry(url
, mode
)
110 var session
= createCacheSession(clientID
, nsICache
.STORE_ON_DISK
, true);
111 var listener
= new CacheListener();
112 session
.asyncOpenCacheEntry(url
, mode
, listener
);
113 while (!listener
.done
)
114 eventQueue
.processPendingEvents();
115 return listener
.descriptor
;
118 function asyncWrite(key
, data
)
120 var outputEntry
= asyncOpenCacheEntry(key
, nsICache
.ACCESS_WRITE
);
122 var output
= outputEntry
.transport
.openOutputStream(0, -1, 0);
123 var count
= output
.write(data
, data
.length
);
125 // store some metadata.
126 outputEntry
.setMetaDataElement("size", data
.length
);
129 outputEntry
.markValid();
135 function StreamListener()
142 StreamListener
.prototype = {
143 QueryInterface : function(iid
)
145 if (iid
.equals(Components
.interfaces
.nsIStreamListener
) ||
146 iid
.equals(Components
.interfaces
.nsIStreamObserver
))
148 throw Components
.results
.NS_NOINTERFACE
;
151 onStartRequest : function(request
, context
)
155 onStopRequest : function(request
, context
, statusCode
, statusText
)
157 this.statusCode
= statusCode
;
161 onDataAvailable : function(request
, context
, input
, offset
, count
)
163 if (this.wrapper
== null)
164 this.wrapper
= wrapInputStream(input
);
165 input
= this.wrapper
;
166 this.data
+= input
.read(count
);
170 function asyncRead(key
)
172 var inputEntry
= asyncOpenCacheEntry(key
, nsICache
.ACCESS_READ
);
173 var listener
= new StreamListener();
174 inputEntry
.transport
.asyncRead(listener
, null, 0, inputEntry
.dataSize
, 0);
175 while (!listener
.done
)
176 eventQueue
.processPendingEvents();
178 return listener
.data
;
183 var inputEntry
= openCacheEntry(key
, nsICache
.ACCESS_READ
);
184 var input
= wrapInputStream(inputEntry
.transport
.openInputStream(0, -1, 0));
185 var data
= input
.read(input
.available());
191 function readMetaData(key
, element
)
193 var inputEntry
= openCacheEntry(key
, nsICache
.ACCESS_READ
);
194 var metadata
= inputEntry
.getMetaDataElement(element
);
201 var key
= url
.toString();
202 var doomedEntry
= openCacheEntry(key
, nsICache
.ACCESS_READ_WRITE
);
209 // download some real content from the network.
210 var url
= new java
.net
.URL("http://www.mozilla.org");
211 var key
= url
.toString();
212 var data
= download(url
);
214 if (asyncWrite(key
, data
) == data
.length
)
215 print("disk cache write works!");
217 print("disk cache write broken!");
219 if (asyncRead(key
) == data
)
220 print("disk cache read works!");
222 print("disk cache read broken!");
224 if (readMetaData(key
, "size") == data
.length
)
225 print("disk cache metadata works!");
227 print("disk cache metadata broken!");
230 function median(array
)
232 var cmp = function(x
, y
) { return x
- y
; }
234 var middle
= Math
.floor(array
.length
/ 2);
235 return array
[middle
];
241 var len
= array
.length
;
242 for (var i
= 0; i
< len
; ++i
)
250 var System
= java
.lang
.System
;
251 var url
= new java
.net
.URL("http://www.mozilla.org");
252 var key
= url
.toString();
253 var downloadTimes
= new Array();
254 for (var i
= 0; i
< N
; ++i
) {
255 var begin
= System
.currentTimeMillis();
257 var end
= System
.currentTimeMillis();
258 downloadTimes
.push(end
- begin
);
260 var downloadTotal
= sum(downloadTimes
);
261 var downloadMean
= downloadTotal
/ N
;
262 var downloadMedian
= median(downloadTimes
);
263 print("" + N
+ " downloads took " + downloadTotal
+ " milliseconds.");
264 print("mean = " + downloadMean
+ " milliseconds.");
265 print("median = " + downloadMedian
+ " milliseconds.");
267 var readTimes
= new Array();
268 for (var i
= 0; i
< N
; ++i
) {
269 var begin
= System
.currentTimeMillis();
271 var end
= System
.currentTimeMillis();
272 readTimes
.push(end
- begin
);
274 var readTotal
= sum(readTimes
);
275 var readMean
= readTotal
/ N
;
276 var readMedian
= median(readTimes
);
277 print("" + N
+ " reads took " + readTotal
+ " milliseconds.");
278 print("mean = " + readMean
+ " milliseconds.");
279 print("median = " + readMedian
+ " milliseconds.");
282 // load the cache service before doing anything with Java...
286 print("cache service loaded.");
288 print("running disk cache test.");
290 print("disk cache test complete.");