3 var clientID
= "javascript";
4 var nsICache
= Components
.interfaces
.nsICache
;
6 function getCacheService()
8 var nsCacheService
= Components
.classes
["@mozilla.org/network/cache-service;1"];
9 var service
= nsCacheService
.getService(Components
.interfaces
.nsICacheService
);
13 function createCacheSession(clientID
, storagePolicy
, streamable
)
15 var service
= getCacheService();
16 var session
= service
.createSession(clientID
, storagePolicy
, streamable
);
20 function openCacheEntry(url
, mode
)
22 var session
= createCacheSession(clientID
, nsICache
.STORE_ON_DISK
, true);
23 var entry
= session
.openCacheEntry(url
, mode
);
29 var leakDetector
= Components
.classes
["@mozilla.org/xpcom/leakdetector;1"].getService(Components
.interfaces
.nsILeakDetector
);
30 leakDetector
.dumpLeaks();
33 function wrapInputStream(input
)
35 var nsIScriptableInputStream
= Components
.interfaces
.nsIScriptableInputStream
;
36 var factory
= Components
.classes
["@mozilla.org/scriptableinputstream;1"];
37 var wrapper
= factory
.createInstance(nsIScriptableInputStream
);
42 function getIOService()
44 var CID
= Components
.classes
["@mozilla.org/network/io-service;1"];
45 var service
= CID
.getService(Components
.interfaces
.nsIIOService
);
49 function downloadHTTP(spec
)
51 var ioService
= getIOService();
52 var uri
= ioService
.newURI(spec
, null, null);
53 var channel
= ioService
.newChannelFromURI(uri
);
54 var input
= wrapInputStream(channel
.open());
55 var data
= input
.read(input
.available());
60 function download(url
)
63 var buffer
= java
.lang
.reflect
.Array
.newInstance(java
.lang
.Byte
.TYPE
, 65536);
64 var stream
= url
.getContent();
66 var count
= stream
.read(buffer
);
69 var str
= new java
.lang
.String(buffer
, 0, count
);
76 function write(url
, data
)
78 var key
= url
.toString();
79 var outputEntry
= openCacheEntry(key
, nsICache
.ACCESS_WRITE
);
80 var output
= outputEntry
.transport
.openOutputStream(0, -1, 0);
81 var count
= output
.write(data
, data
.length
);
83 // store some metadata.
84 outputEntry
.setMetaDataElement("size", data
.length
);
87 outputEntry
.markValid();
95 var key
= url
.toString();
96 var inputEntry
= openCacheEntry(key
, nsICache
.ACCESS_READ
);
97 var input
= wrapInputStream(inputEntry
.transport
.openInputStream(0, -1, 0));
98 var data
= input
.read(input
.available());
104 function readMetaData(url
, element
)
106 var key
= url
.toString();
107 var inputEntry
= openCacheEntry(key
, nsICache
.ACCESS_READ
);
108 var metadata
= inputEntry
.getMetaDataElement(element
);
115 var key
= url
.toString();
116 var doomedEntry
= openCacheEntry(key
, nsICache
.ACCESS_READ_WRITE
);
123 // download some real content from the network.
124 var url
= new java
.net
.URL("http://www.mozilla.org");
125 var key
= url
.toString();
126 var data
= download(url
);
128 if (write(url
, data
) == data
.length
)
129 print("disk cache write works!");
131 print("disk cache write broken!");
133 if (read(url
) == data
)
134 print("disk cache read works!");
136 print("disk cache read broken!");
138 if (readMetaData(url
, "size") == data
.length
)
139 print("disk cache metadata works!");
141 print("disk cache metadata broken!");
144 function median(array
)
146 var cmp = function(x
, y
) { return x
- y
; }
148 var middle
= Math
.floor(array
.length
/ 2);
149 return array
[middle
];
155 var len
= array
.length
;
156 for (var i
= 0; i
< len
; ++i
)
164 var System
= java
.lang
.System
;
165 var url
= new java
.net
.URL("http://www.mozilla.org");
166 var downloadTimes
= new Array();
167 for (var i
= 0; i
< N
; ++i
) {
168 var begin
= System
.currentTimeMillis();
170 var end
= System
.currentTimeMillis();
171 downloadTimes
.push(end
- begin
);
173 var downloadTotal
= sum(downloadTimes
);
174 var downloadMean
= downloadTotal
/ N
;
175 var downloadMedian
= median(downloadTimes
);
176 print("" + N
+ " downloads took " + downloadTotal
+ " milliseconds.");
177 print("mean = " + downloadMean
+ " milliseconds.");
178 print("median = " + downloadMedian
+ " milliseconds.");
180 var readTimes
= new Array();
181 for (var i
= 0; i
< N
; ++i
) {
182 var begin
= System
.currentTimeMillis();
184 var end
= System
.currentTimeMillis();
185 readTimes
.push(end
- begin
);
187 var readTotal
= sum(readTimes
);
188 var readMean
= readTotal
/ N
;
189 var readMedian
= median(readTimes
);
190 print("" + N
+ " reads took " + readTotal
+ " milliseconds.");
191 print("mean = " + readMean
+ " milliseconds.");
192 print("median = " + readMedian
+ " milliseconds.");
195 // load the cache service before doing anything with Java...
199 print("cache service loaded.");
201 print("running disk cache test.");
203 print("disk cache test complete.");