2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 22:01:16 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2000-02-03 01:59:15 +00:00
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2000-02-03 01:59:15 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2000-02-03 01:59:15 +00:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-28 20:14:13 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2000-02-03 01:59:15 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 22:01:16 +00:00
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
2001-09-28 20:14:13 +00:00
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 22:01:16 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-28 20:14:13 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 22:01:16 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2000-02-03 01:59:15 +00:00
|
|
|
|
|
|
|
#include "nsIFileTransportService.h"
|
|
|
|
#include "nsIChannel.h"
|
2001-02-21 20:38:08 +00:00
|
|
|
#include "nsITransport.h"
|
2000-02-03 01:59:15 +00:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2000-06-03 09:46:12 +00:00
|
|
|
#include "nsIMemory.h"
|
2000-02-03 01:59:15 +00:00
|
|
|
#include "nsString.h"
|
2002-05-15 20:44:51 +00:00
|
|
|
#include "nsCRT.h"
|
2000-02-03 01:59:15 +00:00
|
|
|
#include "nsIFileStream.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIEventQueue.h"
|
|
|
|
#include "nsILocalFile.h"
|
2000-03-31 10:08:49 +00:00
|
|
|
#include "plhash.h"
|
2002-01-29 21:22:13 +00:00
|
|
|
#include "nsIComponentRegistrar.h"
|
2000-02-03 01:59:15 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kFileTransportServiceCID, NS_FILETRANSPORTSERVICE_CID);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// A supply of stream data to either store or compare with
|
|
|
|
class nsITestDataStream {
|
|
|
|
public:
|
|
|
|
virtual ~nsITestDataStream() {};
|
|
|
|
virtual PRUint32 Next() = 0;
|
|
|
|
virtual void Read(char* aBuf, PRUint32 aCount) = 0;
|
|
|
|
|
|
|
|
virtual PRBool Match(char* aBuf, PRUint32 aCount) = 0;
|
|
|
|
virtual void Skip(PRUint32 aCount) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A reproducible stream of random data.
|
|
|
|
class RandomStream : public nsITestDataStream {
|
|
|
|
public:
|
|
|
|
RandomStream(PRUint32 aSeed) {
|
|
|
|
mStartSeed = mState = aSeed;
|
|
|
|
}
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2000-02-03 01:59:15 +00:00
|
|
|
PRUint32 GetStartSeed() {
|
|
|
|
return mStartSeed;
|
|
|
|
}
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2000-02-03 01:59:15 +00:00
|
|
|
PRUint32 Next() {
|
|
|
|
mState = 1103515245 * mState + 12345;
|
|
|
|
return mState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Read(char* aBuf, PRUint32 aCount) {
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < aCount; i++) {
|
|
|
|
*aBuf++ = Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
Match(char* aBuf, PRUint32 aCount) {
|
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < aCount; i++) {
|
|
|
|
if (*aBuf++ != (char)(Next() & 0xff))
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Skip(PRUint32 aCount) {
|
|
|
|
while (aCount--)
|
|
|
|
Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2000-02-03 01:59:15 +00:00
|
|
|
PRUint32 mState;
|
|
|
|
PRUint32 mStartSeed;
|
|
|
|
};
|
|
|
|
|
|
|
|
PRIntervalTime gDuration;
|
|
|
|
PRUint32 gTotalBytesWritten = 0;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TestSyncWrite(char* filename, PRUint32 startPosition, PRInt32 length)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIOutputStream> outStream ;
|
|
|
|
RandomStream *randomStream;
|
|
|
|
char buf[500];
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-07-25 07:54:28 +00:00
|
|
|
nsCOMPtr<nsIFileTransportService> fts =
|
|
|
|
do_GetService(kFileTransportServiceCID, &rv) ;
|
2000-02-03 01:59:15 +00:00
|
|
|
if (NS_FAILED(rv)) return rv ;
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2000-02-03 01:59:15 +00:00
|
|
|
nsCOMPtr<nsILocalFile> fs;
|
2002-04-27 06:32:40 +00:00
|
|
|
rv = NS_NewNativeLocalFile(nsDependentCString(filename), PR_FALSE, getter_AddRefs(fs));
|
2000-02-03 01:59:15 +00:00
|
|
|
if (NS_FAILED(rv)) return rv ;
|
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
nsCOMPtr<nsITransport> transport;
|
2002-03-26 23:33:19 +00:00
|
|
|
rv = fts->CreateTransport(fs, PR_RDWR | PR_CREATE_FILE, 0664, PR_TRUE,
|
2000-03-29 03:58:50 +00:00
|
|
|
getter_AddRefs(transport)) ;
|
2000-02-03 01:59:15 +00:00
|
|
|
if (NS_FAILED(rv)) return rv ;
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
rv = transport->OpenOutputStream(startPosition, -1, 0, getter_AddRefs(outStream)) ;
|
2000-02-03 01:59:15 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2000-02-03 01:59:15 +00:00
|
|
|
PRIntervalTime startTime = PR_IntervalNow();
|
|
|
|
|
|
|
|
randomStream = new RandomStream(PL_HashString(filename));
|
|
|
|
|
|
|
|
int remaining = length;
|
|
|
|
while (remaining) {
|
|
|
|
PRUint32 numWritten;
|
|
|
|
int amount = PR_MIN(sizeof buf, remaining);
|
|
|
|
randomStream->Read(buf, amount);
|
|
|
|
|
|
|
|
rv = outStream->Write(buf, amount, &numWritten);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), " ");
|
|
|
|
NS_ASSERTION(numWritten == (PRUint32)amount, "Write() bug?");
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2000-02-03 01:59:15 +00:00
|
|
|
remaining -= amount;
|
|
|
|
}
|
|
|
|
outStream->Close();
|
|
|
|
gTotalBytesWritten += length;
|
|
|
|
|
|
|
|
PRIntervalTime endTime = PR_IntervalNow();
|
|
|
|
gDuration += (endTime - startTime);
|
|
|
|
|
|
|
|
delete randomStream;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_FILES_TO_WRITE 15
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TestSyncWrites(char* filenamePrefix, PRUint32 startPosition, PRInt32 length)
|
|
|
|
{
|
|
|
|
char filename[100];
|
|
|
|
int test;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
for (test = 0; test < MAX_FILES_TO_WRITE; test++) {
|
|
|
|
sprintf(filename, "%s_%d", filenamePrefix, test);
|
|
|
|
|
|
|
|
rv = TestSyncWrite(filename, startPosition, length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
double rate = gTotalBytesWritten / PR_IntervalToMilliseconds(gDuration);
|
|
|
|
rate *= 1000;
|
|
|
|
rate /= (1024 * 1024);
|
|
|
|
printf("Wrote %7d bytes at a rate of %6.2f MB per second \n",
|
|
|
|
gTotalBytesWritten, rate);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2000-02-03 01:59:15 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
printf("usage: %s <file-prefix-to-write> <num-bytes>\n", argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
char* fileName = argv[1];
|
|
|
|
int length = atoi(argv[2]);
|
2002-07-04 14:29:25 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
|
|
|
|
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
|
|
|
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
|
|
|
if (registrar)
|
|
|
|
registrar->AutoRegister(nsnull);
|
|
|
|
|
|
|
|
rv = TestSyncWrites(fileName, 0, length);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed");
|
|
|
|
|
|
|
|
} // this scopes the nsCOMPtrs
|
|
|
|
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
|
|
|
rv = NS_ShutdownXPCOM(nsnull);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
2000-02-03 01:59:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|