The files created are named writetest.
Usage: writetest
Will write
The test is repeated
Example: writetest async 1000000 100000 100 1
#include
#include
#include
#include
#ifdef WIN32
#include
#else
#include
#endif
#include
#include
#ifndef WIN32
#include
#include
#else
#include
#endif
#include
#include
#pragma warning(disable : 4996) // we want to keep the same code for Unix
using namespace std;
class MsgTimer
{
public:
MsgTimer( const char* applname ) {
m_startTime = 0.0;
m_lastTime = 0.0;
m_endTime = 0.0;
m_numMsgs = 0;
strcpy( m_applName, applname );
}
~MsgTimer() { };
// --- timer starts now ---
void start() {
m_startTime = getTimeInSeconds();
m_lastTime = m_startTime;
cerr << "\n" << m_applName << ": ###### MsgTimer started" << endl;
printTimestamp();
}
// --- specify the number of processed messages ---
void stop( long numMsgs ) {
m_endTime = getTimeInSeconds();
m_lastTime = m_endTime;
m_numMsgs = numMsgs;
cerr << "\n" << m_applName << ": ###### MsgTimer stopped after processing " << numMsgs << " messages " << endl;
printTimestamp();
printResult();
}
// --- specify the number of processed messages ---
// returns current msgs/sec
double current( long numMsgs ) {
double currentTime = getTimeInSeconds();
double elapsedTime, mps;
elapsedTime = currentTime - m_startTime;
if( elapsedTime > 0.0 ) mps = (double)numMsgs / elapsedTime;
else mps = 0.0;
cerr << m_applName << ": ###### Accumulated Number of msgs : " << m_numMsgs << endl;
cerr << m_applName << ": ###### Accumulated Elapsed time in sec: " << elapsedTime << endl;
cerr << m_applName << ": ###### Accumulated Msgs-per-sec: " << mps << endl;
elapsedTime = currentTime - m_lastTime;
if( elapsedTime > 0.0 ) mps = (double)(numMsgs-m_numMsgs) / elapsedTime;
else mps = 0.0;
cerr << m_applName << ": ###### Number of msgs since last call: " << numMsgs - m_numMsgs << endl;
cerr << m_applName << ": ###### Partial Elapsed time in sec: " << elapsedTime << endl;
cerr << m_applName << ": ###### Partial Msgs-per-sec: " << mps << endl;
m_numMsgs = numMsgs;
m_lastTime = currentTime;
return mps;
}
// --- prints a timestamp in a common format ---
void printTimestamp() {
time_t t;
char* buf;
t = time(NULL);
//ctime_r(&t,buf);
//buf[strlen(buf)-1]='\0';
buf = ctime(&t);
cerr << m_applName << ": ###### Timestamp: " << buf << endl;
}
static double getTimeInSeconds() {
double t0,t1,t2;
#ifndef WIN32
int r;
struct timeval tp;
struct timezone tzp;
r = gettimeofday(&tp,&tzp);
if (-1 == r) {
cerr << "gettimeofday() failed" << endl;
exit(-1);
}
t1 = (double)tp.tv_sec;
t2 = (double)tp.tv_usec;
t0 = t1+ t2/1000000;
#else
struct _timeb tp;
_ftime(&tp);
t1 = (double)tp.time;
t2 = (double)(tp.millitm/1000.0);
t0 = t1+t2;
#endif
return (t0);
}
private:
// --- prints the result in a common format ---
void printResult() {
double elapsedTime,mps;
elapsedTime = m_endTime - m_startTime;
if( m_numMsgs <= 0 ) {
cerr << "MsgTimer::stop() was not called !" << endl;
return;
}
if (elapsedTime > 0.0) {
mps = (double)m_numMsgs / elapsedTime;
}
else {
mps = 0.0;
}
cerr << m_applName << ": ###### Final Elapsed time in sec: " << elapsedTime << endl;
cerr << m_applName << ": ###### Final Number of msgs : " << m_numMsgs << endl;
cerr << m_applName << ": ###### Final Msgs-per-sec: " << mps << endl;
}
double m_startTime;
double m_lastTime;
double m_endTime;
long m_numMsgs;
char m_applName[512];
};
MsgTimer mt("writetest");
int isSync = 0;
char buffer[1024*1024];
void _testWrite( int cnt, int nummsgs, int printrate, int msgsize )
{
char filename[30];
int msgCount;
int fd;
sprintf( filename, "writetest.%d.dat", cnt );
int oflags = O_RDWR | O_CREAT;
#ifdef WIN32
int pmode = _S_IREAD | _S_IWRITE;
#else
// this flag exists only on Unix
if( isSync==1 ) oflags = oflags | O_SYNC;
mode_t pmode = S_IRUSR | S_IWUSR;
#endif
fd = open( filename, oflags, pmode );
if( fd==-1 ) {
fprintf( stderr, "writetest: Could not create file %s, maybe you did not remove a former instance ?\n", filename );
exit(1);
}
mt.start();
for( msgCount=0; msgCount< nummsgs; msgCount ++ ) {
if( msgCount%10000==0 ) fprintf(stderr,"writetest: written %d msgs\n", msgCount );
if( msgCount%printrate==0 ) {
mt.current( msgCount );
}
write( fd, (char*)buffer, msgsize );
#ifdef WIN32
// flush every message if sync writing (Windows NT only)
if( isSync==1 ) _commit(fd);
#endif
}
close(fd);
mt.stop( msgCount );
}
void usage()
{
fprintf(stderr,"Test the I/O performance by writing several messages to the current directory\n");
fprintf(stderr,"The files created are named writetest.
fprintf(stderr,"Usage: writetest
fprintf(stderr," Will write
fprintf(stderr," The test is repeated
fprintf(stderr,"Example: writetest async 1000000 100000 100 1\n");
exit(1);
}
int
main(int argc, char **argv)
{
if( argc < 6 ) usage();
if( !strcmp(argv[1],"sync") ) isSync = 1;
else {
if( !strcmp(argv[1],"async") ) isSync = 0;
else usage();
}
int nummsgs = atoi(argv[2]); // number of messages to write
int printrate = atoi(argv[3]); // number of tests to run
int msgsize = atoi(argv[4]); // length of the message to write in bytes
int numtests = atoi(argv[5]); // number of tests to run
if( msgsize > sizeof(buffer) ) {
fprintf(stderr,"writetest: msgsize can not be bigger than %ld\n", sizeof(buffer) );
exit(1);
}
fprintf(stderr,"writetest: ARGUMENTS: sync:%d nummsgs:%d printrate:%d msgsize:%d numtests:%d\n",
isSync, nummsgs, printrate, msgsize, numtests );
for( int cnt=0; cnt < numtests; cnt++ ) {
_testWrite( cnt, nummsgs, printrate, msgsize );
}
}
No comments:
Post a Comment