Guest User

Untitled

a guest
Nov 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include <benchmark/benchmark.h>
  2. #include <string>
  3. #include <sstream>
  4. #include <windows.h>
  5. #include <iostream>
  6.  
  7. constexpr static unsigned io_chunk_size = 1024 * 1024 * 8; // 8 MiB
  8.  
  9. static void BM_write_file_nochunks(benchmark::State& state) {
  10. state.PauseTiming();
  11.  
  12. uint64_t bytes_to_write = state.range(0);
  13. DWORD total_bytes_written = 0;
  14. char* buffer = new char[bytes_to_write];
  15.  
  16. std::stringstream file_name;
  17. file_name << "test_nochunks_" << bytes_to_write / 1024 / 1024 << "MB.bin";
  18.  
  19. HANDLE hFile = CreateFile(file_name.str().c_str(),// name of the write
  20. GENERIC_WRITE, // open for writing
  21. 0, // do not share
  22. NULL, // default security
  23. CREATE_ALWAYS, // create new file only
  24. FILE_ATTRIBUTE_NORMAL, // normal file
  25. NULL); // no attr. template
  26.  
  27. state.ResumeTiming();
  28. BOOL error_flag = WriteFile(hFile, buffer, bytes_to_write, &total_bytes_written, NULL);
  29. if (!error_flag)
  30. std::cerr << "ERROR: " << GetLastError() << std::endl;
  31. state.PauseTiming();
  32.  
  33. state.SetBytesProcessed(total_bytes_written);
  34. CloseHandle(hFile);
  35. }
  36.  
  37. static void BM_write_file_chunks(benchmark::State& state) {
  38. state.PauseTiming();
  39.  
  40. uint64_t total_bytes_to_write = state.range(0);
  41. DWORD total_bytes_written = 0;
  42. char* buffer = new char[total_bytes_to_write];
  43. uint64_t offset = 0;
  44.  
  45. std::stringstream file_name;
  46. file_name << "test_chunks_" << total_bytes_to_write / 1024 / 1024 << "MB.bin";
  47.  
  48. HANDLE hFile = CreateFile(file_name.str().c_str(),// name of the write
  49. GENERIC_WRITE, // open for writing
  50. 0, // do not share
  51. NULL, // default security
  52. CREATE_ALWAYS, // create new file only
  53. FILE_ATTRIBUTE_NORMAL, // normal file
  54. NULL); // no attr. template
  55.  
  56. state.ResumeTiming();
  57. while (offset < total_bytes_to_write)
  58. {
  59. DWORD bytes_written = 0;
  60. const auto bytes_to_write = min(io_chunk_size, total_bytes_to_write - total_bytes_written);
  61.  
  62. BOOL error_flag = WriteFile(hFile, buffer, bytes_to_write, &bytes_written, NULL);
  63. if (!error_flag)
  64. std::cerr << "ERROR: " << GetLastError() << std::endl;
  65.  
  66. buffer += bytes_written;
  67. offset += bytes_written;
  68. total_bytes_written += bytes_written;
  69. }
  70. state.PauseTiming();
  71.  
  72. state.SetBytesProcessed(total_bytes_written);
  73. CloseHandle(hFile);
  74. }
  75.  
  76. BENCHMARK(BM_write_file_nochunks)->Range(1 << 20, 1 << 29);
  77. BENCHMARK(BM_write_file_chunks)->Range(1 << 20, 1 << 29);
  78.  
  79. BENCHMARK_MAIN();
Add Comment
Please, Sign In to add comment