Skip to content

Instantly share code, notes, and snippets.

@VelocityRa
Created November 19, 2017 15:18
Show Gist options
  • Save VelocityRa/816d0322f048648e85d48cc6d07869ef to your computer and use it in GitHub Desktop.
Save VelocityRa/816d0322f048648e85d48cc6d07869ef to your computer and use it in GitHub Desktop.
Windows WriteFile() benchmark on writing files in chunks
#include <benchmark/benchmark.h>
#include <string>
#include <sstream>
#include <windows.h>
#include <iostream>
constexpr static unsigned io_chunk_size = 1024 * 1024 * 8; // 8 MiB
static void BM_write_file_nochunks(benchmark::State& state) {
state.PauseTiming();
uint64_t bytes_to_write = state.range(0);
DWORD total_bytes_written = 0;
char* buffer = new char[bytes_to_write];
std::stringstream file_name;
file_name << "test_nochunks_" << bytes_to_write / 1024 / 1024 << "MB.bin";
HANDLE hFile = CreateFile(file_name.str().c_str(),// name of the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, // create new file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
state.ResumeTiming();
BOOL error_flag = WriteFile(hFile, buffer, bytes_to_write, &total_bytes_written, NULL);
if (!error_flag)
std::cerr << "ERROR: " << GetLastError() << std::endl;
state.PauseTiming();
state.SetBytesProcessed(total_bytes_written);
CloseHandle(hFile);
}
static void BM_write_file_chunks(benchmark::State& state) {
state.PauseTiming();
uint64_t total_bytes_to_write = state.range(0);
DWORD total_bytes_written = 0;
char* buffer = new char[total_bytes_to_write];
uint64_t offset = 0;
std::stringstream file_name;
file_name << "test_chunks_" << total_bytes_to_write / 1024 / 1024 << "MB.bin";
HANDLE hFile = CreateFile(file_name.str().c_str(),// name of the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, // create new file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
state.ResumeTiming();
while (offset < total_bytes_to_write)
{
DWORD bytes_written = 0;
const auto bytes_to_write = min(io_chunk_size, total_bytes_to_write - total_bytes_written);
BOOL error_flag = WriteFile(hFile, buffer, bytes_to_write, &bytes_written, NULL);
if (!error_flag)
std::cerr << "ERROR: " << GetLastError() << std::endl;
buffer += bytes_written;
offset += bytes_written;
total_bytes_written += bytes_written;
}
state.PauseTiming();
state.SetBytesProcessed(total_bytes_written);
CloseHandle(hFile);
}
BENCHMARK(BM_write_file_nochunks)->Range(1 << 20, 1 << 29);
BENCHMARK(BM_write_file_chunks)->Range(1 << 20, 1 << 29);
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment