1999-04-13 22:17:29 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsIByteBufferInputStream.h"
|
1999-05-01 02:55:12 +04:00
|
|
|
#include "nsIBuffer.h"
|
1999-05-07 09:30:30 +04:00
|
|
|
#include "nsIServiceManager.h"
|
1999-04-13 22:17:29 +04:00
|
|
|
#include "prprf.h"
|
1999-04-22 11:31:03 +04:00
|
|
|
#include "prinrval.h"
|
1999-04-13 22:17:29 +04:00
|
|
|
#include "plstr.h"
|
1999-05-01 02:55:12 +04:00
|
|
|
#include "nsCRT.h"
|
1999-04-13 22:17:29 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define KEY 0xa7
|
1999-05-01 02:55:12 +04:00
|
|
|
#define ITERATIONS 33333
|
1999-04-13 22:17:29 +04:00
|
|
|
char kTestPattern[] = "My hovercraft is full of eels.\n";
|
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
PRBool gTrace = PR_FALSE;
|
|
|
|
|
1999-04-13 22:17:29 +04:00
|
|
|
class nsReceiver : public nsIRunnable {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
nsresult rv;
|
|
|
|
char buf[101];
|
|
|
|
PRUint32 count;
|
1999-04-22 11:31:03 +04:00
|
|
|
PRIntervalTime start = PR_IntervalNow();
|
1999-04-13 22:17:29 +04:00
|
|
|
while (PR_TRUE) {
|
|
|
|
rv = mIn->Read(buf, 100, &count);
|
|
|
|
if (rv == NS_BASE_STREAM_EOF) {
|
1999-04-22 11:31:03 +04:00
|
|
|
// printf("EOF count = %d\n", mCount);
|
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
1999-04-13 22:17:29 +04:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("read failed\n");
|
1999-04-22 11:31:03 +04:00
|
|
|
break;
|
1999-04-13 22:17:29 +04:00
|
|
|
}
|
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
if (gTrace) {
|
|
|
|
printf("read: ");
|
|
|
|
buf[count] = '\0';
|
|
|
|
printf(buf);
|
|
|
|
printf("\n");
|
|
|
|
}
|
1999-04-13 22:17:29 +04:00
|
|
|
mCount += count;
|
|
|
|
}
|
1999-04-22 11:31:03 +04:00
|
|
|
PRIntervalTime end = PR_IntervalNow();
|
1999-05-01 02:55:12 +04:00
|
|
|
printf("read %d bytes, time = %dms\n", mCount,
|
|
|
|
PR_IntervalToMilliseconds(end - start));
|
1999-04-13 22:17:29 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsReceiver(nsIInputStream* in) : mIn(in), mCount(0) {
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
NS_ADDREF(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~nsReceiver() {
|
|
|
|
NS_RELEASE(mIn);
|
|
|
|
}
|
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
PRUint32 GetBytesRead() { return mCount; }
|
|
|
|
|
1999-04-13 22:17:29 +04:00
|
|
|
protected:
|
|
|
|
nsIInputStream* mIn;
|
|
|
|
PRUint32 mCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsReceiver, nsIRunnable::GetIID());
|
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
nsresult
|
|
|
|
TestPipe(nsIInputStream* in, nsIOutputStream* out)
|
1999-04-13 22:17:29 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-05-01 02:55:12 +04:00
|
|
|
nsIThread* thread;
|
|
|
|
nsReceiver* receiver = new nsReceiver(in);
|
|
|
|
if (receiver == nsnull) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(receiver);
|
1999-04-13 22:17:29 +04:00
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
rv = NS_NewThread(&thread, receiver);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-04-13 22:17:29 +04:00
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
PRUint32 total = 0;
|
1999-04-22 11:31:03 +04:00
|
|
|
PRIntervalTime start = PR_IntervalNow();
|
1999-04-13 22:17:29 +04:00
|
|
|
for (PRUint32 i = 0; i < ITERATIONS; i++) {
|
|
|
|
PRUint32 writeCount;
|
|
|
|
char* buf = PR_smprintf("%d %s", i, kTestPattern);
|
1999-05-01 02:55:12 +04:00
|
|
|
rv = out->Write(buf, nsCRT::strlen(buf), &writeCount);
|
|
|
|
if (gTrace) {
|
|
|
|
printf("wrote: ");
|
|
|
|
for (PRUint32 j = 0; j < writeCount; j++) {
|
|
|
|
putc(buf[j], stdout);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
1999-04-13 22:17:29 +04:00
|
|
|
PR_smprintf_free(buf);
|
1999-05-01 02:55:12 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
total += writeCount;
|
1999-04-13 22:17:29 +04:00
|
|
|
}
|
|
|
|
rv = out->Close();
|
1999-05-01 02:55:12 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-04-22 11:31:03 +04:00
|
|
|
PRIntervalTime end = PR_IntervalNow();
|
1999-04-13 22:17:29 +04:00
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
thread->Join();
|
|
|
|
|
|
|
|
printf("write %d bytes, time = %dms\n", total,
|
|
|
|
PR_IntervalToMilliseconds(end - start));
|
|
|
|
NS_ASSERTION(receiver->GetBytesRead() == total, "didn't read everything");
|
|
|
|
|
|
|
|
NS_RELEASE(thread);
|
1999-04-13 22:17:29 +04:00
|
|
|
NS_RELEASE(receiver);
|
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsIInputStream* in;
|
|
|
|
nsIOutputStream* out;
|
1999-05-07 09:30:30 +04:00
|
|
|
nsIServiceManager* servMgr;
|
|
|
|
|
|
|
|
rv = NS_InitXPCOM(&servMgr);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// XXX why do I have to do this?!
|
|
|
|
rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup,
|
|
|
|
"components");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-05-01 02:55:12 +04:00
|
|
|
|
|
|
|
if (argc > 1 && nsCRT::strcmp(argv[1], "-trace") == 0)
|
|
|
|
gTrace = PR_TRUE;
|
|
|
|
|
|
|
|
rv = NS_NewPipe(&in, &out, PR_TRUE, 4096 * 4);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("NewPipe failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = TestPipe(in, out);
|
|
|
|
NS_RELEASE(in);
|
|
|
|
NS_RELEASE(out);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("TestPipe failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = NS_NewPipe2(&in, &out, 4096, 4096 * 4);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("NewPipe failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = TestPipe(in, out);
|
|
|
|
NS_RELEASE(in);
|
|
|
|
NS_RELEASE(out);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("TestPipe failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1999-04-13 22:17:29 +04:00
|
|
|
return 0;
|
|
|
|
}
|