Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
TestFileInput.cpp File Reference
#include "nsIFileTransportService.h"
#include "nsIStreamListener.h"
#include "nsIServiceManager.h"
#include "nsIComponentRegistrar.h"
#include "nsIInputStream.h"
#include "nsIEventQueue.h"
#include "nsIEventQueueService.h"
#include "prinrval.h"
#include "prmon.h"
#include "prcmon.h"
#include "prio.h"
#include "nsIFileStreams.h"
#include "nsILocalFile.h"
#include "nsNetUtil.h"
#include "nsIPipe.h"
#include "nsIOutputStream.h"
#include "nsIRunnable.h"
#include "nsIThread.h"
#include "nsCOMArray.h"
#include "nsIChannel.h"
#include "nsCOMPtr.h"
#include <stdio.h>
#include "nsInt64.h"

Go to the source code of this file.

Classes

class  nsReader

Defines

#define NS_FILE_TRANSPORT_BUFFER_SIZE   (4*1024)

Functions

static NS_DEFINE_CID (kFileTransportServiceCID, NS_FILETRANSPORTSERVICE_CID)
static NS_DEFINE_CID (kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID)
nsresult Simulated_nsFileTransport_Run (nsReader *reader, const char *path)
void SerialReadTest (char *dirName)
void ParallelReadTest (char *dirName, nsIFileTransportService *fts)
int main (int argc, char *argv[])

Variables

PRIntervalTime gDuration = 0
PRUint32 gVolume = 0

Define Documentation


Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 398 of file TestFileInput.cpp.

{
    nsresult rv;

    if (argc < 2) {
        printf("usage: %s <dir-to-read-all-files-from>\n", argv[0]);
        return -1;
    }
    char* dirName = argv[1];

    {
        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);

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

        SerialReadTest(dirName);

        //ParallelReadTest(dirName, fts);

        fts->ProcessPendingRequests();

        printf("duration %d ms, volume %d\n",
               PR_IntervalToMilliseconds(gDuration),
               gVolume);
        gVolume = 0;
    } // 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 0;
}

Here is the call graph for this function:

static NS_DEFINE_CID ( kFileTransportServiceCID  ,
NS_FILETRANSPORTSERVICE_CID   
) [static]
void ParallelReadTest ( char *  dirName,
nsIFileTransportService *  fts 
)

Definition at line 334 of file TestFileInput.cpp.

{
    nsresult rv;
    PRStatus status;

    PRDir* dir = PR_OpenDir(dirName);
    NS_ASSERTION(dir, "bad dir");

    nsCOMArray<nsIThread> threads;

    PRDirEntry* entry;
    while ((entry = PR_ReadDir(dir, PR_SKIP_BOTH)) != nsnull) {
        nsCOMPtr<nsILocalFile> localFile;
        rv = NS_NewNativeLocalFile(nsDependentCString(dirName), PR_FALSE, getter_AddRefs(localFile));
        NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewNativeLocalFile failed");

        rv = localFile->AppendNative(nsDependentCString(entry->name));
        NS_ASSERTION(NS_SUCCEEDED(rv), "AppendPath failed");

        nsReader* reader = new nsReader();
        NS_ASSERTION(reader, "out of memory");
        NS_ADDREF(reader);

        nsCOMPtr<nsIThread> readerThread;

        rv = NS_NewThread(getter_AddRefs(readerThread), reader, 0,
                          PR_JOINABLE_THREAD);

        NS_ASSERTION(NS_SUCCEEDED(rv), "new thread failed");

        rv = reader->Init(readerThread);
        NS_ASSERTION(NS_SUCCEEDED(rv), "init failed");

        nsIStreamListener* listener;
        reader->QueryInterface(NS_GET_IID(nsIStreamListener), (void**)&listener);
        NS_ASSERTION(listener, "QI failed");

        nsITransport* trans;
        rv = fts->CreateTransport(localFile, PR_RDONLY, 0, PR_TRUE, &trans);
        NS_ASSERTION(NS_SUCCEEDED(rv), "create failed");
        nsCOMPtr<nsIRequest> request;
        rv = trans->AsyncRead(nsnull, listener, 0, -1, 0, getter_AddRefs(request));
        NS_ASSERTION(NS_SUCCEEDED(rv), "AsyncRead failed");

        // the reader thread will hang on to these objects until it quits
        NS_RELEASE(trans);
        NS_RELEASE(listener);
        NS_RELEASE(reader);
        rv = threads.AppendObject(readerThread) ? NS_OK : NS_ERROR_FAILURE;
        NS_ASSERTION(NS_SUCCEEDED(rv), "AppendElement failed");
    }

    // now that we've forked all the async requests, wait until they're done
    PRUint32 threadCount = threads.Count();
    for (PRUint32 i = 0; i < threadCount; i++) {
        nsIThread* thread = threads.ObjectAt(i);
        thread->Join();
    }

    status = PR_CloseDir(dir);
    NS_ASSERTION(status == PR_SUCCESS, "can't close dir");
}

