Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: tools/win/CreateTempFilesPerfEvaluation/CreateTempFilesPerfEval.cc

Issue 2810333008: A tool to evaluate methods in creating temp files (Closed)
Patch Set: Use TEMP directory, delete all created temp folder/files automatically Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 <shlwapi.h>
8 #include <stdio.h>
9 #include <tchar.h>
10
11 #include <algorithm>
12 #include <iostream>
13 #include <sstream>
14 #include <string>
15
16 #pragma warning(disable : 4996)
17
18 // Create |count| number of temp files at |folder_path| usingGUID based method.
19 // The time cost in millisecond of creating every 500 temp files or all the
20 // files if |count| < 500 is printed to the console.
21 bool CreateFilesUsingGuid(UINT count, const char* folder_path);
22
23 // Create |count| number of temp files at |folder_path| using GetTempFileName()
24 // API. The time cost in millisecond of creating every 500 temp files or all the
25 // files if |count| < 500 is printed to the console.
26 bool CreateFilesUsingGetTempFileName(UINT count, const char* folder_path);
27
28 // This method converts GUID to a string.
29 char* ConvertGuidToString(const GUID* id, char* out);
30
31 // Check if the given directory exists and is empty. If it doesn't exist, try to
32 // create it.
33 bool IsDirectoryReadyExistedAndEmpty(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 // Maximum number of temp files allowed to create. This is limited by the
46 // implementation of GetTempFileName().
47 // "This limits GetTempFileName to a maximum of 65,535 unique file names if the
48 // lpPathName and lpPrefixString parameters remain the same."
49 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85).as px
50 UINT kMaxFileCreate = 65535;
51
52 int main() {
53 // Gets the temp path env string.
54 DWORD temp_path_ret = 0;
55 CHAR temp_folder_path[MAX_PATH];
56 temp_path_ret = ::GetTempPathA(MAX_PATH, temp_folder_path);
57 if (temp_path_ret > MAX_PATH || temp_path_ret == 0) {
58 std::cout << "GetTempPath failed" << std::endl;
59 return 0;
60 }
61
62 // A temporary directory where the new temp files created by GetTempFileName()
63 // are written.
64 std::string temp_dir_gettempfilename(
65 std::string(temp_folder_path).append("TempDirGetTempFileName\\"));
66
67 // A temporary directory where the new temp files created by Guid-based method
68 // are written.
69 std::string temp_dir_guid(
70 std::string(temp_folder_path).append("TempDirGuid\\"));
71
72 UINT file_create_count;
73 std::string user_input;
74
75 while (true) {
76 std::cout << "\nPlease enter # of files to create (maximum "
77 << kMaxFileCreate << "), or \"quit\" to end the program : ";
78 std::getline(std::cin, user_input);
79
80 std::transform(user_input.begin(), user_input.end(), user_input.begin(),
81 ::tolower);
82 if (user_input == "quit")
83 break;
84
85 std::cout << std::endl;
86 std::stringstream ss(user_input);
87
88 if (ss >> file_create_count && file_create_count <= kMaxFileCreate) {
89 std::cout << "\nPlease select method to create temp file names,\n"
90 << "\"t\" for GetTempFileName \n"
91 << "\"g\" for GUID-based \n"
92 << "\"b\" for both \n"
93 << "or \"quit\" to end the program : ";
94 std::getline(std::cin, user_input);
95
96 std::transform(user_input.begin(), user_input.end(), user_input.begin(),
97 ::tolower);
98 if (user_input == "quit")
99 break;
100
101 if (user_input == "t" || user_input == "b") {
102 std::cout << "\nGetTempFileName :" << std::endl;
103 if (CreateFilesUsingGetTempFileName(file_create_count,
104 temp_dir_gettempfilename.c_str())) {
105 std::cout << "File creation succeeds at " << temp_dir_gettempfilename
106 << ", now clean all of them!" << std::endl;
107 }
108 DeleteDirectoryAndPrintMsg(temp_dir_gettempfilename.c_str());
109 }
110
111 if (user_input == "g" || user_input == "b") {
112 std::cout << "\nGUID-based :" << std::endl;
113 if (CreateFilesUsingGuid(file_create_count, temp_dir_guid.c_str())) {
114 std::cout << "File creation succeeds at " << temp_dir_guid
115 << ", now clean all of them!" << std::endl;
116 }
117 DeleteDirectoryAndPrintMsg(temp_dir_guid.c_str());
118 }
119 } else {
120 std::cout << "Input number is invalid, please enter # of files to create "
121 "(maximum "
122 << kMaxFileCreate << "), or \"quit\" to end the program : ";
123 }
124 std::cout << std::endl;
125 }
126 return 0;
127 }
128
129 bool CreateFilesUsingGuid(UINT count, const char* dir_path) {
130 if (!IsDirectoryReadyExistedAndEmpty(dir_path))
131 return false;
132
133 LARGE_INTEGER starting_time, ending_time, elapsed_ms;
134 ::QueryPerformanceCounter(&starting_time);
135 LARGE_INTEGER frequency;
136 ::QueryPerformanceFrequency(&frequency);
137
138 for (UINT i = 1; i <= count; ++i) {
139 GUID guid;
140 HRESULT hCreateGuid = ::CoCreateGuid(&guid);
141 char buffer[37];
142 ConvertGuidToString(&guid, buffer);
143 std::string temp_name = std::string(dir_path).append(buffer).append(".tmp");
144
145 HANDLE file_handle =
146 ::CreateFileA(temp_name.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
147 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
148 ::CloseHandle(file_handle);
149
150 if (i % 500 == 0 || i == count) {
151 ::QueryPerformanceCounter(&ending_time);
152 // Convert the elapsed number of ticks to milliseconds.
153 elapsed_ms.QuadPart = (ending_time.QuadPart - starting_time.QuadPart) *
154 1000 / frequency.QuadPart;
155
156 starting_time = ending_time;
157 std::cout << i << " / " << count << " --- " << elapsed_ms.QuadPart
158 << " ms" << std::endl;
159 }
160 }
161 return true;
162 }
163
164 bool CreateFilesUsingGetTempFileName(UINT count, const char* dir_path) {
165 if (!IsDirectoryReadyExistedAndEmpty(dir_path))
166 return false;
167
168 CHAR temp_name[MAX_PATH];
169 LARGE_INTEGER starting_time, ending_time, elapsed_ms;
170 ::QueryPerformanceCounter(&starting_time);
171 LARGE_INTEGER frequency;
172 ::QueryPerformanceFrequency(&frequency);
173
174 for (UINT i = 1; i <= count; ++i) {
175 ::GetTempFileNameA(dir_path, "", 0, temp_name);
176 if (i % 500 == 0 || i == count) {
177 ::QueryPerformanceCounter(&ending_time);
178 // Convert the elapsed number of ticks to milliseconds.
179 elapsed_ms.QuadPart = (ending_time.QuadPart - starting_time.QuadPart) *
180 1000 / frequency.QuadPart;
181
182 starting_time = ending_time;
183 std::cout << i << " / " << count << " --- " << elapsed_ms.QuadPart
stanisc 2017/04/14 23:30:54 Printing out to the console takes some non-trivial
chengx 2017/04/17 19:00:57 You're right! Done.
stanisc 2017/04/17 22:36:09 The same issue still exists in CreateFilesUsingGui
chengx 2017/04/17 23:12:16 Ah, I forgot. Now it's fixed.
184 << " ms" << std::endl;
185 }
186 }
187 return true;
188 }
189
190 char* ConvertGuidToString(const GUID* id, char* out) {
191 int i;
192 char* ret = out;
193 out += sprintf(out, "%.8lX-%.4hX-%.4hX-", id->Data1, id->Data2, id->Data3);
194 for (i = 0; i < sizeof(id->Data4); ++i) {
195 out += sprintf(out, "%.2hhX", id->Data4[i]);
196 if (i == 1)
197 *(out++) = '-';
198 }
199 return ret;
200 }
201
202 bool IsDirectoryReadyExistedAndEmpty(const char* folder_path) {
stanisc 2017/04/14 23:30:54 The function name is somewhat confusing. Please co
chengx 2017/04/17 19:00:57 Done.
203 if (::PathFileExistsA(folder_path)) {
204 if (!::PathIsDirectoryEmptyA(folder_path)) {
205 std::cout << folder_path
206 << " directory is not empty, please remove all its content.";
207 return false;
208 }
209 return true;
210 } else if (::CreateDirectoryA(folder_path, NULL) == 0) {
211 std::cout << folder_path << "directory creation fails.";
212 return false;
213 } else {
214 return true;
215 }
216 }
217
218 void DeleteDirectoryContent(const char* folder_path) {
219 char fileFound[256];
stanisc 2017/04/14 23:30:54 Use MAX_PATH instead.
chengx 2017/04/17 19:00:57 Done.
220 WIN32_FIND_DATAA info;
221 HANDLE hp;
222 sprintf(fileFound, "%s\\*.*", folder_path);
223 hp = ::FindFirstFileA(fileFound, &info);
224 do {
225 if (!((strcmp(info.cFileName, ".") == 0) ||
226 (strcmp(info.cFileName, "..") == 0))) {
stanisc 2017/04/14 23:30:55 I suggest using a pattern where you skip these fil
chengx 2017/04/17 19:00:57 Done.
227 if ((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ==
228 FILE_ATTRIBUTE_DIRECTORY) {
stanisc 2017/04/14 23:30:55 Do you really expect nested directories there? You
brucedawson 2017/04/14 23:36:15 Or, when creating files track the names and then d
chengx 2017/04/17 19:00:57 Agree that sub-directories won't be created, so I
229 std::string subFolder = folder_path;
230 subFolder.append("\\");
231 subFolder.append(info.cFileName);
232 DeleteDirectoryContent(subFolder.c_str());
233 ::RemoveDirectoryA(subFolder.c_str());
234 } else {
235 sprintf(fileFound, "%s\\%s", folder_path, info.cFileName);
236 BOOL retVal = ::DeleteFileA(fileFound);
237 }
238 }
239
240 } while (::FindNextFileA(hp, &info));
241 FindClose(hp);
242 }
243
244 bool DeleteDirectory(const char* folder_path) {
245 DeleteDirectoryContent(folder_path);
246 return ::RemoveDirectoryA(folder_path) != 0;
247 }
248
249 void DeleteDirectoryAndPrintMsg(const char* folder_path) {
250 if (DeleteDirectory(folder_path)) {
251 std::cout << folder_path << " directory is deleted!" << std::endl;
252 } else {
253 std::cout << "[Attention] " << folder_path
254 << " directory's deletion fails, please take a look by yourself!"
255 << std::endl;
256 }
257 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698