Back to index

lightning-sunbird  0.9+nobinonly
TestPerf.cpp
Go to the documentation of this file.
00001 #include "TestCommon.h"
00002 #include <stdio.h>
00003 #include "nsCRT.h" /* should be "plstr.h"? */
00004 #include "nsNetUtil.h"
00005 #include "nsIEventQueueService.h"
00006 #include "nsIServiceManager.h"
00007 #include "nsIComponentRegistrar.h"
00008 #include "nsISupportsArray.h"
00009 
00010 static nsIIOService *gIOService = nsnull;
00011 
00012 //-----------------------------------------------------------------------------
00013 
00014 static PRBool
00015 load_sync_1(nsISupports *element, void *data)
00016 {
00017     nsCOMPtr<nsIInputStream> stream;
00018     nsCOMPtr<nsIURI> uri( do_QueryInterface(element) );
00019     nsCAutoString spec;
00020     nsresult rv;
00021 
00022     rv = NS_OpenURI(getter_AddRefs(stream), uri, gIOService);
00023     if (NS_FAILED(rv)) {
00024         uri->GetAsciiSpec(spec);
00025         fprintf(stderr, "*** failed opening %s [rv=%x]\n", spec.get(), rv);
00026         return PR_TRUE;
00027     }
00028 
00029     char buf[4096];
00030     PRUint32 bytesRead;
00031 
00032     while (1) {
00033         rv = stream->Read(buf, sizeof(buf), &bytesRead);
00034         if (NS_FAILED(rv) || bytesRead == 0) {
00035             if (NS_FAILED(rv)) {
00036                 uri->GetAsciiSpec(spec);
00037                 fprintf(stderr, "*** failed reading %s [rv=%x]\n", spec.get(), rv);
00038             }
00039             break;
00040         }
00041     }
00042 
00043     return PR_TRUE;
00044 }
00045 
00046 static nsresult
00047 load_sync(nsISupportsArray *urls)
00048 {
00049     urls->EnumerateForwards(load_sync_1, nsnull);
00050     return NS_OK;
00051 }
00052 
00053 //-----------------------------------------------------------------------------
00054 
00055 static int gRequestCount = 0;
00056 static nsIEventQueue *gEventQ = 0;
00057 
00058 
00059 class MyListener : public nsIStreamListener
00060 {
00061 public:
00062     NS_DECL_ISUPPORTS
00063     NS_DECL_NSIREQUESTOBSERVER
00064     NS_DECL_NSISTREAMLISTENER
00065 
00066     MyListener() { }
00067     virtual ~MyListener() {}
00068 };
00069 
00070 NS_IMPL_ISUPPORTS2(MyListener, nsIStreamListener, nsIRequestObserver)
00071 
00072 NS_IMETHODIMP
00073 MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctx)
00074 {
00075     return NS_OK;
00076 }
00077 
00078 NS_IMETHODIMP
00079 MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx,
00080                             nsIInputStream *stream,
00081                             PRUint32 offset, PRUint32 count)
00082 {
00083     nsresult rv;
00084     char buf[4096];
00085     PRUint32 n, bytesRead;
00086     while (count) {
00087         n = PR_MIN(count, sizeof(buf));
00088         rv = stream->Read(buf, n, &bytesRead);
00089         if (NS_FAILED(rv))
00090             break;
00091         count -= bytesRead;
00092         if (bytesRead == 0)
00093             break;
00094     }
00095     return NS_OK;
00096 }
00097 
00098 static void *PR_CALLBACK
00099 ShutdownEvent_Handler(PLEvent *ev)
00100 {
00101     return nsnull;
00102 }
00103 
00104 static void PR_CALLBACK
00105 ShutdownEvent_Cleanup(PLEvent *ev)
00106 {
00107     delete ev;
00108 }
00109 
00110 NS_IMETHODIMP
00111 MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
00112 {
00113     if (NS_FAILED(status)) {
00114         nsCAutoString spec;
00115         req->GetName(spec);
00116         fprintf(stderr, "*** failed loading %s [reason=%x]\n", spec.get(), status);
00117     }
00118     if (--gRequestCount == 0) {
00119         // post shutdown event
00120         PLEvent *ev = new PLEvent;
00121         PL_InitEvent(ev, nsnull,
00122                 ShutdownEvent_Handler,
00123                 ShutdownEvent_Cleanup);
00124         gEventQ->PostEvent(ev);
00125     }
00126     return NS_OK;
00127 }
00128 
00129 static PRBool
00130 load_async_1(nsISupports *element, void *data)
00131 {
00132     nsCOMPtr<nsIURI> uri( do_QueryInterface(element) );
00133     if (!uri)
00134         return PR_TRUE;
00135 
00136     MyListener *listener = new MyListener();
00137     if (!listener)
00138         return PR_TRUE;
00139     NS_ADDREF(listener);
00140     nsresult rv = NS_OpenURI(listener, nsnull, uri, gIOService);
00141     NS_RELEASE(listener);
00142     if (NS_SUCCEEDED(rv))
00143         gRequestCount++;
00144     else 
00145         printf(">> NS_OpenURI failed [rv=%x]\n", rv);
00146     return PR_TRUE;
00147 }
00148 
00149 static nsresult
00150 load_async(nsISupportsArray *urls)
00151 {
00152     nsresult rv;
00153 
00154     // Create the Event Queue for this thread...
00155     nsCOMPtr<nsIEventQueueService> eqs(
00156             do_GetService("@mozilla.org/event-queue-service;1", &rv) );
00157     if (NS_FAILED(rv)) return rv;
00158 
00159     rv = eqs->CreateMonitoredThreadEventQueue();
00160     if (NS_FAILED(rv)) return rv;
00161 
00162     rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00163     if (NS_FAILED(rv)) return rv;
00164 
00165     urls->EnumerateForwards(load_async_1, nsnull);
00166 
00167     while (1) {
00168         PLEvent *ev;
00169         gEventQ->WaitForEvent(&ev);
00170         if (gRequestCount == 0)
00171             break;
00172         gEventQ->HandleEvent(ev);
00173     }
00174     NS_RELEASE(gEventQ);
00175     return NS_OK;
00176 }
00177 
00178 //-----------------------------------------------------------------------------
00179 
00180 static nsresult
00181 read_file(const char *fname, nsISupportsArray *urls)
00182 {
00183     FILE *fp = fopen(fname, "r");
00184     if (!fp) {
00185         printf("failed opening file: %s\n", fname);
00186         return NS_ERROR_FAILURE;
00187     }
00188 
00189     nsCOMPtr<nsIURI> uri;
00190     nsresult rv;
00191     char buf[512];
00192     while (fgets(buf, sizeof(buf), fp)) {
00193         // remove trailing newline
00194         buf[strlen(buf) - 1] = 0;
00195         rv = NS_NewURI(getter_AddRefs(uri), buf, nsnull, gIOService); 
00196         if (NS_FAILED(rv))
00197             printf("*** ignoring malformed uri: %s\n", buf);
00198         else {
00199             //nsXPIDLCString spec;
00200             //uri->GetSpec(getter_Copies(spec));
00201             //printf("read url: %s\n", spec.get());
00202             urls->AppendElement(uri);
00203         }
00204     }
00205 
00206     fclose(fp);
00207     return NS_OK;
00208 }
00209 
00210 //-----------------------------------------------------------------------------
00211 
00212 static void
00213 print_usage()
00214 {
00215     printf("usage: TestPerf [-sync|-async] <file-of-urls>\n");
00216 }
00217 
00218 int
00219 main(int argc, char **argv)
00220 {
00221     if (test_common_init(&argc, &argv) != 0)
00222         return -1;
00223 
00224     nsresult rv;
00225     PRBool sync;
00226 
00227     if (argc < 3) {
00228         print_usage();
00229         return -1;
00230     }
00231 
00232     if (PL_strcasecmp(argv[1], "-sync") == 0)
00233         sync = PR_TRUE;
00234     else if (PL_strcasecmp(argv[1], "-async") == 0)
00235         sync = PR_FALSE;
00236     else {
00237         print_usage();
00238         return -1;
00239     }
00240 
00241     nsCOMPtr<nsIServiceManager> servMan;
00242     NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00243     nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00244     NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00245     registrar->AutoRegister(nsnull);
00246 
00247     // cache the io service
00248     {
00249         nsCOMPtr<nsIIOService> ioserv( do_GetIOService() );
00250         NS_ADDREF(gIOService = ioserv);
00251     }
00252 
00253     nsCOMPtr<nsISupportsArray> urls;
00254     rv = NS_NewISupportsArray(getter_AddRefs(urls));
00255     if (NS_FAILED(rv)) return -1;
00256 
00257     rv = read_file(argv[2], urls);
00258     if (NS_FAILED(rv)) {
00259         printf("failed reading file-of-urls\n");
00260         return -1;
00261     }
00262 
00263     PRUint32 urlCount;
00264     urls->Count(&urlCount);
00265 
00266     PRIntervalTime start = PR_IntervalNow();
00267 
00268     if (sync)
00269         rv = load_sync(urls);
00270     else
00271         rv = load_async(urls);
00272 
00273     if (NS_FAILED(rv)) {
00274         printf("load failed\n");
00275         return -1;
00276     }
00277 
00278     PRIntervalTime end = PR_IntervalNow();
00279     fprintf(stderr, "read: %u urls; total time: %u milliseconds\n",
00280             urlCount,
00281             PR_IntervalToMilliseconds(end - start));
00282 
00283     NS_RELEASE(gIOService);
00284     return 0;
00285 }