On x86 compilers without fastcall, simulate it when invoking traces and un-simulate...
[wine-gecko.git] / netwerk / test / TestAsyncCache.js
blob381e10546f970a152d9a9f2d9386f8c8073f38d8
1 var DEBUG = true;
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);
20     return service;
23 function createCacheSession(clientID, storagePolicy, streamable)
25     var service = getCacheService();
26     var session = service.createSession(clientID, storagePolicy, streamable);
27     return session;
30 function openCacheEntry(url, mode)
32     var session = createCacheSession(clientID, nsICache.STORE_ON_DISK, true);
33     var entry = session.openCacheEntry(url, mode);
34     return entry;
37 function dumpLeaks()
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);
48     wrapper.init(input);
49     return wrapper;
52 function download(url)
54     var data = "";
55     var buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 65536);
56     var stream = url.getContent();
57     while (true) {
58         var count = stream.read(buffer);
59         if (count <= 0)
60             break;
61         var str = new java.lang.String(buffer, 0, count);
62         data += str;
63     }
64     stream.close();
65     return data;
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);
78     output.close();
79     outputEntry.markValid();
80     outputEntry.close();
82     return count;
85 function CacheListener()
87     this.done = false;
90 CacheListener.prototype = {
91     QueryInterface : function(iid)
92     {
93         if (iid.equals(Components.interfaces.nsICacheListener))
94             return this;
95         throw Components.results.NS_NOINTERFACE;
96     },
98     onCacheEntryAvailable : function(/* in nsICacheEntryDescriptor */   descriptor,
99                                      /* in nsCacheAccessMode */         accessGranted,
100                                      /* in nsresult */                  status)
101     {
102         this.descriptor = descriptor;
103         this.status = status;
104         this.done = true;
105     }
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);
121     
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);
128     output.close();
129     outputEntry.markValid();
130     outputEntry.close();
132     return count;
135 function StreamListener()
137     this.done = false;
138     this.data = "";
139     this.wrapper = null;
142 StreamListener.prototype = {
143     QueryInterface : function(iid)
144     {
145         if (iid.equals(Components.interfaces.nsIStreamListener) ||
146             iid.equals(Components.interfaces.nsIStreamObserver))
147             return this;
148         throw Components.results.NS_NOINTERFACE;
149     },
150     
151     onStartRequest : function(request, context)
152     {
153     },
154     
155     onStopRequest : function(request, context, statusCode, statusText)
156     {
157         this.statusCode = statusCode;
158         this.done = true;
159     },
160     
161     onDataAvailable : function(request, context, input, offset, count)
162     {
163         if (this.wrapper == null)
164             this.wrapper = wrapInputStream(input);
165         input = this.wrapper;
166         this.data += input.read(count);
167     }
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();
177     inputEntry.close();
178     return listener.data;
181 function read(key)
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());
186     input.close();
187     inputEntry.close();
188     return data;
191 function readMetaData(key, element)
193     var inputEntry = openCacheEntry(key, nsICache.ACCESS_READ);
194     var metadata = inputEntry.getMetaDataElement(element);
195     inputEntry.close();
196     return metadata;
199 function doom(url)
201     var key = url.toString();
202     var doomedEntry = openCacheEntry(key, nsICache.ACCESS_READ_WRITE);
203     doomedEntry.doom();
204     doomedEntry.close();
207 function test()
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);
213     
214     if (asyncWrite(key, data) == data.length)
215         print("disk cache write works!");
216     else
217         print("disk cache write broken!");
219     if (asyncRead(key) == data)
220         print("disk cache read works!");
221     else
222         print("disk cache read broken!");
224     if (readMetaData(key, "size") == data.length)
225         print("disk cache metadata works!");
226     else
227         print("disk cache metadata broken!");
230 function median(array)
232     var cmp = function(x, y) { return x - y; }
233     array.sort(cmp);
234     var middle = Math.floor(array.length / 2);
235     return array[middle];
238 function sum(array)
240     var s = 0;
241     var len = array.length;
242     for (var i = 0; i < len; ++i)
243         s += array[i];
244     return s;
247 function time()
249     var N = 50;
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();
256         download(url);
257         var end = System.currentTimeMillis();
258         downloadTimes.push(end - begin);
259     }
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();
270         asyncRead(key);
271         var end = System.currentTimeMillis();
272         readTimes.push(end - begin);
273     }
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...
283 getCacheService();
285 if (DEBUG) {
286     print("cache service loaded.");
287 } else {
288     print("running disk cache test.");
289     test();
290     print("disk cache test complete.");