0

I want to benchmark a TCP Connection to an ECU (Transferrate)

For that, i want so send some Data. The amout of data can be set in my GUI from x KB to x GB.

The transferSize is calculated from the values set in the GUI. Buffer Size is 1024:

#define BENCHMARK_TRANSFER_BUFFER_SIZE 1024


void logic::TransferAndLatencyEthernetBenchmark::transferDataTcp(TCPClient& tcpclient)
{
uint32_t toSend = transferSize;
uint32_t currentSendSize = 0;

while (toSend > 0)
{
    if (toSend / BENCHMARK_TRANSFER_BUFFER_SIZE >= 1)
    {
        currentSendSize = BENCHMARK_TRANSFER_BUFFER_SIZE;
    }
    else if ((toSend / BENCHMARK_TRANSFER_BUFFER_SIZE) == 0)
    {
        currentSendSize = toSend % BENCHMARK_TRANSFER_BUFFER_SIZE;
    }
    toSend -= currentSendSize;

    tcpclient.sendData(transferSendBuffer, currentSendSize);
 }
}

So iam calling the tcpclient.sendData(transferSendBuffer, currentSendSize) function als long as toSend > 0:

...
QDataStream out;
....

void TCPClient::sendData(uint8_t* buffer, uint32_t size)
{

qDebug() << "current send size: " << size << " - Transfer send buffer: " << (const char*)buffer;
for (int i=0; i<size; i++)
 {
    buffer[i] = 'a';
 }
 out.setDevice(socket_aurix);
 out.writeBytes((const char*)buffer, size);

}

Iam pretty new on Qt (C++ in generel) and iam sure iam doing it wrong ;) It actually works but crashed when i try so send bigger amouts of data.

What would be a better way to initialize the fill the array instead of the for loop?

What what be a better approach in generel to use QDataStream?

Many thanks.

  • 2
    Replace the loop with: memset(&buffer[0],'a',size); – gj13 Jan 26 '18 at 09:20
  • 2
    You might use `QByteArray` and initialize it in constructor like `QByteArray ba(100, 'a');`, for example. – vahancho Jan 26 '18 at 09:20
  • And also if buffer is always going to be an array of 'a', defined it once (preferably as a constant in your constructor) with size BENCHMARK_TRANSFER_BUFFER_SIZE and re-use. – falopsy Jan 26 '18 at 09:25
  • Thanks for your answers...I tryed if with QByteArray, in Constuctor: QByteArray array_out(1024, 'a'). Then in function: out.writeBytes(array, size). Then: array_out.resize(size). Seems like the initialisation with 'A' doesnt work, when i qDebug() << array_out: "1\x00""4\x00)\x01\xFF\x00\x90\x01\x00\x00*\x01\xFF\x00\x90\x01""4\x00+\x01\xFF\x00\x90\x01""4\x00,\x01\xFF\x00\x90\x01""4\x00-\x01\xFF\x00\x90\x01""4\x00.\x01\xFF\x00<\x03\x00?\xAC\.......any ideas? – Chris Nelson Jan 26 '18 at 09:41
  • Is there no way to set the whole qbytearray to a certain value like 'a'? – Chris Nelson Jan 26 '18 at 09:47
  • ive got one more question: is the TCP Stream via QDataStream Asyncron? I soon as i send more than 5MB, my GUI seems to be blocked. Do I need a own Thread? – Chris Nelson Jan 26 '18 at 09:53
  • @ChrisNelson As for the original question `qDebug() << (const char*)buffer;` - did you make sure that `buffer` is null-terminated i.e. has zero byte at the end? Comment out the qDebug() line and check if the crash is still there. – absolute.madness Jan 26 '18 at 09:55
  • Do you really need QDataStream? it's meant for serializing data, which doesn't look to be your case. About the async behavior, QTcpSocket write is async, it will copy the data you ->write() to an internal buffer an send later, but when you have lots of data chances are the process will have little time to refresh it's UI, so better do this in a thread. – Daniel Nicoletti Jan 27 '18 at 16:12
  • Thank you for all the help. I will do it with an thread for the writing, so my GUI wont get locked. – Chris Nelson Jan 29 '18 at 08:28

0 Answers0