michael@0: #include "TestCommon.h" michael@0: #include michael@0: #include "nsCRT.h" /* should be "plstr.h"? */ michael@0: #include "nsNetUtil.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIComponentRegistrar.h" michael@0: #include "nsISupportsArray.h" michael@0: #include michael@0: michael@0: namespace TestPerf { michael@0: michael@0: static nsIIOService *gIOService = nullptr; michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: static bool michael@0: load_sync_1(nsISupports *element, void *data) michael@0: { michael@0: nsCOMPtr stream; michael@0: nsCOMPtr uri( do_QueryInterface(element) ); michael@0: nsAutoCString spec; michael@0: nsresult rv; michael@0: michael@0: rv = NS_OpenURI(getter_AddRefs(stream), uri, gIOService); michael@0: if (NS_FAILED(rv)) { michael@0: uri->GetAsciiSpec(spec); michael@0: fprintf(stderr, "*** failed opening %s [rv=%x]\n", spec.get(), rv); michael@0: return true; michael@0: } michael@0: michael@0: char buf[4096]; michael@0: uint32_t bytesRead; michael@0: michael@0: while (1) { michael@0: rv = stream->Read(buf, sizeof(buf), &bytesRead); michael@0: if (NS_FAILED(rv) || bytesRead == 0) { michael@0: if (NS_FAILED(rv)) { michael@0: uri->GetAsciiSpec(spec); michael@0: fprintf(stderr, "*** failed reading %s [rv=%x]\n", spec.get(), rv); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static nsresult michael@0: load_sync(nsISupportsArray *urls) michael@0: { michael@0: urls->EnumerateForwards(load_sync_1, nullptr); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: static int gRequestCount = 0; michael@0: michael@0: class MyListener : public nsIStreamListener michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: NS_DECL_NSISTREAMLISTENER michael@0: michael@0: MyListener() { } michael@0: virtual ~MyListener() {} michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(MyListener, nsIStreamListener, nsIRequestObserver) michael@0: michael@0: NS_IMETHODIMP michael@0: MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctx) michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx, michael@0: nsIInputStream *stream, michael@0: uint64_t offset, uint32_t count) michael@0: { michael@0: nsresult rv; michael@0: char buf[4096]; michael@0: uint32_t n, bytesRead; michael@0: while (count) { michael@0: n = std::min(count, sizeof(buf)); michael@0: rv = stream->Read(buf, n, &bytesRead); michael@0: if (NS_FAILED(rv)) michael@0: break; michael@0: count -= bytesRead; michael@0: if (bytesRead == 0) michael@0: break; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status) michael@0: { michael@0: if (NS_FAILED(status)) { michael@0: nsAutoCString spec; michael@0: req->GetName(spec); michael@0: fprintf(stderr, "*** failed loading %s [reason=%x]\n", spec.get(), status); michael@0: } michael@0: if (--gRequestCount == 0) { michael@0: // post shutdown event michael@0: QuitPumpingEvents(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: static bool michael@0: load_async_1(nsISupports *element, void *data) michael@0: { michael@0: nsCOMPtr uri( do_QueryInterface(element) ); michael@0: if (!uri) michael@0: return true; michael@0: michael@0: MyListener *listener = new MyListener(); michael@0: if (!listener) michael@0: return true; michael@0: NS_ADDREF(listener); michael@0: nsresult rv = NS_OpenURI(listener, nullptr, uri, gIOService); michael@0: NS_RELEASE(listener); michael@0: if (NS_SUCCEEDED(rv)) michael@0: gRequestCount++; michael@0: else michael@0: printf(">> NS_OpenURI failed [rv=%x]\n", rv); michael@0: return true; michael@0: } michael@0: michael@0: static nsresult michael@0: load_async(nsISupportsArray *urls) michael@0: { michael@0: urls->EnumerateForwards(load_async_1, nullptr); michael@0: michael@0: PumpEvents(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: static nsresult michael@0: read_file(const char *fname, nsISupportsArray *urls) michael@0: { michael@0: FILE *fp = fopen(fname, "r"); michael@0: if (!fp) { michael@0: printf("failed opening file: %s\n", fname); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsCOMPtr uri; michael@0: nsresult rv; michael@0: char buf[512]; michael@0: while (fgets(buf, sizeof(buf), fp)) { michael@0: // remove trailing newline michael@0: buf[strlen(buf) - 1] = 0; michael@0: rv = NS_NewURI(getter_AddRefs(uri), buf, nullptr, gIOService); michael@0: if (NS_FAILED(rv)) michael@0: printf("*** ignoring malformed uri: %s\n", buf); michael@0: else { michael@0: //nsXPIDLCString spec; michael@0: //uri->GetSpec(getter_Copies(spec)); michael@0: //printf("read url: %s\n", spec.get()); michael@0: urls->AppendElement(uri); michael@0: } michael@0: } michael@0: michael@0: fclose(fp); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: static void michael@0: print_usage() michael@0: { michael@0: printf("usage: TestPerf [-sync|-async] \n"); michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: using namespace TestPerf; michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: if (test_common_init(&argc, &argv) != 0) michael@0: return -1; michael@0: michael@0: nsresult rv; michael@0: bool sync; michael@0: michael@0: if (argc < 3) { michael@0: print_usage(); michael@0: return -1; michael@0: } michael@0: michael@0: if (PL_strcasecmp(argv[1], "-sync") == 0) michael@0: sync = true; michael@0: else if (PL_strcasecmp(argv[1], "-async") == 0) michael@0: sync = false; michael@0: else { michael@0: print_usage(); michael@0: return -1; michael@0: } michael@0: michael@0: nsCOMPtr servMan; michael@0: NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); michael@0: nsCOMPtr registrar = do_QueryInterface(servMan); michael@0: NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); michael@0: registrar->AutoRegister(nullptr); michael@0: michael@0: // cache the io service michael@0: { michael@0: nsCOMPtr ioserv( do_GetIOService() ); michael@0: NS_ADDREF(gIOService = ioserv); michael@0: } michael@0: michael@0: nsCOMPtr urls; michael@0: rv = NS_NewISupportsArray(getter_AddRefs(urls)); michael@0: if (NS_FAILED(rv)) return -1; michael@0: michael@0: rv = read_file(argv[2], urls); michael@0: if (NS_FAILED(rv)) { michael@0: printf("failed reading file-of-urls\n"); michael@0: return -1; michael@0: } michael@0: michael@0: uint32_t urlCount; michael@0: urls->Count(&urlCount); michael@0: michael@0: PRIntervalTime start = PR_IntervalNow(); michael@0: michael@0: if (sync) michael@0: rv = load_sync(urls); michael@0: else michael@0: rv = load_async(urls); michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: printf("load failed\n"); michael@0: return -1; michael@0: } michael@0: michael@0: PRIntervalTime end = PR_IntervalNow(); michael@0: fprintf(stderr, "read: %u urls; total time: %u milliseconds\n", michael@0: urlCount, michael@0: PR_IntervalToMilliseconds(end - start)); michael@0: michael@0: NS_RELEASE(gIOService); michael@0: return 0; michael@0: }