C ++ Windows应用程序网络IO吞吐量

时间:2015-10-01 15:18:03

标签: c++ windows networking bandwidth throughput

我正在尝试编写一个在Win2012上运行的Visual C ++ 2008应用程序,能够通过单个Gbps接口(和Gbps交换机)从NAS上的两个不同磁盘组异步读/写文件。 / p>

同时手动文件复制(从网络存储到本地硬盘,从本地硬盘到网络存储)显示,通过我们的系统,网络接口,交换机和NAS,我们能够执行1Gbps吞吐量(发送950 Mbps) ,接收950 Mbps)。

但是使用我的Visual C ++应用程序,它有两个线程(boost),一个用于读取操作,另一个用于写操作(在一个单独的文件上),我们有350Mbps用于写入和650Mbps用于读取。

当停止阅读器或写入器时,发送或接收速度最高可达950 Mbps。

我正在使用stdio fread / fwrite读取和写入2 MB数据块。

我正在测试很多东西来解决这个问题,我可以稍后解释,但目前,也许有一些明显可以解释为什么我无法获得所需的吞吐量?

以下是我的代码重现行为,线程之间没有交互,因此IO操作不应该相互阻塞。我正在使用任务管理器检查吞吐量。

#include <stdio.h>
#include <windows.h>
#include <boost/thread.hpp>

#define INPUT_CHUNK_SIZE 2700000
#define OUTPUT_CHUNK_SIZE 2700000
#define OUTPUT_FILE_DESIRED_SIZE 1000000000

boost::thread* readerProcessThread = NULL;
boost::thread* writerProcessThread = NULL;
bool readerEnded = false;
bool writerEnded = false;
bool stop = false;
const char* inputFileName = "\\\\NASIP\\input\\inputfile.bin";
const char* outputFileName = "\\\\NASIP\\output\\outputfile.bin";

void readerThread()
{
    FILE *myFile = fopen(inputFileName, "rb");
    char *inputBuffer = new char[INPUT_CHUNK_SIZE];

    if(myFile)
    {
        while(!stop && !feof(myFile))
        {
            size_t readBytes = fread(inputBuffer, 1, INPUT_CHUNK_SIZE, myFile);
        }

        fclose(myFile);
    }
    delete[] inputBuffer;

    readerEnded = true;
    stop = (writerEnded || !writerProcessThread);
}

void writerThread()
{
    char *outputBuffer = new char[OUTPUT_CHUNK_SIZE];
    memset(outputBuffer, 0, OUTPUT_CHUNK_SIZE);
    FILE *myFile = fopen(outputFileName, "wb");
    unsigned long long writtenBytesTotal = 0;
    size_t writtenBytes = OUTPUT_CHUNK_SIZE;

    if(myFile)
    {
        while(!stop && writtenBytesTotal < OUTPUT_FILE_DESIRED_SIZE && writtenBytes == OUTPUT_CHUNK_SIZE)
        {
            size_t writtenBytes = fwrite(outputBuffer, 1, OUTPUT_CHUNK_SIZE, myFile);
            writtenBytesTotal += (unsigned long long)writtenBytes;
        }
        fclose(myFile);
    }

    writerEnded = true;
    stop = (readerEnded || !readerProcessThread);
}

int main(int argc, char * argv[])
{
    readerProcessThread = new boost::thread(&readerThread);
    writerProcessThread = new boost::thread(&writerThread);
    if(readerProcessThread && writerProcessThread)
    {
        while(!stop)
        {
            Sleep(1000);
        }
    }
    stop = true;
    if (readerProcessThread)
    {
        readerProcessThread->join();
        delete readerProcessThread;
        readerProcessThread = NULL;
    }

    if (writerProcessThread)
    {
        writerProcessThread->join();
        delete writerProcessThread;
        writerProcessThread = NULL;
    }
    return 0;
}

0 个答案:

没有答案