Getting very slow speed while using QSslSocket to send data
-
Hi,
I am working on a qt project(client/server app) on Windows.
In our project we are reading blocks(partition blocks/clusters) of data which is of 4096 bytes using CreateFile and ReadFile and sending it to the server using sockets(QSslSocket). Actually when directly reading files using QFile and sending the data we are getting around 25 Mb/sec speed but while reading blocks we are getting just 10-12 Mb/sec speed. What can be done to increase the speed while reading blocks and sending it to the server. Below is my client side code that is sending the data to server,@void client::senddata()
{
HANDLE handle;
char *data = new char[4096];
DWORD pos = -1, dwRead;
BOOL ret;
long long sectorno;
LONGLONG distance;
LARGE_INTEGER phyoff;
start_time = QDateTime::currentDateTime().toTime_t();handle = CreateFile(L"\\\\.\\D:",GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0); if(handle == INVALID_HANDLE_VALUE) { qDebug() << "[client::senddata] Invalid Read sector handle: " << QString::number(GetLastError()); return; } QByteArray databuff; for(quint64 i = 0; i < blocklist.size(); i++){ sectorno = (blocklist.at(i)*8); distance = sectorno * 512; phyoff.QuadPart = distance; if(pos != phyoff.u.LowPart){ pos = SetFilePointer(handle, phyoff.u.LowPart,&phyoff.u.HighPart,FILE_BEGIN); if (pos == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR){ qDebug() << "[client::senddata] SetFilePointer Error: " << GetLastError(); } } memset(data,0,4096); ret = ReadFile(handle, data, 4096, &dwRead, 0); if(ret == FALSE){ qDebug() << "[client::senddata] Readfile Error: " << GetLastError(); } pos+=4096; bblen++;
rite(QByteArray(data, dwRead).prepend("d"));
if((bblen % 2500 == 0) && (bblen > 0)){
socket->flush();
socket->waitForBytesWritten(12000);
}
}
qDebug() << "[client::senddata] Time Taken" << (QDateTime::currentDateTime().toTime_t() - start_time);
CloseHandle(handle);
if(data)
delete data;
}// Socket write safe as it appends the length to the beginning of the string
qint64 client::rite(const QByteArray & data){
quint16 qlen = ((quint16)data.size());
QByteArray len;
bool ok;
len.append(QByteArray(((char*)&qlen)+1,1));
len.append(QByteArray((char*)&qlen,1));
qint64 w = socket->write(len + data);
qDebug() << "[client::rite] Size : " << data.size() << "Written : " << w << QByteArray(((char*)&qlen)+1,1).toInt(&ok);
return w;
}@Can anyone help me with this?
Awaiting a positive response.Thanks in advance.
-
What is the size of data you are writing to socket when reading from directly from file ? What do you mean by directly reading from file ? Do you mean to say that you read the entire content of the file and dumping to socket ? Also how are you measuring the speed in both the cases ?
-
Hi,
Thanks for the reply.
bq. What is the size of data you are writing to socket when reading from directly from file ?
The total size of one file is 324 Mb and of other is 1.65 gb and I am reading 4096 bytes of data from file and writing it to socket.
bq. What do you mean by directly reading from file ?
Actually reading directly from file means reading the file contents using QFile and read ( char * data, qint64 maxSize ) where maxSize is of 4096 bytes.
bq. Do you mean to say that you read the entire content of the file and dumping to socket ?
No. I am not reading the entire contents of file and then writing it to socket.
I am just reading 4096 bytes of data from file and then writing it to socket and reading next 4096 bytes and so on.
My main approach is to find all the blocks(clusters) of that file and then read the data(4096 bytes) of those blocks and write it to socket.bq. Also how are you measuring the speed in both the cases ?
I am calculating difference in time before the initial reading of file and writing it to socket and on completion of reading the entire file and writing it to socket.
for eg:
@time = current time;
for(int i = 0; i < blocklist.size(); i++){//Read data from block(blocklist.at(i)) using ReadFile function of Windows into data buffer which is 4096 bytes
//Write data in socket
//Call socket function flush and waitforbytes written on a particular condition.
}
qDebug() << "time taken" << time - current time;
@You can also check the exact code in my original post.
-
got it. So you are measuring the entire time. So here time required to read from the file is also measured. So it is not the issue of sockets. It is the issue of file reading. Is that correct ?
-
Hi,
Hmm may be not sure as only reading of 320 Mb file is taking 12 seconds and then after complete reading of that file when writing it to socket the time taken is further 22 seconds. So speed of reading only file is 26 Mb/sec while writing it to socket is 14 Mb/sec.
But when both are working together means first reading blocks of 4096 bytes and writing it to socket we are getting a total of 10-12 Mb/sec speed. -
I did not get it. Is your issue resolved or still do you have clarifications?
-
Hi,
My issue is not resolved.
I want to speed up more when first reading data of 4096 bytes and writing it to socket. I am getting just 10-12 Mb/s which is slow, is it possible to increase the speed by any other approach or way? Do you have any solution for the same?Note: Actually we have to send around 10-12 gb of raw data, but at present we are just testing with 2 files of 320 Mb and 1.65 gb respectively.
-
There are many variables and not enough information.
10-12 megabits per second is not slow if you are sending over a typical DSL Internet link, or using a slow processor to do the SSL encryption or decryption (either end). You consistently use a lower-case "b" to mean bytes when referring to the files sizes, so if you mean 10-12 megabytes per second then that is a saturated 100 Mbps LAN and nothing less than upgrading the LAN would change the result.
What are you transferring the data over?
Have you tried the transfer using another tool?
Have you tried the transfer without the encryption?