fsgenerator.lpr 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. {
  2. Filesystem traffic generator
  3. -------------------------------------------------------------------------
  4. Creates, modifies, removes files, quickly and in large quantities.
  5. Useful for testing how a program behaves when there's a lot of traffic
  6. happening on the file system.
  7. Copyright (C) 2010-2012 Przemysław Nagay ([email protected])
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. }
  20. program fsgenerator;
  21. {$mode objfpc}{$H+}
  22. uses
  23. SysUtils, Classes, Windows;
  24. var
  25. Path: UTF8String;
  26. fs: TFileStream;
  27. filenames: TStringList;
  28. nr: Integer;
  29. buffer: array[0..16383] of byte;
  30. procedure GenNames;
  31. var
  32. i, j: Integer;
  33. name: String;
  34. begin
  35. for i := 0 to Random(1000) do
  36. begin
  37. name := '';
  38. for j := 0 to random(100) do
  39. name := name + chr(random(ord('z') - ord('a')) + ord('a'));
  40. filenames.Add(name);
  41. end;
  42. end;
  43. function RandomName: String;
  44. begin
  45. Result := Path + filenames[Random(Filenames.Count)];
  46. end;
  47. procedure Create(name: String);
  48. begin
  49. fs := TFileStream.Create(name, fmCreate);
  50. fs.Write(buffer, random(sizeof(buffer)));
  51. fs.Free;
  52. end;
  53. procedure Modify(name: String);
  54. var
  55. P: Int64;
  56. count: Int64;
  57. Mode: Word;
  58. size: int64;
  59. begin
  60. if not FileExists(name) then
  61. mode := fmCreate
  62. else
  63. mode := fmOpenReadWrite;
  64. fs := TFileStream.Create(Name, mode);
  65. if mode = fmCreate then
  66. begin
  67. fs.Write(buffer, random(sizeof(buffer)));
  68. fs.Seek(0, soBeginning);
  69. end;
  70. size := fs.size;
  71. p := random(size);
  72. fs.Seek(p, soBeginning);
  73. count := min(sizeof(buffer),random(size-p));
  74. fs.Write(buffer, count);
  75. //writeln('writing ',count, ' p=',p,' size=',size);
  76. fs.Free;
  77. end;
  78. procedure Delete(name: String);
  79. begin
  80. if FileExists(Name) then
  81. Sysutils.DeleteFile(Name);
  82. end;
  83. begin
  84. if Paramcount = 0 then
  85. begin
  86. WriteLn('File system traffic generator.');
  87. WriteLn('Creates, modifies, removes files, quickly and in large quantities.');
  88. Writeln;
  89. WriteLn('Usage:');
  90. WriteLn(ExtractFileName(ParamStr(0)) + ' <destination_path>');
  91. Exit;
  92. end;
  93. FileNames := TStringList.Create;
  94. GenNames;
  95. Path := IncludeTrailingPathDelimiter(ParamStr(1));
  96. ForceDirectories(Path);
  97. WriteLn('Starting changing ', Path);
  98. while True do
  99. begin
  100. case Random(6) of
  101. 0: Sleep(10);
  102. 1: Modify(RandomName);
  103. 2: Create(RandomName);
  104. 3: Modify(RandomName);
  105. 4: Delete(RandomName);
  106. 5: Modify(RandomName);
  107. end;
  108. Sleep(10);
  109. if (GetKeyState(VK_SPACE) < 0) or
  110. (GetKeyState(VK_SHIFT) < 0) or
  111. (GetKeyState(VK_ESCAPE) < 0) then
  112. Break;
  113. end;
  114. WriteLn('Finished changing');
  115. Filenames.Free;
  116. end.