made JitterTester work on windows

This commit is contained in:
wangyix 2014-08-14 11:31:31 -07:00
parent 395f643f1b
commit 029ef962b7

View file

@ -13,6 +13,7 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
#include <cerrno>
#include <stdio.h>
#include <MovingMinMaxAvg.h> // for MovingMinMaxAvg
@ -58,54 +59,64 @@ int main(int argc, const char * argv[]) {
void runSend(const char* addressOption, int port, int gap, int size, int report) {
std::cout << "runSend...\n";
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed with error %d\n", WSAGetLastError());
return;
}
#endif
int sockfd;
struct sockaddr_in servaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(addressOption);
servaddr.sin_port = htons(port);
const int SAMPLES_FOR_30_SECONDS = 30 * 1000000 / gap;
std::cout << "SAMPLES_FOR_30_SECONDS:" << SAMPLES_FOR_30_SECONDS << "\n";
const int SAMPLES_PER_REPORT = report * MSEC_TO_USEC / gap;
std::cout << "SAMPLES_PER_REPORT:" << SAMPLES_PER_REPORT << "\n";
char* outputBuffer = new char[size];
memset(outputBuffer, 0, size);
quint16 outgoingSequenceNumber = 0;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr(addressOption);
servaddr.sin_port=htons(port);
const int SAMPLES_FOR_30_SECONDS = 30 * 1000000 / gap;
std::cout << "SAMPLES_FOR_30_SECONDS:" << SAMPLES_FOR_30_SECONDS << "\n";
MovingMinMaxAvg<int> timeGaps(1, SAMPLES_FOR_30_SECONDS); // stats
const int SAMPLES_PER_REPORT = report * MSEC_TO_USEC / gap;
std::cout << "SAMPLES_PER_REPORT:" << SAMPLES_PER_REPORT << "\n";
MovingMinMaxAvg<int> timeGaps(1, SAMPLES_FOR_30_SECONDS);
MovingMinMaxAvg<int> timeGapsPerReport(1, SAMPLES_PER_REPORT);
StDev stDevReportInterval;
StDev stDev30s;
StDev stDev;
quint64 last = usecTimestampNow();
quint64 lastReport = 0;
while (true) {
quint64 now = usecTimestampNow();
int actualGap = now - last;
if (actualGap >= gap) {
// pack seq num
memcpy(outputBuffer, &outgoingSequenceNumber, sizeof(quint16));
sendto(sockfd, outputBuffer, size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
int n = sendto(sockfd, outputBuffer, size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
if (n < 0) {
std::cout << "Send error: " << strerror(errno) << "\n";
}
outgoingSequenceNumber++;
int gapDifferece = actualGap - gap;
timeGaps.update(gapDifferece);
timeGapsPerReport.update(gapDifferece);
@ -117,23 +128,23 @@ void runSend(const char* addressOption, int port, int gap, int size, int report)
if (now - lastReport >= (report * MSEC_TO_USEC)) {
std::cout << "\n"
<< "SEND gap Difference From Expected\n"
<< "Overall:\n"
<< "min: " << timeGaps.getMin() << " usecs, "
<< "max: " << timeGaps.getMax() << " usecs, "
<< "avg: " << timeGaps.getAverage() << " usecs, "
<< "stdev: " << stDev.getStDev() << " usecs\n"
<< "Last 30s:\n"
<< "min: " << timeGaps.getWindowMin() << " usecs, "
<< "max: " << timeGaps.getWindowMax() << " usecs, "
<< "avg: " << timeGaps.getWindowAverage() << " usecs, "
<< "stdev: " << stDev30s.getStDev() << " usecs\n"
<< "Last report interval:\n"
<< "min: " << timeGapsPerReport.getWindowMin() << " usecs, "
<< "max: " << timeGapsPerReport.getWindowMax() << " usecs, "
<< "avg: " << timeGapsPerReport.getWindowAverage() << " usecs, "
<< "stdev: " << stDevReportInterval.getStDev() << " usecs\n"
<< "\n";
<< "SEND gap Difference From Expected\n"
<< "Overall:\n"
<< "min: " << timeGaps.getMin() << " usecs, "
<< "max: " << timeGaps.getMax() << " usecs, "
<< "avg: " << timeGaps.getAverage() << " usecs, "
<< "stdev: " << stDev.getStDev() << " usecs\n"
<< "Last 30s:\n"
<< "min: " << timeGaps.getWindowMin() << " usecs, "
<< "max: " << timeGaps.getWindowMax() << " usecs, "
<< "avg: " << timeGaps.getWindowAverage() << " usecs, "
<< "stdev: " << stDev30s.getStDev() << " usecs\n"
<< "Last report interval:\n"
<< "min: " << timeGapsPerReport.getWindowMin() << " usecs, "
<< "max: " << timeGapsPerReport.getWindowMax() << " usecs, "
<< "avg: " << timeGapsPerReport.getWindowAverage() << " usecs, "
<< "stdev: " << stDevReportInterval.getStDev() << " usecs\n"
<< "\n";
stDevReportInterval.reset();
if (stDev30s.getSamples() > SAMPLES_FOR_30_SECONDS) {
@ -144,62 +155,75 @@ void runSend(const char* addressOption, int port, int gap, int size, int report)
}
}
}
delete[] outputBuffer;
#ifdef _WIN32
WSACleanup();
#endif
}
void runReceive(const char* addressOption, int port, int gap, int size, int report) {
std::cout << "runReceive...\n";
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed with error %d\n", WSAGetLastError());
return;
}
#endif
int sockfd,n;
int sockfd, n;
struct sockaddr_in myaddr;
char* inputBuffer = new char[size];
memset(inputBuffer, 0, size);
sockfd=socket(AF_INET, SOCK_DGRAM, 0);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
myaddr.sin_port=htons(port);
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(port);
const int SAMPLES_FOR_30_SECONDS = 30 * 1000000 / gap;
std::cout << "SAMPLES_FOR_30_SECONDS:" << SAMPLES_FOR_30_SECONDS << "\n";
MovingMinMaxAvg<int> timeGaps(1, SAMPLES_FOR_30_SECONDS); // stats
const int SAMPLES_PER_REPORT = report * MSEC_TO_USEC / gap;
std::cout << "SAMPLES_PER_REPORT:" << SAMPLES_PER_REPORT << "\n";
MovingMinMaxAvg<int> timeGapsPerReport(1, SAMPLES_PER_REPORT);
const int REPORTS_FOR_30_SECONDS = 30 * MSECS_PER_SECOND / report;
std::cout << "REPORTS_FOR_30_SECONDS:" << REPORTS_FOR_30_SECONDS << "\n";
char* inputBuffer = new char[size];
memset(inputBuffer, 0, size);
MovingMinMaxAvg<int> timeGaps(1, SAMPLES_FOR_30_SECONDS);
MovingMinMaxAvg<int> timeGapsPerReport(1, SAMPLES_PER_REPORT);
SequenceNumberStats seqStats(REPORTS_FOR_30_SECONDS);
StDev stDevReportInterval;
StDev stDev30s;
StDev stDev;
if (bind(sockfd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
std::cout << "bind failed\n";
return;
}
}
quint64 last = 0; // first case
quint64 lastReport = 0;
while (true) {
n = recvfrom(sockfd, inputBuffer, size, 0, NULL, NULL); // we don't care about where it came from
if (n < 0) {
std::cout << "Receive error: " << strerror(errno) << "\n";
}
// parse seq num
quint16 incomingSequenceNumber = *(reinterpret_cast<quint16*>(inputBuffer));
seqStats.sequenceNumberReceived(incomingSequenceNumber);
if (last == 0) {
last = usecTimestampNow();
std::cout << "first packet received\n";
@ -213,28 +237,28 @@ void runReceive(const char* addressOption, int port, int gap, int size, int repo
stDev30s.addValue(gapDifferece);
stDevReportInterval.addValue(gapDifferece);
last = now;
if (now - lastReport >= (report * MSEC_TO_USEC)) {
seqStats.pushStatsToHistory();
std::cout << "RECEIVE gap Difference From Expected\n"
<< "Overall:\n"
<< "min: " << timeGaps.getMin() << " usecs, "
<< "max: " << timeGaps.getMax() << " usecs, "
<< "avg: " << timeGaps.getAverage() << " usecs, "
<< "stdev: " << stDev.getStDev() << " usecs\n"
<< "Last 30s:\n"
<< "min: " << timeGaps.getWindowMin() << " usecs, "
<< "max: " << timeGaps.getWindowMax() << " usecs, "
<< "avg: " << timeGaps.getWindowAverage() << " usecs, "
<< "stdev: " << stDev30s.getStDev() << " usecs\n"
<< "Last report interval:\n"
<< "min: " << timeGapsPerReport.getWindowMin() << " usecs, "
<< "max: " << timeGapsPerReport.getWindowMax() << " usecs, "
<< "avg: " << timeGapsPerReport.getWindowAverage() << " usecs, "
<< "stdev: " << stDevReportInterval.getStDev() << " usecs\n"
<< "\n";
<< "Overall:\n"
<< "min: " << timeGaps.getMin() << " usecs, "
<< "max: " << timeGaps.getMax() << " usecs, "
<< "avg: " << timeGaps.getAverage() << " usecs, "
<< "stdev: " << stDev.getStDev() << " usecs\n"
<< "Last 30s:\n"
<< "min: " << timeGaps.getWindowMin() << " usecs, "
<< "max: " << timeGaps.getWindowMax() << " usecs, "
<< "avg: " << timeGaps.getWindowAverage() << " usecs, "
<< "stdev: " << stDev30s.getStDev() << " usecs\n"
<< "Last report interval:\n"
<< "min: " << timeGapsPerReport.getWindowMin() << " usecs, "
<< "max: " << timeGapsPerReport.getWindowMax() << " usecs, "
<< "avg: " << timeGapsPerReport.getWindowAverage() << " usecs, "
<< "stdev: " << stDevReportInterval.getStDev() << " usecs\n"
<< "\n";
stDevReportInterval.reset();
if (stDev30s.getSamples() > SAMPLES_FOR_30_SECONDS) {
@ -245,20 +269,24 @@ void runReceive(const char* addressOption, int port, int gap, int size, int repo
PacketStreamStats packetStatsLastReportInterval = seqStats.getStatsForLastHistoryInterval();
std::cout << "RECEIVE Packet Stats\n"
<< "Overall:\n"
<< "lost: " << seqStats.getLost() << ", "
<< "lost %: " << seqStats.getStats().getLostRate() * 100.0f << "%\n"
<< "Last 30s:\n"
<< "lost: " << packetStatsLast30s._lost << ", "
<< "lost %: " << packetStatsLast30s.getLostRate() * 100.0f << "%\n"
<< "Last report interval:\n"
<< "lost: " << packetStatsLastReportInterval._lost << ", "
<< "lost %: " << packetStatsLastReportInterval.getLostRate() * 100.0f << "%\n"
<< "\n\n";
<< "Overall:\n"
<< "lost: " << seqStats.getLost() << ", "
<< "lost %: " << seqStats.getStats().getLostRate() * 100.0f << "%\n"
<< "Last 30s:\n"
<< "lost: " << packetStatsLast30s._lost << ", "
<< "lost %: " << packetStatsLast30s.getLostRate() * 100.0f << "%\n"
<< "Last report interval:\n"
<< "lost: " << packetStatsLastReportInterval._lost << ", "
<< "lost %: " << packetStatsLastReportInterval.getLostRate() * 100.0f << "%\n"
<< "\n\n";
lastReport = now;
}
}
}
}
delete[] inputBuffer;
#ifdef _WIN32
WSACleanup();
#endif
}