Debian Codegraph @

tools/fsgenerator/fsgenerator.lpr

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