2004-10-22 00:57:17 +04:00
|
|
|
#include "TestCommon.h"
|
2001-11-29 03:50:55 +03:00
|
|
|
#include <stdio.h>
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h" /* should be "plstr.h"? */
|
2001-11-29 03:50:55 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2002-01-30 00:22:13 +03:00
|
|
|
#include "nsIComponentRegistrar.h"
|
2001-11-29 03:50:55 +03:00
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
|
2008-07-28 05:09:10 +04:00
|
|
|
namespace TestPerf {
|
|
|
|
|
2001-11-29 03:50:55 +03:00
|
|
|
static nsIIOService *gIOService = nsnull;
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2001-11-29 03:50:55 +03:00
|
|
|
load_sync_1(nsISupports *element, void *data)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
nsCOMPtr<nsIURI> uri( do_QueryInterface(element) );
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCAutoString spec;
|
2001-11-29 03:50:55 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = NS_OpenURI(getter_AddRefs(stream), uri, gIOService);
|
|
|
|
if (NS_FAILED(rv)) {
|
2002-03-06 10:48:55 +03:00
|
|
|
uri->GetAsciiSpec(spec);
|
2001-11-29 03:50:55 +03:00
|
|
|
fprintf(stderr, "*** failed opening %s [rv=%x]\n", spec.get(), rv);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2001-11-29 03:50:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char buf[4096];
|
|
|
|
PRUint32 bytesRead;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
rv = stream->Read(buf, sizeof(buf), &bytesRead);
|
2003-01-18 05:15:14 +03:00
|
|
|
if (NS_FAILED(rv) || bytesRead == 0) {
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
uri->GetAsciiSpec(spec);
|
|
|
|
fprintf(stderr, "*** failed reading %s [rv=%x]\n", spec.get(), rv);
|
|
|
|
}
|
2001-11-29 03:50:55 +03:00
|
|
|
break;
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
2001-11-29 03:50:55 +03:00
|
|
|
}
|
2002-07-04 18:29:25 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2001-11-29 03:50:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
load_sync(nsISupportsArray *urls)
|
|
|
|
{
|
|
|
|
urls->EnumerateForwards(load_sync_1, nsnull);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static int gRequestCount = 0;
|
|
|
|
|
|
|
|
class MyListener : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
|
2003-01-09 01:35:09 +03:00
|
|
|
MyListener() { }
|
2001-11-29 03:50:55 +03:00
|
|
|
virtual ~MyListener() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(MyListener, nsIStreamListener, nsIRequestObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctx)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx,
|
|
|
|
nsIInputStream *stream,
|
|
|
|
PRUint32 offset, PRUint32 count)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
char buf[4096];
|
|
|
|
PRUint32 n, bytesRead;
|
|
|
|
while (count) {
|
2011-06-25 18:06:02 +04:00
|
|
|
n = NS_MIN<PRUint32>(count, sizeof(buf));
|
2001-11-29 03:50:55 +03:00
|
|
|
rv = stream->Read(buf, n, &bytesRead);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
count -= bytesRead;
|
|
|
|
if (bytesRead == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(status)) {
|
2002-03-21 01:50:33 +03:00
|
|
|
nsCAutoString spec;
|
|
|
|
req->GetName(spec);
|
2003-01-18 05:15:14 +03:00
|
|
|
fprintf(stderr, "*** failed loading %s [reason=%x]\n", spec.get(), status);
|
2001-11-29 03:50:55 +03:00
|
|
|
}
|
|
|
|
if (--gRequestCount == 0) {
|
|
|
|
// post shutdown event
|
2006-05-10 21:30:15 +04:00
|
|
|
QuitPumpingEvents();
|
2001-11-29 03:50:55 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2001-11-29 03:50:55 +03:00
|
|
|
load_async_1(nsISupports *element, void *data)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri( do_QueryInterface(element) );
|
|
|
|
if (!uri)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2001-11-29 03:50:55 +03:00
|
|
|
|
|
|
|
MyListener *listener = new MyListener();
|
|
|
|
if (!listener)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2001-11-29 03:50:55 +03:00
|
|
|
NS_ADDREF(listener);
|
|
|
|
nsresult rv = NS_OpenURI(listener, nsnull, uri, gIOService);
|
|
|
|
NS_RELEASE(listener);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
gRequestCount++;
|
|
|
|
else
|
|
|
|
printf(">> NS_OpenURI failed [rv=%x]\n", rv);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2001-11-29 03:50:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
load_async(nsISupportsArray *urls)
|
|
|
|
{
|
|
|
|
urls->EnumerateForwards(load_async_1, nsnull);
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
PumpEvents();
|
2001-11-29 03:50:55 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
read_file(const char *fname, nsISupportsArray *urls)
|
|
|
|
{
|
|
|
|
FILE *fp = fopen(fname, "r");
|
|
|
|
if (!fp) {
|
|
|
|
printf("failed opening file: %s\n", fname);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv;
|
|
|
|
char buf[512];
|
|
|
|
while (fgets(buf, sizeof(buf), fp)) {
|
|
|
|
// remove trailing newline
|
|
|
|
buf[strlen(buf) - 1] = 0;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), buf, nsnull, gIOService);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
printf("*** ignoring malformed uri: %s\n", buf);
|
|
|
|
else {
|
|
|
|
//nsXPIDLCString spec;
|
|
|
|
//uri->GetSpec(getter_Copies(spec));
|
|
|
|
//printf("read url: %s\n", spec.get());
|
|
|
|
urls->AppendElement(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_usage()
|
|
|
|
{
|
|
|
|
printf("usage: TestPerf [-sync|-async] <file-of-urls>\n");
|
|
|
|
}
|
|
|
|
|
2008-07-28 05:09:10 +04:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
using namespace TestPerf;
|
|
|
|
|
2001-11-29 03:50:55 +03:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2004-10-22 00:57:17 +04:00
|
|
|
if (test_common_init(&argc, &argv) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2001-11-29 03:50:55 +03:00
|
|
|
nsresult rv;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool sync;
|
2001-11-29 03:50:55 +03:00
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
print_usage();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PL_strcasecmp(argv[1], "-sync") == 0)
|
2011-10-17 18:59:28 +04:00
|
|
|
sync = true;
|
2001-11-29 03:50:55 +03:00
|
|
|
else if (PL_strcasecmp(argv[1], "-async") == 0)
|
2011-10-17 18:59:28 +04:00
|
|
|
sync = false;
|
2001-11-29 03:50:55 +03:00
|
|
|
else {
|
|
|
|
print_usage();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-01-30 00:22:13 +03:00
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
|
|
|
|
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
|
|
|
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
|
|
|
registrar->AutoRegister(nsnull);
|
2001-11-29 03:50:55 +03:00
|
|
|
|
|
|
|
// cache the io service
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIIOService> ioserv( do_GetIOService() );
|
|
|
|
NS_ADDREF(gIOService = ioserv);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsArray> urls;
|
|
|
|
rv = NS_NewISupportsArray(getter_AddRefs(urls));
|
|
|
|
if (NS_FAILED(rv)) return -1;
|
|
|
|
|
|
|
|
rv = read_file(argv[2], urls);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("failed reading file-of-urls\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 urlCount;
|
|
|
|
urls->Count(&urlCount);
|
|
|
|
|
|
|
|
PRIntervalTime start = PR_IntervalNow();
|
|
|
|
|
|
|
|
if (sync)
|
|
|
|
rv = load_sync(urls);
|
|
|
|
else
|
|
|
|
rv = load_async(urls);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("load failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIntervalTime end = PR_IntervalNow();
|
|
|
|
fprintf(stderr, "read: %u urls; total time: %u milliseconds\n",
|
|
|
|
urlCount,
|
|
|
|
PR_IntervalToMilliseconds(end - start));
|
|
|
|
|
|
|
|
NS_RELEASE(gIOService);
|
|
|
|
return 0;
|
|
|
|
}
|