Added 2 test cases: TestWriteStreams is Scott's performance test (for the cache). TestWriteSpeed plots buffer size vs. throughput.
git-svn-id: svn://10.0.0.236/trunk@59604 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
parent
db9f8fce72
commit
20afc432bc
128
mozilla/netwerk/test/TestWriteSpeed.cpp
Normal file
128
mozilla/netwerk/test/TestWriteSpeed.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
/* -*- 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.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/NPL/
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
|
||||
#include "prio.h"
|
||||
#include "prinrval.h"
|
||||
#include "prmem.h"
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#define MIN_SIZE 4096
|
||||
#define MAX_SIZE (512 * 1024)
|
||||
#define SIZE_INCREMENT 4096
|
||||
#define ITERATIONS 10
|
||||
|
||||
void
|
||||
NS_MeanAndStdDev(double n, double sumOfValues, double sumOfSquaredValues,
|
||||
double *meanResult, double *stdDevResult)
|
||||
{
|
||||
double mean = 0.0, var = 0.0, stdDev = 0.0;
|
||||
if (n > 0.0 && sumOfValues >= 0) {
|
||||
mean = sumOfValues / n;
|
||||
double temp = (n * sumOfSquaredValues) - (sumOfValues * sumOfValues);
|
||||
if (temp < 0.0 || n <= 1)
|
||||
var = 0.0;
|
||||
else
|
||||
var = temp / (n * (n - 1));
|
||||
// for some reason, Windows says sqrt(0.0) is "-1.#J" (?!) so do this:
|
||||
stdDev = var != 0.0 ? sqrt(var) : 0.0;
|
||||
}
|
||||
*meanResult = mean;
|
||||
*stdDevResult = stdDev;
|
||||
}
|
||||
|
||||
int
|
||||
Test(const char* filename)
|
||||
{
|
||||
fprintf(stdout, " size write: mean stddev iters total: mean stddev iters\n");
|
||||
for (PRInt32 size = MIN_SIZE; size <= MAX_SIZE; size += SIZE_INCREMENT) {
|
||||
// create a buffer of stuff to write
|
||||
char* buf = (char*)PR_Malloc(size);
|
||||
if (buf == NULL)
|
||||
return -1;
|
||||
|
||||
// initialize it with a pattern
|
||||
PRInt32 i;
|
||||
char hex[] = "0123456789ABCDEF";
|
||||
for (i = 0; i < size; i++) {
|
||||
buf[i] = hex[i & 0xF];
|
||||
}
|
||||
|
||||
double writeCount = 0, writeRate = 0, writeRateSquared = 0;
|
||||
double totalCount = 0, totalRate = 0, totalRateSquared = 0;
|
||||
for (i = 0; i < ITERATIONS; i++) {
|
||||
PRIntervalTime start = PR_IntervalNow();
|
||||
|
||||
PRFileDesc* fd = PR_Open(filename, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0664);
|
||||
if (fd == NULL)
|
||||
return -1;
|
||||
|
||||
PRIntervalTime writeStart = PR_IntervalNow();
|
||||
PRInt32 rv = PR_Write(fd, buf, size);
|
||||
if (rv < 0) return rv;
|
||||
if (rv != size) return -1;
|
||||
PRIntervalTime writeStop = PR_IntervalNow();
|
||||
|
||||
PRStatus st = PR_Close(fd);
|
||||
if (st == PR_FAILURE) return -1;
|
||||
|
||||
PRIntervalTime stop = PR_IntervalNow();
|
||||
|
||||
PRIntervalTime writeTime = PR_IntervalToMilliseconds(writeStop - writeStart);
|
||||
if (writeTime > 0) {
|
||||
double wr = size / writeTime;
|
||||
writeRate += wr;
|
||||
writeRateSquared += wr * wr;
|
||||
writeCount++;
|
||||
}
|
||||
|
||||
PRIntervalTime totalTime = PR_IntervalToMilliseconds(stop - start);
|
||||
if (totalTime > 0) {
|
||||
double t = size / totalTime;
|
||||
totalRate += t;
|
||||
totalRateSquared += t * t;
|
||||
totalCount++;
|
||||
}
|
||||
}
|
||||
|
||||
PR_Free(buf);
|
||||
|
||||
double writeMean, writeStddev;
|
||||
double totalMean, totalStddev;
|
||||
NS_MeanAndStdDev(writeCount, writeRate, writeRateSquared,
|
||||
&writeMean, &writeStddev);
|
||||
NS_MeanAndStdDev(totalCount, totalRate, totalRateSquared,
|
||||
&totalMean, &totalStddev);
|
||||
fprintf(stdout, "%10d %10.2f %10.2f %10d %10.2f %10.2f %10d\n",
|
||||
size, writeMean, writeStddev, (PRInt32)writeCount,
|
||||
totalMean, totalStddev, (PRInt32)totalCount);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
Test("y:\\foo");
|
||||
}
|
||||
204
mozilla/netwerk/test/TestWriteStream.cpp
Normal file
204
mozilla/netwerk/test/TestWriteStream.cpp
Normal file
@ -0,0 +1,204 @@
|
||||
/* -*- 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.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/NPL/
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The Original Code is mozilla.org code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
|
||||
#include "nsIFileTransportService.h"
|
||||
#include "nsIChannel.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIComponentManager.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsIAllocator.h"
|
||||
#include "nsString.h"
|
||||
#include "nsIFileStream.h"
|
||||
#include "nsIStreamListener.h"
|
||||
#include "nsIEventQueueService.h"
|
||||
#include "nsIEventQueue.h"
|
||||
#include "nsILocalFile.h"
|
||||
#include "nsCOMPtr.h"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
PRUint32 GetStartSeed() {
|
||||
return mStartSeed;
|
||||
}
|
||||
|
||||
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:
|
||||
|
||||
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];
|
||||
|
||||
NS_WITH_SERVICE(nsIFileTransportService, fts, kFileTransportServiceCID, &rv) ;
|
||||
if (NS_FAILED(rv)) return rv ;
|
||||
|
||||
nsCOMPtr<nsILocalFile> fs;
|
||||
rv = NS_NewLocalFile(filename, getter_AddRefs(fs));
|
||||
if (NS_FAILED(rv)) return rv ;
|
||||
|
||||
nsCOMPtr<nsIChannel> transport;
|
||||
rv = fts->CreateTransport(fs, PR_RDWR, "load", 0, 0, getter_AddRefs(transport)) ;
|
||||
if (NS_FAILED(rv)) return rv ;
|
||||
|
||||
rv = transport->OpenOutputStream(startPosition, 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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
nsresult
|
||||
NS_AutoregisterComponents()
|
||||
{
|
||||
nsresult rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, NULL /* default */);
|
||||
return rv;
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
rv = NS_AutoregisterComponents();
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = TestSyncWrites(fileName, 0, length);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed");
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
@ -33,9 +33,11 @@ PROG8 = .\$(OBJDIR)\TestFileTransport.exe
|
||||
PROG9 = .\$(OBJDIR)\TestRes.exe
|
||||
PROGA = .\$(OBJDIR)\TestRawCache.exe
|
||||
PROGB = .\$(OBJDIR)\TestCacheMgr.exe
|
||||
PROGC = .\$(OBJDIR)\TestWriteStream.exe
|
||||
PROGD = .\$(OBJDIR)\TestWriteSpeed.exe
|
||||
PROGRAMS = \
|
||||
$(PROG1) $(PROG2) $(PROG3) $(PROG4) $(PROG5) $(PROG6) $(PROG7) $(PROG8) $(PROG9)\
|
||||
$(PROGA) $(PROGB)
|
||||
$(PROGA) $(PROGB) $(PROGC) $(PROGD)
|
||||
|
||||
LCFLAGS=-DUSE_NSREG -GX
|
||||
|
||||
@ -79,3 +81,8 @@ $(PROG9): $(OBJDIR) TestRes.cpp
|
||||
$(PROGA): $(OBJDIR) TestRawCache.cpp
|
||||
|
||||
$(PROGB): $(OBJDIR) TestCacheMgr.cpp
|
||||
|
||||
$(PROGC): $(OBJDIR) TestWriteStream.cpp
|
||||
|
||||
$(PROGC): $(OBJDIR) TestWriteSpeed.cpp
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user