Index: tools/win/CreateTempFilesPerfEvaluation/CreateTempFilesPerfEval.cc |
diff --git a/tools/win/CreateTempFilesPerfEvaluation/CreateTempFilesPerfEval.cc b/tools/win/CreateTempFilesPerfEvaluation/CreateTempFilesPerfEval.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6b8b0717aa15422dd1a9876facffb50da8aa23a4 |
--- /dev/null |
+++ b/tools/win/CreateTempFilesPerfEvaluation/CreateTempFilesPerfEval.cc |
@@ -0,0 +1,194 @@ |
+// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <windows.h> |
+ |
+#include <shlwapi.h> |
+#include <stdio.h> |
+#include <tchar.h> |
+ |
+#include <algorithm> |
+#include <iostream> |
+#include <sstream> |
+#include <string> |
+ |
+#pragma warning(disable : 4996) |
+ |
+// Create |count| number of temp files at |kTempDirForGuid| usingGUID based |
+// method. The time cost in millisecond of creating every 500 temp files or all |
+// the files if |count| < 500 is printed to the console. |
+bool CreateFilesUsingGuid(UINT count); |
+ |
+// Create |count| number of temp files at |kTempDirForGetTempFileName| using |
+// GetTempFileName() API. The time cost in millisecond of creating every 500 |
+// temp files or all the files if |count| < 500 is printed to the console. |
+bool CreateFilesUsingGetTempFileName(UINT count); |
+ |
+// This method converts GUID to a string. |
+char* ConvertGuidToString(const GUID* id, char* out); |
+ |
+// Check if the given directory exists and is empty. If it doesn't exist, try to |
+// create it. |
+bool IsDirectoryReadyExistedAndEmpty(char* dir_name); |
+ |
+// A temporary directory where the new temp files created by GetTempFileName() |
+// are written. |
+CHAR kTempDirForGetTempFileName[] = "C:\\src\\TempDirGetTempFileName\\"; |
+ |
+// A temporary directory where the new temp files created by Guid-based method |
+// are written. |
+CHAR kTempDirForGuid[] = "C:\\src\\TempDirGuid\\"; |
stanisc
2017/04/14 18:45:59
If you want to make the tool reusable it shouldn't
chengx
2017/04/14 22:27:22
Thanks for the reminder. I have switched to the TE
|
+ |
+// Maximum number of temp files allowed to create. This is limited by the |
+// implementation of GetTempFileName(). |
+// "This limits GetTempFileName to a maximum of 65,535 unique file names if the |
+// lpPathName and lpPrefixString parameters remain the same." |
+// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85).aspx |
+UINT kMaxFileCreate = 65535; |
+ |
+int main() { |
+ UINT file_create_count; |
+ std::string user_input; |
+ |
+ while (true) { |
+ std::cout << "\nPlease enter # of files to create (maximum " |
+ << kMaxFileCreate << "), or \"quit\" to end the program : "; |
+ std::getline(std::cin, user_input); |
+ |
+ std::transform(user_input.begin(), user_input.end(), user_input.begin(), |
+ ::tolower); |
+ if (user_input == "quit") |
+ break; |
+ |
+ std::cout << std::endl; |
+ std::stringstream ss(user_input); |
+ |
+ if (ss >> file_create_count && file_create_count <= kMaxFileCreate) { |
+ std::cout << "\nPlease select method to create temp file names,\n" |
+ << "\"t\" for GetTempFileName \n" |
+ << "\"g\" for GUID-based \n" |
+ << "\"b\" for both \n" |
+ << "or \"quit\" to end the program : "; |
+ std::getline(std::cin, user_input); |
+ |
+ std::transform(user_input.begin(), user_input.end(), user_input.begin(), |
+ ::tolower); |
+ if (user_input == "quit") |
+ break; |
+ |
+ if (user_input == "t" || user_input == "b") { |
+ std::cout << "\nGetTempFileName :" << std::endl; |
+ if (CreateFilesUsingGetTempFileName(file_create_count)) { |
+ std::cout << "File creation succeeds at " |
+ << kTempDirForGetTempFileName |
+ << ", remember to clean all of them!" << std::endl; |
+ } |
+ } |
+ |
+ if (user_input == "g" || user_input == "b") { |
+ std::cout << "\nGUID-based :" << std::endl; |
+ if (CreateFilesUsingGuid(file_create_count)) { |
+ std::cout << "File creation succeeds at " << kTempDirForGuid |
+ << ", remember to clean all of them!" << std::endl; |
+ } |
+ } |
+ } else { |
+ std::cout << "Input number is invalid, please enter # of files to create " |
+ "(maximum " |
+ << kMaxFileCreate << "), or \"quit\" to end the program : "; |
+ } |
+ std::cout << std::endl; |
+ } |
+ return 0; |
+} |
+ |
+bool CreateFilesUsingGuid(UINT count) { |
+ if (!IsDirectoryReadyExistedAndEmpty(kTempDirForGuid)) |
+ return false; |
+ |
+ LARGE_INTEGER starting_time, ending_time, elapsed_ms; |
+ ::QueryPerformanceCounter(&starting_time); |
+ LARGE_INTEGER frequency; |
+ ::QueryPerformanceFrequency(&frequency); |
+ |
+ for (UINT i = 1; i <= count; ++i) { |
+ GUID guid; |
+ HRESULT hCreateGuid = ::CoCreateGuid(&guid); |
+ char buffer[37]; |
+ ConvertGuidToString(&guid, buffer); |
+ std::string temp_name = |
+ std::string(kTempDirForGuid).append(buffer).append(".tmp"); |
+ |
+ HANDLE file_handle = |
+ ::CreateFileA(temp_name.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, |
+ CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
+ ::CloseHandle(file_handle); |
+ |
+ if (i % 500 == 0 || i == count) { |
+ ::QueryPerformanceCounter(&ending_time); |
+ // Convert the elapsed number of ticks to milliseconds. |
+ elapsed_ms.QuadPart = (ending_time.QuadPart - starting_time.QuadPart) * |
+ 1000 / frequency.QuadPart; |
+ |
+ starting_time = ending_time; |
+ std::cout << i << " / " << count << " --- " << elapsed_ms.QuadPart |
+ << " ms" << std::endl; |
+ } |
+ } |
+ return true; |
+} |
+ |
+bool CreateFilesUsingGetTempFileName(UINT count) { |
+ if (!IsDirectoryReadyExistedAndEmpty(kTempDirForGetTempFileName)) |
+ return false; |
+ |
+ CHAR temp_name[MAX_PATH]; |
+ LARGE_INTEGER starting_time, ending_time, elapsed_ms; |
+ ::QueryPerformanceCounter(&starting_time); |
+ LARGE_INTEGER frequency; |
+ ::QueryPerformanceFrequency(&frequency); |
+ |
+ for (UINT i = 1; i <= count; ++i) { |
+ ::GetTempFileNameA(kTempDirForGetTempFileName, "", 0, temp_name); |
+ if (i % 500 == 0 || i == count) { |
+ ::QueryPerformanceCounter(&ending_time); |
+ // Convert the elapsed number of ticks to milliseconds. |
+ elapsed_ms.QuadPart = (ending_time.QuadPart - starting_time.QuadPart) * |
+ 1000 / frequency.QuadPart; |
+ |
+ starting_time = ending_time; |
+ std::cout << i << " / " << count << " --- " << elapsed_ms.QuadPart |
+ << " ms" << std::endl; |
+ } |
+ } |
+ return true; |
+} |
+ |
+char* ConvertGuidToString(const GUID* id, char* out) { |
+ int i; |
+ char* ret = out; |
+ out += sprintf(out, "%.8lX-%.4hX-%.4hX-", id->Data1, id->Data2, id->Data3); |
+ for (i = 0; i < sizeof(id->Data4); ++i) { |
+ out += sprintf(out, "%.2hhX", id->Data4[i]); |
stanisc
2017/04/14 18:46:00
Could use cast Data4 to to avoid printing byte by
stanisc
2017/04/14 18:47:58
What I was really trying to say is: Could this try
chengx
2017/04/14 22:27:22
Em, I am not sure but I will try. I think the curr
|
+ if (i == 1) |
+ *(out++) = '-'; |
+ } |
+ return ret; |
+} |
+ |
+bool IsDirectoryReadyExistedAndEmpty(char* dir_name) { |
+ if (::PathFileExistsA(dir_name)) { |
+ if (!::PathIsDirectoryEmptyA(dir_name)) { |
+ std::cout << dir_name |
+ << " directory is not empty, please remove all its content."; |
+ return false; |
+ } |
+ return true; |
+ } else if (::CreateDirectoryA(dir_name, NULL) == 0) { |
+ std::cout << dir_name << "directory creation fails."; |
+ return false; |
+ } else { |
+ return true; |
+ } |
+} |