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