1 #include "TestCommon.h"
3 #include "nsCRT.h" /* should be "plstr.h"? */
5 #include "nsIServiceManager.h"
6 #include "nsIComponentRegistrar.h"
7 #include "nsISupportsArray.h"
11 static nsIIOService
*gIOService
= nsnull
;
13 //-----------------------------------------------------------------------------
16 load_sync_1(nsISupports
*element
, void *data
)
18 nsCOMPtr
<nsIInputStream
> stream
;
19 nsCOMPtr
<nsIURI
> uri( do_QueryInterface(element
) );
23 rv
= NS_OpenURI(getter_AddRefs(stream
), uri
, gIOService
);
25 uri
->GetAsciiSpec(spec
);
26 fprintf(stderr
, "*** failed opening %s [rv=%x]\n", spec
.get(), rv
);
34 rv
= stream
->Read(buf
, sizeof(buf
), &bytesRead
);
35 if (NS_FAILED(rv
) || bytesRead
== 0) {
37 uri
->GetAsciiSpec(spec
);
38 fprintf(stderr
, "*** failed reading %s [rv=%x]\n", spec
.get(), rv
);
48 load_sync(nsISupportsArray
*urls
)
50 urls
->EnumerateForwards(load_sync_1
, nsnull
);
54 //-----------------------------------------------------------------------------
56 static int gRequestCount
= 0;
58 class MyListener
: public nsIStreamListener
62 NS_DECL_NSIREQUESTOBSERVER
63 NS_DECL_NSISTREAMLISTENER
66 virtual ~MyListener() {}
69 NS_IMPL_ISUPPORTS2(MyListener
, nsIStreamListener
, nsIRequestObserver
)
72 MyListener::OnStartRequest(nsIRequest
*req
, nsISupports
*ctx
)
78 MyListener::OnDataAvailable(nsIRequest
*req
, nsISupports
*ctx
,
79 nsIInputStream
*stream
,
80 PRUint32 offset
, PRUint32 count
)
84 PRUint32 n
, bytesRead
;
86 n
= PR_MIN(count
, sizeof(buf
));
87 rv
= stream
->Read(buf
, n
, &bytesRead
);
98 MyListener::OnStopRequest(nsIRequest
*req
, nsISupports
*ctx
, nsresult status
)
100 if (NS_FAILED(status
)) {
103 fprintf(stderr
, "*** failed loading %s [reason=%x]\n", spec
.get(), status
);
105 if (--gRequestCount
== 0) {
106 // post shutdown event
113 load_async_1(nsISupports
*element
, void *data
)
115 nsCOMPtr
<nsIURI
> uri( do_QueryInterface(element
) );
119 MyListener
*listener
= new MyListener();
123 nsresult rv
= NS_OpenURI(listener
, nsnull
, uri
, gIOService
);
124 NS_RELEASE(listener
);
125 if (NS_SUCCEEDED(rv
))
128 printf(">> NS_OpenURI failed [rv=%x]\n", rv
);
133 load_async(nsISupportsArray
*urls
)
135 urls
->EnumerateForwards(load_async_1
, nsnull
);
141 //-----------------------------------------------------------------------------
144 read_file(const char *fname
, nsISupportsArray
*urls
)
146 FILE *fp
= fopen(fname
, "r");
148 printf("failed opening file: %s\n", fname
);
149 return NS_ERROR_FAILURE
;
152 nsCOMPtr
<nsIURI
> uri
;
155 while (fgets(buf
, sizeof(buf
), fp
)) {
156 // remove trailing newline
157 buf
[strlen(buf
) - 1] = 0;
158 rv
= NS_NewURI(getter_AddRefs(uri
), buf
, nsnull
, gIOService
);
160 printf("*** ignoring malformed uri: %s\n", buf
);
162 //nsXPIDLCString spec;
163 //uri->GetSpec(getter_Copies(spec));
164 //printf("read url: %s\n", spec.get());
165 urls
->AppendElement(uri
);
173 //-----------------------------------------------------------------------------
178 printf("usage: TestPerf [-sync|-async] <file-of-urls>\n");
183 using namespace TestPerf
;
186 main(int argc
, char **argv
)
188 if (test_common_init(&argc
, &argv
) != 0)
199 if (PL_strcasecmp(argv
[1], "-sync") == 0)
201 else if (PL_strcasecmp(argv
[1], "-async") == 0)
208 nsCOMPtr
<nsIServiceManager
> servMan
;
209 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
210 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
211 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
212 registrar
->AutoRegister(nsnull
);
214 // cache the io service
216 nsCOMPtr
<nsIIOService
> ioserv( do_GetIOService() );
217 NS_ADDREF(gIOService
= ioserv
);
220 nsCOMPtr
<nsISupportsArray
> urls
;
221 rv
= NS_NewISupportsArray(getter_AddRefs(urls
));
222 if (NS_FAILED(rv
)) return -1;
224 rv
= read_file(argv
[2], urls
);
226 printf("failed reading file-of-urls\n");
231 urls
->Count(&urlCount
);
233 PRIntervalTime start
= PR_IntervalNow();
236 rv
= load_sync(urls
);
238 rv
= load_async(urls
);
241 printf("load failed\n");
245 PRIntervalTime end
= PR_IntervalNow();
246 fprintf(stderr
, "read: %u urls; total time: %u milliseconds\n",
248 PR_IntervalToMilliseconds(end
- start
));
250 NS_RELEASE(gIOService
);