Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
TestWriteStream.cpp File Reference
#include "nsIFileTransportService.h"
#include "nsIChannel.h"
#include "nsITransport.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsCOMPtr.h"
#include "nsIMemory.h"
#include "nsString.h"
#include "nsCRT.h"
#include "nsIFileStream.h"
#include "nsIStreamListener.h"
#include "nsIEventQueueService.h"
#include "nsIEventQueue.h"
#include "nsILocalFile.h"
#include "plhash.h"
#include "nsIComponentRegistrar.h"

Go to the source code of this file.


class  nsITestDataStream
class  RandomStream


#define MAX_FILES_TO_WRITE   15


nsresult TestSyncWrite (char *filename, PRUint32 startPosition, PRInt32 length)
nsresult TestSyncWrites (char *filenamePrefix, PRUint32 startPosition, PRInt32 length)
int main (int argc, char *argv[])


PRIntervalTime gDuration
PRUint32 gTotalBytesWritten = 0

Define Documentation

Definition at line 171 of file TestWriteStream.cpp.

Function Documentation

int main ( int  argc,
char *  argv[] 

Definition at line 198 of file TestWriteStream.cpp.

    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]);
        nsCOMPtr<nsIServiceManager> servMan;
        NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
        nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
        NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
        if (registrar)

        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");
    return NS_OK;

Here is the call graph for this function:

static NS_DEFINE_CID ( kFileTransportServiceCID  ,
) [static]
nsresult TestSyncWrite ( char *  filename,
PRUint32  startPosition,
PRInt32  length 

Definition at line 121 of file TestWriteStream.cpp.

    nsresult rv;
    nsCOMPtr<nsIOutputStream> outStream ;
    RandomStream *randomStream;
    char buf[500];

    nsCOMPtr<nsIFileTransportService> fts = 
             do_GetService(kFileTransportServiceCID, &rv) ;
    if (NS_FAILED(rv)) return rv ;

    nsCOMPtr<nsILocalFile> fs;
    rv = NS_NewNativeLocalFile(nsDependentCString(filename), PR_FALSE, getter_AddRefs(fs));
    if (NS_FAILED(rv)) return rv ;

    nsCOMPtr<nsITransport> transport;
    rv = fts->CreateTransport(fs, PR_RDWR | PR_CREATE_FILE, 0664, PR_TRUE,
                              getter_AddRefs(transport)) ;
    if (NS_FAILED(rv)) return rv ;

    rv = transport->OpenOutputStream(startPosition, -1, 0, getter_AddRefs(outStream)) ;
    if (NS_FAILED(rv)) return rv;

    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?");

        remaining -= amount;
    gTotalBytesWritten += length;

    PRIntervalTime endTime = PR_IntervalNow();
    gDuration += (endTime - startTime);

    delete randomStream;

    return NS_OK;

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult TestSyncWrites ( char *  filenamePrefix,
PRUint32  startPosition,
PRInt32  length 

Definition at line 174 of file TestWriteStream.cpp.

    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;

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

Definition at line 115 of file TestWriteStream.cpp.

Definition at line 116 of file TestWriteStream.cpp.