我正在使用TCP套接字测试两个应用程序。第一个应用程序使用新的RIO套接字调用,第二个应用程序使用经典的Winsock套接字调用。两个应用程序都会向服务器发送一个字符串。我让他们都使用非阻塞套接字并禁用Nagle算法。当我测量执行发送所需的时间时,与我的预期相反,经典套接字更快。这是什么原因?
这是我的代码:
/*
Classic socket
*/
SOCKET ConnectSocket = INVALID_SOCKET;
// ... socket initialization
// Make socket non-blocking and disable Nagle's algorithm
unsigned long mode = 1;
ioctlsocket(ConnectSocket, FIONBIO, &mode);
int isOn = 1;
setsockopt(ConnectSocket, IPPROTO_TCP, TCP_NODELAY, (char *) &isOn, sizeof(int) );
// Measure exec time
t1 = __rdtscp(&dum);
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
t2 = __rdtscp(&dum);
cout << (t2 - t1) / 3000.0f << " 10^-6 sec" << endl;
/*
RIO socket
*/
SOCKET s = WSASocket(
AF_INET,
SOCK_STREAM,
IPPROTO_TCP,
NULL,
0,
WSA_FLAG_REGISTERED_IO);
// ... socket initialization
// Make socket non-blocking and disable Nagel's algorithm
unsigned long mode = 1;
ioctlsocket(s, FIONBIO, &mode);
int isOn = 1;
setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *)&isOn, sizeof(int));
// RIO socket is event-based
hEvent = WSACreateEvent();
RIO_NOTIFICATION_COMPLETION type;
type.Type = RIO_EVENT_COMPLETION;
type.Event.EventHandle = hEvent;
type.Event.NotifyReset = TRUE;
RIO_CQ complQueue = rioFuncTable.RIOCreateCompletionQueue(1000, &type);
RIO_RQ requestQueue = rioFuncTable.RIOCreateRequestQueue(
s,
10,
1,
10,
1,
complQueue,
complQueue,
NULL);
// Init rio buffers
char *pSndBuffer = new char[sndBufferSize + 1]();
std::copy(message, message + sndBufferSize, pSndBuffer); // put message to a server here
RIO_BUFFERID sndBufID = rioFuncTable.RIORegisterBuffer(pSndBuffer, sndBufferSize);
RIO_BUF sndBuffer;
sndBuffer.BufferId = sndBufID;
sndBuffer.Offset = 0;
sndBuffer.Length = sndBufferSize;
// Measure exec time
t1 = __rdtscp(&dum);
rioFuncTable.RIOSend(requestQueue, &sndBuffer, 1, 0, NULL);
t2 = __rdtscp(&dum);
cout << (t2 - t1) / 3000.0f << " 10^-6 sec" << endl;
答案 0 :(得分:0)
RioSend()发送真实内容。 send()将缓冲区memcopies到socket的tx缓冲区。