1 // Copyright (c) 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <windows.h>
6
7 #include <stdio.h>
8 #include <tchar.h>
9
10 #include <algorithm>
11 #include <iostream>
12 #include <sstream>
13 #include <string>
14
15 #include "base/win/shlwapi.h"
16
17 #pragma warning(disable : 4996)
18
19 // Create |count| number of temp files at |folder_path| using GUID based method.
20 // The time cost in millisecond of creating every 500 temp files or all the
21 // files if |count| < 500 is printed to the console.
22 bool CreateFilesUsingGuid(UINT count, const char* folder_path);
23
24 // Create |count| number of temp files at |folder_path| using GetTempFileName()
25 // API. The time cost in millisecond of creating every 500 temp files or all the
26 // files if |count| < 500 is printed to the console.
27 bool CreateFilesUsingGetTempFileName(UINT count, const char* folder_path);
28
29 // This method converts GUID to a string.
30 char* ConvertGuidToString(const GUID* id, char* out);
31
32 // If |folder_path| doesn't exist, creat it, otherwise check if it is empty.
33 bool CreateOrValidateTempDirectory(const char* folder_path);
34
35 // Deletes all the content in |folder_path|.
36 void DeleteDirectoryContent(const char* folder_path);
37
38 // Deletes |folder_path| including its contents and the raw directory.
39 bool DeleteDirectory(const char* folder_path);
40
41 // Deletes |folder_path| including its contents and the raw directory, and print
42 // the delete status to the console.
43 void DeleteDirectoryAndPrintMsg(const char* folder_path);
44
45 // Prints the elapsed time at current step for the latest cycle.
46 void FormatPrintElapsedTime(UINT cur_step,
47 UINT total_step,
48 const LARGE_INTEGER& elapsed_ms);
49
50 // Maximum number of temp files allowed to create. This is limited by the
51 // implementation of GetTempFileName().
52 // "This limits GetTempFileName to a maximum of 65,535 unique file names if the
53 // lpPathName and lpPrefixString parameters remain the same."
54 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85).aspx
55 UINT kMaxFileCreate = 65535;
56
57 // Query the time cost each time when this amount of temp files are created.
58 UINT kFileCountPerMetric = 500;
59
main()60 int main() {
61 // Gets the temp path env string.
62 DWORD temp_path_ret = 0;
63 CHAR temp_folder_path[MAX_PATH];
64 temp_path_ret = ::GetTempPathA(MAX_PATH, temp_folder_path);
65 if (temp_path_ret > MAX_PATH || temp_path_ret == 0) {
66 std::cout << "GetTempPath failed" << std::endl;
67 return 0;
68 }
69
70 // A temporary directory where the new temp files created by GetTempFileName()
71 // are written.
72 std::string temp_dir_gettempfilename(
73 std::string(temp_folder_path).append("TempDirGetTempFileName\\"));
74
75 // A temporary directory where the new temp files created by Guid-based method
76 // are written.
77 std::string temp_dir_guid(
78 std::string(temp_folder_path).append("TempDirGuid\\"));
79
80 UINT file_create_count;
81 std::string user_input;
82
83 while (true) {
84 std::cout << "\nPlease enter # of files to create (maximum "
85 << kMaxFileCreate << "), or \"quit\" to end the program : ";
86 std::getline(std::cin, user_input);
87
88 std::transform(user_input.begin(), user_input.end(), user_input.begin(),
89 ::tolower);
90 if (user_input == "quit")
91 break;
92
93 std::cout << std::endl;
94 std::stringstream ss(user_input);
95
96 if (ss >> file_create_count && file_create_count <= kMaxFileCreate) {
97 std::cout << "\nPlease select method to create temp file names,\n"
98 << "\"t\" for GetTempFileName \n"
99 << "\"g\" for GUID-based \n"
100 << "\"b\" for both \n"
101 << "or \"quit\" to end the program : ";
102 std::getline(std::cin, user_input);
103
104 std::transform(user_input.begin(), user_input.end(), user_input.begin(),
105 ::tolower);
106 if (user_input == "quit")
107 break;
108
109 if (user_input == "t" || user_input == "b") {
110 std::cout << "\nGetTempFileName Performance:\n [start - end] / total "
111 "--- time "
112 "cost in ms"
113 << std::endl;
114 if (CreateFilesUsingGetTempFileName(file_create_count,
115 temp_dir_gettempfilename.c_str())) {
116 std::cout << "File creation succeeds at " << temp_dir_gettempfilename
117 << ", now clean all of them!" << std::endl;
118 }
119 DeleteDirectoryAndPrintMsg(temp_dir_gettempfilename.c_str());
120 }
121
122 if (user_input == "g" || user_input == "b") {
123 std::cout << "\nGUID-based Performance:\n [start - end] / total --- "
124 "time cost in ms"
125 << std::endl;
126 if (CreateFilesUsingGuid(file_create_count, temp_dir_guid.c_str())) {
127 std::cout << "File creation succeeds at " << temp_dir_guid
128 << ", now clean all of them!" << std::endl;
129 }
130 DeleteDirectoryAndPrintMsg(temp_dir_guid.c_str());
131 }
132 } else {
133 std::cout << "Input number is invalid, please enter # of files to create "
134 "(maximum "
135 << kMaxFileCreate << "), or \"quit\" to end the program : ";
136 }
137 std::cout << std::endl;
138 }
139 return 0;
140 }
141
CreateFilesUsingGuid(UINT count,const char * dir_path)142 bool CreateFilesUsingGuid(UINT count, const char* dir_path) {
143 if (!CreateOrValidateTempDirectory(dir_path))
144 return false;
145
146 LARGE_INTEGER starting_time, ending_time, elapsed_ms;
147 ::QueryPerformanceCounter(&starting_time);
148 LARGE_INTEGER frequency;
149 ::QueryPerformanceFrequency(&frequency);
150
151 for (UINT i = 1; i <= count; ++i) {
152 GUID guid;
153 ::CoCreateGuid(&guid);
154 char buffer[37];
155 ConvertGuidToString(&guid, buffer);
156 std::string temp_name = std::string(dir_path).append(buffer).append(".tmp");
157
158 HANDLE file_handle =
159 ::CreateFileA(temp_name.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
160 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
161 ::CloseHandle(file_handle);
162
163 if (i % kFileCountPerMetric == 0 || i == count) {
164 ::QueryPerformanceCounter(&ending_time);
165 // Convert the elapsed number of ticks to milliseconds.
166 elapsed_ms.QuadPart = (ending_time.QuadPart - starting_time.QuadPart) *
167 1000 / frequency.QuadPart;
168
169 FormatPrintElapsedTime(i, count, elapsed_ms);
170
171 ::QueryPerformanceCounter(&starting_time);
172 }
173 }
174 return true;
175 }
176
CreateFilesUsingGetTempFileName(UINT count,const char * dir_path)177 bool CreateFilesUsingGetTempFileName(UINT count, const char* dir_path) {
178 if (!CreateOrValidateTempDirectory(dir_path))
179 return false;
180
181 CHAR temp_name[MAX_PATH];
182 LARGE_INTEGER starting_time, ending_time, elapsed_ms;
183 ::QueryPerformanceCounter(&starting_time);
184 LARGE_INTEGER frequency;
185 ::QueryPerformanceFrequency(&frequency);
186
187 for (UINT i = 1; i <= count; ++i) {
188 ::GetTempFileNameA(dir_path, "", 0, temp_name);
189 if (i % kFileCountPerMetric == 0 || i == count) {
190 ::QueryPerformanceCounter(&ending_time);
191 // Convert the elapsed number of ticks to milliseconds.
192 elapsed_ms.QuadPart = (ending_time.QuadPart - starting_time.QuadPart) *
193 1000 / frequency.QuadPart;
194
195 FormatPrintElapsedTime(i, count, elapsed_ms);
196
197 ::QueryPerformanceCounter(&starting_time);
198 }
199 }
200 return true;
201 }
202
ConvertGuidToString(const GUID * id,char * out)203 char* ConvertGuidToString(const GUID* id, char* out) {
204 int i;
205 char* ret = out;
206 out += sprintf(out, "%.8lX-%.4hX-%.4hX-", id->Data1, id->Data2, id->Data3);
207 for (i = 0; i < sizeof(id->Data4); ++i) {
208 out += sprintf(out, "%.2hhX", id->Data4[i]);
209 if (i == 1)
210 *(out++) = '-';
211 }
212 return ret;
213 }
214
CreateOrValidateTempDirectory(const char * folder_path)215 bool CreateOrValidateTempDirectory(const char* folder_path) {
216 if (::PathFileExistsA(folder_path)) {
217 if (!::PathIsDirectoryEmptyA(folder_path)) {
218 std::cout << folder_path
219 << " directory is not empty, please remove all its content.";
220 return false;
221 }
222 return true;
223 } else if (::CreateDirectoryA(folder_path, NULL) == 0) {
224 std::cout << folder_path << "directory creation fails.";
225 return false;
226 } else {
227 return true;
228 }
229 }
230
DeleteDirectoryContent(const char * folder_path)231 void DeleteDirectoryContent(const char* folder_path) {
232 char file_found[MAX_PATH];
233 WIN32_FIND_DATAA info;
234 HANDLE hp;
235 sprintf(file_found, "%s\\*.*", folder_path);
236 hp = ::FindFirstFileA(file_found, &info);
237 do {
238 if ((strcmp(info.cFileName, ".") == 0) ||
239 (strcmp(info.cFileName, "..") == 0)) {
240 continue;
241 }
242 sprintf(file_found, "%s\\%s", folder_path, info.cFileName);
243 ::DeleteFileA(file_found);
244 } while (::FindNextFileA(hp, &info));
245 ::FindClose(hp);
246 }
247
DeleteDirectory(const char * folder_path)248 bool DeleteDirectory(const char* folder_path) {
249 DeleteDirectoryContent(folder_path);
250 return ::RemoveDirectoryA(folder_path) != 0;
251 }
252
DeleteDirectoryAndPrintMsg(const char * folder_path)253 void DeleteDirectoryAndPrintMsg(const char* folder_path) {
254 if (DeleteDirectory(folder_path)) {
255 std::cout << folder_path << " directory is deleted!" << std::endl;
256 } else {
257 std::cout << "[Attention] " << folder_path
258 << " directory's deletion fails, please take a look by yourself!"
259 << std::endl;
260 }
261 }
262
FormatPrintElapsedTime(UINT cur_step,UINT total_step,const LARGE_INTEGER & elapsed_ms)263 void FormatPrintElapsedTime(UINT cur_step,
264 UINT total_step,
265 const LARGE_INTEGER& elapsed_ms) {
266 UINT count_prev = 0;
267 if (cur_step % kFileCountPerMetric == 0)
268 count_prev = cur_step + 1 - kFileCountPerMetric;
269 else if (cur_step > kFileCountPerMetric)
270 count_prev = cur_step / kFileCountPerMetric * kFileCountPerMetric + 1;
271 printf(" [%5d - %5d] / %d --- %lld\n", count_prev, cur_step, total_step,
272 elapsed_ms.QuadPart);
273 }
274