|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsIServiceManager.h" |
|
7 #include "nsIComponentRegistrar.h" |
|
8 #include "nsIInputStream.h" |
|
9 #include "nsIOutputStream.h" |
|
10 #include "nsIRunnable.h" |
|
11 #include "nsIThread.h" |
|
12 #include "nsCOMArray.h" |
|
13 #include "nsISimpleEnumerator.h" |
|
14 #include "prinrval.h" |
|
15 #include "nsIFileStreams.h" |
|
16 #include "nsIFileChannel.h" |
|
17 #include "nsIFile.h" |
|
18 #include "nsNetUtil.h" |
|
19 #include <stdio.h> |
|
20 |
|
21 //////////////////////////////////////////////////////////////////////////////// |
|
22 |
|
23 #include <math.h> |
|
24 #include "prprf.h" |
|
25 #include "nsAutoLock.h" |
|
26 |
|
27 class nsTimeSampler { |
|
28 public: |
|
29 nsTimeSampler(); |
|
30 void Reset(); |
|
31 void StartTime(); |
|
32 void EndTime(); |
|
33 void AddTime(PRIntervalTime time); |
|
34 PRIntervalTime LastInterval() { return mLastInterval; } |
|
35 char* PrintStats(); |
|
36 protected: |
|
37 PRIntervalTime mStartTime; |
|
38 double mSquares; |
|
39 double mTotalTime; |
|
40 uint32_t mCount; |
|
41 PRIntervalTime mLastInterval; |
|
42 }; |
|
43 |
|
44 nsTimeSampler::nsTimeSampler() |
|
45 { |
|
46 Reset(); |
|
47 } |
|
48 |
|
49 void |
|
50 nsTimeSampler::Reset() |
|
51 { |
|
52 mStartTime = 0; |
|
53 mSquares = 0; |
|
54 mTotalTime = 0; |
|
55 mCount = 0; |
|
56 mLastInterval = 0; |
|
57 } |
|
58 |
|
59 void |
|
60 nsTimeSampler::StartTime() |
|
61 { |
|
62 mStartTime = PR_IntervalNow(); |
|
63 } |
|
64 |
|
65 void |
|
66 nsTimeSampler::EndTime() |
|
67 { |
|
68 NS_ASSERTION(mStartTime != 0, "Forgot to call StartTime"); |
|
69 PRIntervalTime endTime = PR_IntervalNow(); |
|
70 mLastInterval = endTime - mStartTime; |
|
71 AddTime(mLastInterval); |
|
72 mStartTime = 0; |
|
73 } |
|
74 |
|
75 void |
|
76 nsTimeSampler::AddTime(PRIntervalTime time) |
|
77 { |
|
78 nsAutoCMonitor mon(this); |
|
79 mTotalTime += time; |
|
80 mSquares += (double)time * (double)time; |
|
81 mCount++; |
|
82 } |
|
83 |
|
84 char* |
|
85 nsTimeSampler::PrintStats() |
|
86 { |
|
87 double mean = mTotalTime / mCount; |
|
88 double variance = fabs(mSquares / mCount - mean * mean); |
|
89 double stddev = sqrt(variance); |
|
90 uint32_t imean = (uint32_t)mean; |
|
91 uint32_t istddev = (uint32_t)stddev; |
|
92 return PR_smprintf("%d +/- %d ms", |
|
93 PR_IntervalToMilliseconds(imean), |
|
94 PR_IntervalToMilliseconds(istddev)); |
|
95 } |
|
96 |
|
97 //////////////////////////////////////////////////////////////////////////////// |
|
98 |
|
99 nsTimeSampler gTimeSampler; |
|
100 |
|
101 typedef nsresult (*CreateFun)(nsIRunnable* *result, |
|
102 nsIFile* inPath, |
|
103 nsIFile* outPath, |
|
104 uint32_t bufferSize); |
|
105 |
|
106 //////////////////////////////////////////////////////////////////////////////// |
|
107 |
|
108 nsresult |
|
109 Copy(nsIInputStream* inStr, nsIOutputStream* outStr, |
|
110 char* buf, uint32_t bufSize, uint32_t *copyCount) |
|
111 { |
|
112 nsresult rv; |
|
113 while (true) { |
|
114 uint32_t count; |
|
115 rv = inStr->Read(buf, bufSize, &count); |
|
116 if (NS_FAILED(rv)) return rv; |
|
117 if (count == 0) break; |
|
118 |
|
119 uint32_t writeCount; |
|
120 rv = outStr->Write(buf, count, &writeCount); |
|
121 if (NS_FAILED(rv)) return rv; |
|
122 NS_ASSERTION(writeCount == count, "didn't write all the data"); |
|
123 *copyCount += writeCount; |
|
124 } |
|
125 rv = outStr->Flush(); |
|
126 return rv; |
|
127 } |
|
128 |
|
129 //////////////////////////////////////////////////////////////////////////////// |
|
130 |
|
131 class FileSpecWorker : public nsIRunnable { |
|
132 public: |
|
133 |
|
134 NS_IMETHOD Run() { |
|
135 nsresult rv; |
|
136 |
|
137 PRIntervalTime startTime = PR_IntervalNow(); |
|
138 PRIntervalTime endTime; |
|
139 nsCOMPtr<nsIInputStream> inStr; |
|
140 nsCOMPtr<nsIOutputStream> outStr; |
|
141 uint32_t copyCount = 0; |
|
142 |
|
143 // Open the input stream: |
|
144 nsCOMPtr<nsIInputStream> fileIn; |
|
145 rv = NS_NewLocalFileInputStream(getter_AddRefs(fileIn), mInPath); |
|
146 if (NS_FAILED(rv)) return rv; |
|
147 |
|
148 rv = NS_NewBufferedInputStream(getter_AddRefs(inStr), fileIn, 65535); |
|
149 if (NS_FAILED(rv)) return rv; |
|
150 |
|
151 // Open the output stream: |
|
152 nsCOMPtr<nsIOutputStream> fileOut; |
|
153 rv = NS_NewLocalFileOutputStream(getter_AddRefs(fileOut), |
|
154 mOutPath, |
|
155 PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE, |
|
156 0664); |
|
157 if (NS_FAILED(rv)) return rv; |
|
158 |
|
159 rv = NS_NewBufferedOutputStream(getter_AddRefs(outStr), fileOut, 65535); |
|
160 if (NS_FAILED(rv)) return rv; |
|
161 |
|
162 // Copy from one to the other |
|
163 rv = Copy(inStr, outStr, mBuffer, mBufferSize, ©Count); |
|
164 if (NS_FAILED(rv)) return rv; |
|
165 |
|
166 endTime = PR_IntervalNow(); |
|
167 gTimeSampler.AddTime(endTime - startTime); |
|
168 |
|
169 return rv; |
|
170 } |
|
171 |
|
172 NS_DECL_ISUPPORTS |
|
173 |
|
174 FileSpecWorker() |
|
175 : mInPath(nullptr), mOutPath(nullptr), mBuffer(nullptr), |
|
176 mBufferSize(0) |
|
177 { |
|
178 } |
|
179 |
|
180 nsresult Init(nsIFile* inPath, nsIFile* outPath, |
|
181 uint32_t bufferSize) |
|
182 { |
|
183 mInPath = inPath; |
|
184 mOutPath = outPath; |
|
185 mBuffer = new char[bufferSize]; |
|
186 mBufferSize = bufferSize; |
|
187 return (mInPath && mOutPath && mBuffer) |
|
188 ? NS_OK : NS_ERROR_OUT_OF_MEMORY; |
|
189 } |
|
190 |
|
191 static nsresult Create(nsIRunnable* *result, |
|
192 nsIFile* inPath, |
|
193 nsIFile* outPath, |
|
194 uint32_t bufferSize) |
|
195 { |
|
196 FileSpecWorker* worker = new FileSpecWorker(); |
|
197 if (worker == nullptr) |
|
198 return NS_ERROR_OUT_OF_MEMORY; |
|
199 NS_ADDREF(worker); |
|
200 |
|
201 nsresult rv = worker->Init(inPath, outPath, bufferSize); |
|
202 if (NS_FAILED(rv)) { |
|
203 NS_RELEASE(worker); |
|
204 return rv; |
|
205 } |
|
206 *result = worker; |
|
207 return NS_OK; |
|
208 } |
|
209 |
|
210 virtual ~FileSpecWorker() { |
|
211 delete[] mBuffer; |
|
212 } |
|
213 |
|
214 protected: |
|
215 nsCOMPtr<nsIFile> mInPath; |
|
216 nsCOMPtr<nsIFile> mOutPath; |
|
217 char* mBuffer; |
|
218 uint32_t mBufferSize; |
|
219 }; |
|
220 |
|
221 NS_IMPL_ISUPPORTS(FileSpecWorker, nsIRunnable) |
|
222 |
|
223 //////////////////////////////////////////////////////////////////////////////// |
|
224 |
|
225 #include "nsIIOService.h" |
|
226 #include "nsIChannel.h" |
|
227 |
|
228 class FileChannelWorker : public nsIRunnable { |
|
229 public: |
|
230 |
|
231 NS_IMETHOD Run() { |
|
232 nsresult rv; |
|
233 |
|
234 PRIntervalTime startTime = PR_IntervalNow(); |
|
235 PRIntervalTime endTime; |
|
236 uint32_t copyCount = 0; |
|
237 nsCOMPtr<nsIFileChannel> inCh; |
|
238 nsCOMPtr<nsIFileChannel> outCh; |
|
239 nsCOMPtr<nsIInputStream> inStr; |
|
240 nsCOMPtr<nsIOutputStream> outStr; |
|
241 |
|
242 rv = NS_NewLocalFileChannel(getter_AddRefs(inCh), mInPath); |
|
243 if (NS_FAILED(rv)) return rv; |
|
244 |
|
245 rv = inCh->Open(getter_AddRefs(inStr)); |
|
246 if (NS_FAILED(rv)) return rv; |
|
247 |
|
248 //rv = NS_NewLocalFileChannel(getter_AddRefs(outCh), mOutPath); |
|
249 //if (NS_FAILED(rv)) return rv; |
|
250 |
|
251 //rv = outCh->OpenOutputStream(0, -1, 0, getter_AddRefs(outStr)); |
|
252 //if (NS_FAILED(rv)) return rv; |
|
253 |
|
254 // Copy from one to the other |
|
255 rv = Copy(inStr, outStr, mBuffer, mBufferSize, ©Count); |
|
256 if (NS_FAILED(rv)) return rv; |
|
257 |
|
258 endTime = PR_IntervalNow(); |
|
259 gTimeSampler.AddTime(endTime - startTime); |
|
260 |
|
261 return rv; |
|
262 } |
|
263 |
|
264 NS_DECL_ISUPPORTS |
|
265 |
|
266 FileChannelWorker() |
|
267 : mInPath(nullptr), mOutPath(nullptr), mBuffer(nullptr), |
|
268 mBufferSize(0) |
|
269 { |
|
270 } |
|
271 |
|
272 nsresult Init(nsIFile* inPath, nsIFile* outPath, |
|
273 uint32_t bufferSize) |
|
274 { |
|
275 mInPath = inPath; |
|
276 mOutPath = outPath; |
|
277 mBuffer = new char[bufferSize]; |
|
278 mBufferSize = bufferSize; |
|
279 return (mInPath && mOutPath && mBuffer) |
|
280 ? NS_OK : NS_ERROR_OUT_OF_MEMORY; |
|
281 } |
|
282 |
|
283 static nsresult Create(nsIRunnable* *result, |
|
284 nsIFile* inPath, |
|
285 nsIFile* outPath, |
|
286 uint32_t bufferSize) |
|
287 { |
|
288 FileChannelWorker* worker = new FileChannelWorker(); |
|
289 if (worker == nullptr) |
|
290 return NS_ERROR_OUT_OF_MEMORY; |
|
291 NS_ADDREF(worker); |
|
292 |
|
293 nsresult rv = worker->Init(inPath, outPath, bufferSize); |
|
294 if (NS_FAILED(rv)) { |
|
295 NS_RELEASE(worker); |
|
296 return rv; |
|
297 } |
|
298 *result = worker; |
|
299 return NS_OK; |
|
300 } |
|
301 |
|
302 virtual ~FileChannelWorker() { |
|
303 delete[] mBuffer; |
|
304 } |
|
305 |
|
306 protected: |
|
307 nsCOMPtr<nsIFile> mInPath; |
|
308 nsCOMPtr<nsIFile> mOutPath; |
|
309 char* mBuffer; |
|
310 uint32_t mBufferSize; |
|
311 }; |
|
312 |
|
313 NS_IMPL_ISUPPORTS(FileChannelWorker, nsIRunnable) |
|
314 |
|
315 //////////////////////////////////////////////////////////////////////////////// |
|
316 |
|
317 void |
|
318 Test(CreateFun create, uint32_t count, |
|
319 nsIFile* inDirSpec, nsIFile* outDirSpec, uint32_t bufSize) |
|
320 { |
|
321 nsresult rv; |
|
322 uint32_t i; |
|
323 |
|
324 nsAutoCString inDir; |
|
325 nsAutoCString outDir; |
|
326 (void)inDirSpec->GetNativePath(inDir); |
|
327 (void)outDirSpec->GetNativePath(outDir); |
|
328 printf("###########\nTest: from %s to %s, bufSize = %d\n", |
|
329 inDir.get(), outDir.get(), bufSize); |
|
330 gTimeSampler.Reset(); |
|
331 nsTimeSampler testTime; |
|
332 testTime.StartTime(); |
|
333 |
|
334 nsCOMArray<nsIThread> threads; |
|
335 |
|
336 nsCOMPtr<nsISimpleEnumerator> entries; |
|
337 rv = inDirSpec->GetDirectoryEntries(getter_AddRefs(entries)); |
|
338 NS_ASSERTION(NS_SUCCEEDED(rv), "GetDirectoryEntries failed"); |
|
339 |
|
340 i = 0; |
|
341 bool hasMore; |
|
342 while (i < count && NS_SUCCEEDED(entries->HasMoreElements(&hasMore)) && hasMore) { |
|
343 nsCOMPtr<nsISupports> next; |
|
344 rv = entries->GetNext(getter_AddRefs(next)); |
|
345 if (NS_FAILED(rv)) goto done; |
|
346 |
|
347 nsCOMPtr<nsIFile> inSpec = do_QueryInterface(next, &rv); |
|
348 if (NS_FAILED(rv)) goto done; |
|
349 |
|
350 nsCOMPtr<nsIFile> outSpec; |
|
351 rv = outDirSpec->Clone(getter_AddRefs(outSpec)); // don't munge the original |
|
352 if (NS_FAILED(rv)) goto done; |
|
353 |
|
354 nsAutoCString leafName; |
|
355 rv = inSpec->GetNativeLeafName(leafName); |
|
356 if (NS_FAILED(rv)) goto done; |
|
357 |
|
358 rv = outSpec->AppendNative(leafName); |
|
359 if (NS_FAILED(rv)) goto done; |
|
360 |
|
361 bool exists; |
|
362 rv = outSpec->Exists(&exists); |
|
363 if (NS_FAILED(rv)) goto done; |
|
364 |
|
365 if (exists) { |
|
366 rv = outSpec->Remove(false); |
|
367 if (NS_FAILED(rv)) goto done; |
|
368 } |
|
369 |
|
370 nsCOMPtr<nsIThread> thread; |
|
371 nsCOMPtr<nsIRunnable> worker; |
|
372 rv = create(getter_AddRefs(worker), |
|
373 inSpec, |
|
374 outSpec, |
|
375 bufSize); |
|
376 if (NS_FAILED(rv)) goto done; |
|
377 |
|
378 rv = NS_NewThread(getter_AddRefs(thread), worker, 0, PR_JOINABLE_THREAD); |
|
379 if (NS_FAILED(rv)) goto done; |
|
380 |
|
381 bool inserted = threads.InsertObjectAt(thread, i); |
|
382 NS_ASSERTION(inserted, "not inserted"); |
|
383 |
|
384 i++; |
|
385 } |
|
386 |
|
387 uint32_t j; |
|
388 for (j = 0; j < i; j++) { |
|
389 nsIThread* thread = threads.ObjectAt(j); |
|
390 thread->Join(); |
|
391 } |
|
392 |
|
393 done: |
|
394 NS_ASSERTION(rv == NS_OK, "failed"); |
|
395 |
|
396 testTime.EndTime(); |
|
397 char* testStats = testTime.PrintStats(); |
|
398 char* workerStats = gTimeSampler.PrintStats(); |
|
399 printf(" threads = %d\n work time = %s,\n test time = %s\n", |
|
400 i, workerStats, testStats); |
|
401 PR_smprintf_free(workerStats); |
|
402 PR_smprintf_free(testStats); |
|
403 } |
|
404 |
|
405 //////////////////////////////////////////////////////////////////////////////// |
|
406 |
|
407 int |
|
408 main(int argc, char* argv[]) |
|
409 { |
|
410 nsresult rv; |
|
411 |
|
412 if (argc < 2) { |
|
413 printf("usage: %s <in-dir> <out-dir>\n", argv[0]); |
|
414 return -1; |
|
415 } |
|
416 char* inDir = argv[1]; |
|
417 char* outDir = argv[2]; |
|
418 |
|
419 { |
|
420 nsCOMPtr<nsIServiceManager> servMan; |
|
421 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); |
|
422 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); |
|
423 NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); |
|
424 if (registrar) |
|
425 registrar->AutoRegister(nullptr); |
|
426 |
|
427 nsCOMPtr<nsIFile> inDirFile; |
|
428 rv = NS_NewNativeLocalFile(nsDependentCString(inDir), false, getter_AddRefs(inDirFile)); |
|
429 if (NS_FAILED(rv)) return rv; |
|
430 |
|
431 nsCOMPtr<nsIFile> outDirFile; |
|
432 rv = NS_NewNativeLocalFile(nsDependentCString(outDir), false, getter_AddRefs(outDirFile)); |
|
433 if (NS_FAILED(rv)) return rv; |
|
434 |
|
435 CreateFun create = FileChannelWorker::Create; |
|
436 Test(create, 1, inDirFile, outDirFile, 16 * 1024); |
|
437 #if 1 |
|
438 printf("FileChannelWorker *****************************\n"); |
|
439 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
440 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
441 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
442 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
443 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
444 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
445 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
446 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
447 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
448 #endif |
|
449 create = FileSpecWorker::Create; |
|
450 printf("FileSpecWorker ********************************\n"); |
|
451 #if 1 |
|
452 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
453 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
454 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
455 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
456 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
457 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
458 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
459 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
460 Test(create, 20, inDirFile, outDirFile, 16 * 1024); |
|
461 #endif |
|
462 #if 1 |
|
463 Test(create, 20, inDirFile, outDirFile, 4 * 1024); |
|
464 Test(create, 20, inDirFile, outDirFile, 4 * 1024); |
|
465 Test(create, 20, inDirFile, outDirFile, 4 * 1024); |
|
466 Test(create, 20, inDirFile, outDirFile, 4 * 1024); |
|
467 Test(create, 20, inDirFile, outDirFile, 4 * 1024); |
|
468 Test(create, 20, inDirFile, outDirFile, 4 * 1024); |
|
469 Test(create, 20, inDirFile, outDirFile, 4 * 1024); |
|
470 Test(create, 20, inDirFile, outDirFile, 4 * 1024); |
|
471 Test(create, 20, inDirFile, outDirFile, 4 * 1024); |
|
472 #endif |
|
473 } // this scopes the nsCOMPtrs |
|
474 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM |
|
475 rv = NS_ShutdownXPCOM(nullptr); |
|
476 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); |
|
477 return 0; |
|
478 } |
|
479 |
|
480 //////////////////////////////////////////////////////////////////////////////// |