Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | #include "TestCommon.h" |
michael@0 | 2 | #include <stdio.h> |
michael@0 | 3 | #include "nsCRT.h" /* should be "plstr.h"? */ |
michael@0 | 4 | #include "nsNetUtil.h" |
michael@0 | 5 | #include "nsIServiceManager.h" |
michael@0 | 6 | #include "nsIComponentRegistrar.h" |
michael@0 | 7 | #include "nsISupportsArray.h" |
michael@0 | 8 | #include <algorithm> |
michael@0 | 9 | |
michael@0 | 10 | namespace TestPerf { |
michael@0 | 11 | |
michael@0 | 12 | static nsIIOService *gIOService = nullptr; |
michael@0 | 13 | |
michael@0 | 14 | //----------------------------------------------------------------------------- |
michael@0 | 15 | |
michael@0 | 16 | static bool |
michael@0 | 17 | load_sync_1(nsISupports *element, void *data) |
michael@0 | 18 | { |
michael@0 | 19 | nsCOMPtr<nsIInputStream> stream; |
michael@0 | 20 | nsCOMPtr<nsIURI> uri( do_QueryInterface(element) ); |
michael@0 | 21 | nsAutoCString spec; |
michael@0 | 22 | nsresult rv; |
michael@0 | 23 | |
michael@0 | 24 | rv = NS_OpenURI(getter_AddRefs(stream), uri, gIOService); |
michael@0 | 25 | if (NS_FAILED(rv)) { |
michael@0 | 26 | uri->GetAsciiSpec(spec); |
michael@0 | 27 | fprintf(stderr, "*** failed opening %s [rv=%x]\n", spec.get(), rv); |
michael@0 | 28 | return true; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | char buf[4096]; |
michael@0 | 32 | uint32_t bytesRead; |
michael@0 | 33 | |
michael@0 | 34 | while (1) { |
michael@0 | 35 | rv = stream->Read(buf, sizeof(buf), &bytesRead); |
michael@0 | 36 | if (NS_FAILED(rv) || bytesRead == 0) { |
michael@0 | 37 | if (NS_FAILED(rv)) { |
michael@0 | 38 | uri->GetAsciiSpec(spec); |
michael@0 | 39 | fprintf(stderr, "*** failed reading %s [rv=%x]\n", spec.get(), rv); |
michael@0 | 40 | } |
michael@0 | 41 | break; |
michael@0 | 42 | } |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | return true; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | static nsresult |
michael@0 | 49 | load_sync(nsISupportsArray *urls) |
michael@0 | 50 | { |
michael@0 | 51 | urls->EnumerateForwards(load_sync_1, nullptr); |
michael@0 | 52 | return NS_OK; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | //----------------------------------------------------------------------------- |
michael@0 | 56 | |
michael@0 | 57 | static int gRequestCount = 0; |
michael@0 | 58 | |
michael@0 | 59 | class MyListener : public nsIStreamListener |
michael@0 | 60 | { |
michael@0 | 61 | public: |
michael@0 | 62 | NS_DECL_ISUPPORTS |
michael@0 | 63 | NS_DECL_NSIREQUESTOBSERVER |
michael@0 | 64 | NS_DECL_NSISTREAMLISTENER |
michael@0 | 65 | |
michael@0 | 66 | MyListener() { } |
michael@0 | 67 | virtual ~MyListener() {} |
michael@0 | 68 | }; |
michael@0 | 69 | |
michael@0 | 70 | NS_IMPL_ISUPPORTS(MyListener, nsIStreamListener, nsIRequestObserver) |
michael@0 | 71 | |
michael@0 | 72 | NS_IMETHODIMP |
michael@0 | 73 | MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctx) |
michael@0 | 74 | { |
michael@0 | 75 | return NS_OK; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | NS_IMETHODIMP |
michael@0 | 79 | MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx, |
michael@0 | 80 | nsIInputStream *stream, |
michael@0 | 81 | uint64_t offset, uint32_t count) |
michael@0 | 82 | { |
michael@0 | 83 | nsresult rv; |
michael@0 | 84 | char buf[4096]; |
michael@0 | 85 | uint32_t n, bytesRead; |
michael@0 | 86 | while (count) { |
michael@0 | 87 | n = std::min<uint32_t>(count, sizeof(buf)); |
michael@0 | 88 | rv = stream->Read(buf, n, &bytesRead); |
michael@0 | 89 | if (NS_FAILED(rv)) |
michael@0 | 90 | break; |
michael@0 | 91 | count -= bytesRead; |
michael@0 | 92 | if (bytesRead == 0) |
michael@0 | 93 | break; |
michael@0 | 94 | } |
michael@0 | 95 | return NS_OK; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | NS_IMETHODIMP |
michael@0 | 99 | MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status) |
michael@0 | 100 | { |
michael@0 | 101 | if (NS_FAILED(status)) { |
michael@0 | 102 | nsAutoCString spec; |
michael@0 | 103 | req->GetName(spec); |
michael@0 | 104 | fprintf(stderr, "*** failed loading %s [reason=%x]\n", spec.get(), status); |
michael@0 | 105 | } |
michael@0 | 106 | if (--gRequestCount == 0) { |
michael@0 | 107 | // post shutdown event |
michael@0 | 108 | QuitPumpingEvents(); |
michael@0 | 109 | } |
michael@0 | 110 | return NS_OK; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | static bool |
michael@0 | 114 | load_async_1(nsISupports *element, void *data) |
michael@0 | 115 | { |
michael@0 | 116 | nsCOMPtr<nsIURI> uri( do_QueryInterface(element) ); |
michael@0 | 117 | if (!uri) |
michael@0 | 118 | return true; |
michael@0 | 119 | |
michael@0 | 120 | MyListener *listener = new MyListener(); |
michael@0 | 121 | if (!listener) |
michael@0 | 122 | return true; |
michael@0 | 123 | NS_ADDREF(listener); |
michael@0 | 124 | nsresult rv = NS_OpenURI(listener, nullptr, uri, gIOService); |
michael@0 | 125 | NS_RELEASE(listener); |
michael@0 | 126 | if (NS_SUCCEEDED(rv)) |
michael@0 | 127 | gRequestCount++; |
michael@0 | 128 | else |
michael@0 | 129 | printf(">> NS_OpenURI failed [rv=%x]\n", rv); |
michael@0 | 130 | return true; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | static nsresult |
michael@0 | 134 | load_async(nsISupportsArray *urls) |
michael@0 | 135 | { |
michael@0 | 136 | urls->EnumerateForwards(load_async_1, nullptr); |
michael@0 | 137 | |
michael@0 | 138 | PumpEvents(); |
michael@0 | 139 | return NS_OK; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | //----------------------------------------------------------------------------- |
michael@0 | 143 | |
michael@0 | 144 | static nsresult |
michael@0 | 145 | read_file(const char *fname, nsISupportsArray *urls) |
michael@0 | 146 | { |
michael@0 | 147 | FILE *fp = fopen(fname, "r"); |
michael@0 | 148 | if (!fp) { |
michael@0 | 149 | printf("failed opening file: %s\n", fname); |
michael@0 | 150 | return NS_ERROR_FAILURE; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | nsCOMPtr<nsIURI> uri; |
michael@0 | 154 | nsresult rv; |
michael@0 | 155 | char buf[512]; |
michael@0 | 156 | while (fgets(buf, sizeof(buf), fp)) { |
michael@0 | 157 | // remove trailing newline |
michael@0 | 158 | buf[strlen(buf) - 1] = 0; |
michael@0 | 159 | rv = NS_NewURI(getter_AddRefs(uri), buf, nullptr, gIOService); |
michael@0 | 160 | if (NS_FAILED(rv)) |
michael@0 | 161 | printf("*** ignoring malformed uri: %s\n", buf); |
michael@0 | 162 | else { |
michael@0 | 163 | //nsXPIDLCString spec; |
michael@0 | 164 | //uri->GetSpec(getter_Copies(spec)); |
michael@0 | 165 | //printf("read url: %s\n", spec.get()); |
michael@0 | 166 | urls->AppendElement(uri); |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | fclose(fp); |
michael@0 | 171 | return NS_OK; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | //----------------------------------------------------------------------------- |
michael@0 | 175 | |
michael@0 | 176 | static void |
michael@0 | 177 | print_usage() |
michael@0 | 178 | { |
michael@0 | 179 | printf("usage: TestPerf [-sync|-async] <file-of-urls>\n"); |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | } // namespace |
michael@0 | 183 | |
michael@0 | 184 | using namespace TestPerf; |
michael@0 | 185 | |
michael@0 | 186 | int |
michael@0 | 187 | main(int argc, char **argv) |
michael@0 | 188 | { |
michael@0 | 189 | if (test_common_init(&argc, &argv) != 0) |
michael@0 | 190 | return -1; |
michael@0 | 191 | |
michael@0 | 192 | nsresult rv; |
michael@0 | 193 | bool sync; |
michael@0 | 194 | |
michael@0 | 195 | if (argc < 3) { |
michael@0 | 196 | print_usage(); |
michael@0 | 197 | return -1; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | if (PL_strcasecmp(argv[1], "-sync") == 0) |
michael@0 | 201 | sync = true; |
michael@0 | 202 | else if (PL_strcasecmp(argv[1], "-async") == 0) |
michael@0 | 203 | sync = false; |
michael@0 | 204 | else { |
michael@0 | 205 | print_usage(); |
michael@0 | 206 | return -1; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | nsCOMPtr<nsIServiceManager> servMan; |
michael@0 | 210 | NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); |
michael@0 | 211 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); |
michael@0 | 212 | NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); |
michael@0 | 213 | registrar->AutoRegister(nullptr); |
michael@0 | 214 | |
michael@0 | 215 | // cache the io service |
michael@0 | 216 | { |
michael@0 | 217 | nsCOMPtr<nsIIOService> ioserv( do_GetIOService() ); |
michael@0 | 218 | NS_ADDREF(gIOService = ioserv); |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | nsCOMPtr<nsISupportsArray> urls; |
michael@0 | 222 | rv = NS_NewISupportsArray(getter_AddRefs(urls)); |
michael@0 | 223 | if (NS_FAILED(rv)) return -1; |
michael@0 | 224 | |
michael@0 | 225 | rv = read_file(argv[2], urls); |
michael@0 | 226 | if (NS_FAILED(rv)) { |
michael@0 | 227 | printf("failed reading file-of-urls\n"); |
michael@0 | 228 | return -1; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | uint32_t urlCount; |
michael@0 | 232 | urls->Count(&urlCount); |
michael@0 | 233 | |
michael@0 | 234 | PRIntervalTime start = PR_IntervalNow(); |
michael@0 | 235 | |
michael@0 | 236 | if (sync) |
michael@0 | 237 | rv = load_sync(urls); |
michael@0 | 238 | else |
michael@0 | 239 | rv = load_async(urls); |
michael@0 | 240 | |
michael@0 | 241 | if (NS_FAILED(rv)) { |
michael@0 | 242 | printf("load failed\n"); |
michael@0 | 243 | return -1; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | PRIntervalTime end = PR_IntervalNow(); |
michael@0 | 247 | fprintf(stderr, "read: %u urls; total time: %u milliseconds\n", |
michael@0 | 248 | urlCount, |
michael@0 | 249 | PR_IntervalToMilliseconds(end - start)); |
michael@0 | 250 | |
michael@0 | 251 | NS_RELEASE(gIOService); |
michael@0 | 252 | return 0; |
michael@0 | 253 | } |