Here is the call graph for this function:

void SerialReadTest ( char *  dirName)

Definition at line 273 of file TestFileInput.cpp.

{
    nsresult rv;
    PRStatus status;

    PRDir* dir = PR_OpenDir(dirName);
    NS_ASSERTION(dir, "bad dir");

    nsCOMArray<nsIThread> threads;

    PRIntervalTime startTime = PR_IntervalNow();
    PRDirEntry* entry;

    while ((entry = PR_ReadDir(dir, PR_SKIP_BOTH)) != nsnull) {
        char buffer[1024];

        sprintf(buffer, "%s%s%s" dirName, FILE_PATH_SEPARATOR, entry->name);

        nsReader* reader = new nsReader();
        NS_ASSERTION(reader, "out of memory");
        NS_ADDREF(reader);

        nsIThread* readerThread;
        rv = NS_NewThread(&readerThread, reader, 0, PR_JOINABLE_THREAD);
        NS_ASSERTION(NS_SUCCEEDED(rv), "new thread failed");

        rv = reader->Init(readerThread);
        NS_ASSERTION(NS_SUCCEEDED(rv), "init failed");

        // XXXbz shouldn't we put these in our array?

        nsIStreamListener* listener;
        reader->QueryInterface(NS_GET_IID(nsIStreamListener), (void**)&listener);
        NS_ASSERTION(listener, "QI failed");

        rv = Simulated_nsFileTransport_Run(reader, buffer);
        NS_ASSERTION(NS_SUCCEEDED(rv), "Simulated_nsFileTransport_Run failed");

        // the reader thread will hang on to these objects until it quits
        NS_RELEASE(listener);
        NS_RELEASE(readerThread);
        NS_RELEASE(reader);
    }
    PRIntervalTime endTime = PR_IntervalNow();
    printf("duration %d ms, volume %d\n",
           PR_IntervalToMilliseconds(endTime - startTime),
           gVolume);
    gVolume = 0;

    // now that we've forked all the async requests, wait until they're done
    PRUint32 threadCount = threads.Count();
    for (PRUint32 i = 0; i < threadCount; i++) {
        nsIThread* thread = threads.ObjectAt(i);
        thread->Join();
    }

    status = PR_CloseDir(dir);
    NS_ASSERTION(status == PR_SUCCESS, "can't close dir");
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult Simulated_nsFileTransport_Run ( nsReader reader,
const char *  path 
)

Definition at line 219 of file TestFileInput.cpp.

{
    // duplicate the work of nsFileTransport::Run here

#define NS_FILE_TRANSPORT_BUFFER_SIZE (4*1024)

    nsresult rv;
    nsCOMPtr<nsIInputStream> fileStr;
    nsIInputStream* bufStr = nsnull;
    PRUint32 sourceOffset = 0;
    nsCOMPtr<nsIOutputStream> out;
    nsCOMPtr<nsILocalFile> file;

    rv = reader->OnStartRequest(nsnull, nsnull);
    if (NS_FAILED(rv)) goto done;       // XXX should this abort the transfer?

    rv = NS_NewNativeLocalFile(nsDependentCString(path), PR_FALSE, getter_AddRefs(file));
    if (NS_FAILED(rv)) goto done;

    rv = NS_NewLocalFileInputStream(getter_AddRefs(fileStr), file);
    if (NS_FAILED(rv)) goto done;

    rv = NS_NewPipe(&bufStr, getter_AddRefs(out),
                    NS_FILE_TRANSPORT_BUFFER_SIZE,
                    NS_FILE_TRANSPORT_BUFFER_SIZE);
    if (NS_FAILED(rv)) goto done;

    /*
    if ( spec.GetFileSize() == 0) goto done;
    */

    while (PR_TRUE) {
        PRUint32 amt;
                  /* id'l change to FillFrom... */
        PRInt64 size;
        rv = file->GetFileSize(&size);
        if (NS_FAILED(rv)) break;
        rv = out->WriteFrom(fileStr, nsInt64(size), &amt);
        if (NS_FAILED(rv) || amt == 0) break;

        rv = reader->OnDataAvailable(nsnull, nsnull, bufStr, sourceOffset, amt);
        if (NS_FAILED(rv)) break;

        sourceOffset += amt;
    }

  done:
    NS_IF_RELEASE(bufStr);

    rv = reader->OnStopRequest(nsnull, nsnull, rv);
    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 77 of file TestFileInput.cpp.

Definition at line 78 of file TestFileInput.cpp.