1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
#!/usr/bin/perl
# Usage: perl bm_commit.pl <file_count> <depth> <total_commits> <file_perc> <line_perc>
# Example: perl bm_commit.pl 100 5 50 10 2
use strict;
use warnings;
use File::Copy;
use File::Spec;
use File::Path qw(remove_tree make_path);
use File::Find;
use File::Basename;
use Cwd qw(getcwd abs_path);
use Time::HiRes qw(time);
my ($files, $depth, $total_commits, $file_perc, $line_perc) = @ARGV;
if (!defined $line_perc) {
die "Usage: perl bm_commit.pl <file_count> <depth> <total_commits> <file_perc> <line_perc>\n";
}
my $base_dir = getcwd();
my $urn_bin = abs_path(File::Spec->catfile("..", "urn"));
my $seed_bin = abs_path("seed.pl");
my $bm_repo = "sandbox";
my $sample_rate = int($total_commits / 5) || 1;
my %results;
my %final_stats = ( URN => { size => "N/A", inodes => 0 }, GIT => { size => "N/A", inodes => 0 } );
my $initial_repo_size = "0 KB";
my $rebase_count = 0;
my %last_base_hashes;
sub get_size {
my $dir = shift;
return "0 KB" unless -d $dir;
my $size = `du -sk $dir 2>/dev/null`;
$size =~ /^(\d+)/;
return ($1 || 0) . " KB";
}
sub count_inodes {
my $dir = shift;
return 0 unless -d $dir;
my %inodes;
find(sub { my @s = lstat($_); $inodes{$s[1]} = 1 if @s; }, $dir);
return scalar(keys %inodes);
}
sub track_rebases {
return unless -f ".urn/index";
open(my $fh, '<', ".urn/index") or return;
while (<$fh>) {
chomp; my @cols = split(/\t/); next unless @cols >= 6;
my ($b_hash, $path) = ($cols[2], $cols[5]);
$rebase_count++ if exists $last_base_hashes{$path} && $last_base_hashes{$path} ne $b_hash;
$last_base_hashes{$path} = $b_hash;
}
close($fh);
}
sub generate_surgical_line {
my ($original_line) = @_;
my @types = qw(int char float double bool uint32_t);
my @vars = qw(offset limit buffer status count ptr index);
$original_line =~ /^(\s*)/;
my $indent = $1 || " ";
my $type = $types[rand @types];
my $var = $vars[rand @vars] . "_" . int(rand(100));
my $val = int(rand(1000));
my $new_line = "$indent$type $var = $val;\n";
# Byte matching: pad with spaces before the newline to keep file size identical
my $target_len = length($original_line);
if (length($new_line) < $target_len) {
substr($new_line, -1, 0, " " x ($target_len - length($new_line)));
} elsif (length($new_line) > $target_len) {
$new_line = substr($new_line, 0, $target_len - 2) . ";\n";
}
return $new_line;
}
sub run_commit_benchmark {
my ($tool_name) = @_;
print ">>> Starting COMMIT BENCHMARK: $tool_name\n";
remove_tree($bm_repo) if -d $bm_repo;
system("perl $seed_bin $files $depth > /dev/null 2>&1");
$initial_repo_size = get_size($bm_repo) if $initial_repo_size eq "0 KB";
chdir($bm_repo) or die $!;
my $init_cmd = ($tool_name eq "URN") ? "perl $urn_bin init" : "git init";
my $add_cmd = ($tool_name eq "URN") ? "perl $urn_bin add ." : "git add .";
system("$init_cmd > /dev/null 2>&1");
system("$add_cmd > /dev/null 2>&1");
system(($tool_name eq "URN" ? "perl $urn_bin" : "git") . " commit -m 'initial' > /dev/null 2>&1");
track_rebases() if $tool_name eq "URN";
my @file_list;
find(sub { push @file_list, $File::Find::name if -f $_ && $File::Find::name !~ /\.(git|urn)/ }, ".");
my $num_to_change = int(scalar(@file_list) * ($file_perc / 100)) || 1;
my @target_files = @file_list[0 .. $num_to_change - 1];
for my $i (2 .. $total_commits) {
for my $target (@target_files) {
open(my $fh, '<', $target) or die "Read fail: $target - $!";
my @lines = <$fh>;
close($fh);
# Find lines that are simple variable assignments to maintain surgical diffs
my @eligible = grep { $lines[$_] =~ /^\s*\w+ \w+ = \d+;/ } (0 .. $#lines);
if (@eligible) {
# Calculate count based on percentage of eligible lines
my $to_mod = int(scalar(@eligible) * ($line_perc / 100));
$to_mod = 1 if $to_mod == 0;
my @indices = (sort { rand() <=> rand() } @eligible)[0 .. $to_mod - 1];
for my $idx (@indices) {
next unless defined $idx;
$lines[$idx] = generate_surgical_line($lines[$idx]);
}
open(my $out, '>', $target) or die "Write fail: $target - $!";
print $out join('', @lines);
close($out);
utime(undef, undef, $target);
}
}
my $cmd = ($tool_name eq "URN")
? "perl $urn_bin add . && perl $urn_bin commit -m 'c$i'"
: "git add . && git commit -m 'c$i'";
if ($i % $sample_rate == 0 || $i == $total_commits) {
my $raw_output = `/usr/bin/time -l sh -c "$cmd" 2>&1`;
track_rebases() if $tool_name eq "URN";
my ($real, $rss, $maj, $min) = (0, 0, 0, 0);
$real = $1 if $raw_output =~ /(\d+\.\d+)\s+real/;
$rss = sprintf("%.2f MB", $1 / 1024 / 1024) if $raw_output =~ /(\d+)\s+maximum resident set size/;
$min = $1 if $raw_output =~ /(\d+)\s+page reclaims/;
$maj = $1 if $raw_output =~ /(\d+)\s+page faults/;
my $meta = ($tool_name eq "URN") ? ".urn" : ".git";
$results{$i}{$tool_name} = {
real => $real . "s",
rss => $rss || "0 MB",
faults => "Maj:$maj / Min:$min",
inodes => count_inodes($meta),
size => get_size($meta),
};
print " [Commit $i] $tool_name sampled.\n";
} else {
system("$cmd > /dev/null 2>&1");
track_rebases() if $tool_name eq "URN";
}
}
system("git gc --prune=now --quiet") if $tool_name eq "GIT";
my $final_meta = ($tool_name eq "URN") ? ".urn" : ".git";
$final_stats{$tool_name} = {
size => get_size($final_meta) || "0 KB",
inodes => count_inodes($final_meta) || 0,
};
chdir($base_dir);
remove_tree($bm_repo);
}
run_commit_benchmark("URN");
run_commit_benchmark("GIT");
my $out_file = "BM_COMMIT_${files}_${total_commits}.txt";
open(my $res, '>', $out_file) or die $!;
print $res "=============================================================\n";
print $res " COMMIT BENCHMARK: $files files ($total_commits commits)\n";
print $res " CONDITIONS: Depth=$depth, Files Mod=$file_perc%, Line Mod=$line_perc%\n";
print $res " INITIAL REPO SIZE: $initial_repo_size\n";
print $res "=============================================================\n\n";
foreach my $i (sort { $a <=> $b } keys %results) {
my $u = $results{$i}{"URN"};
my $g = $results{$i}{"GIT"};
next unless defined $u && defined $g;
print $res "SNAPSHOT: Commit #$i\n";
print $res "-------------------------------------------------------------\n";
printf $res "%-15s | %-20s | %-20s\n", "METRIC", "URN", "GIT";
print $res "----------------+----------------------+---------------------\n";
printf $res "%-15s | %20s | %20s\n", "Time", $u->{real}, $g->{real};
printf $res "%-15s | %20s | %20s\n", "Max RSS", $u->{rss}, $g->{rss};
printf $res "%-15s | %20s | %20s\n", "Page faults", $u->{faults}, $g->{faults};
printf $res "%-15s | %20s | %20s\n", "Inodes", $u->{inodes}, $g->{inodes};
printf $res "%-15s | %20s | %20s\n", "Repo size", $u->{size}, $g->{size};
print $res "-------------------------------------------------------------\n\n";
}
print $res "AFTER GIT GC\n";
print $res "-------------------------------------------------------------\n";
printf $res "%-15s | %20s | %20s\n", "Final Size", $final_stats{URN}{size}, $final_stats{GIT}{size};
printf $res "%-15s | %20s | %20s\n", "Final Inodes", $final_stats{URN}{inodes}, $final_stats{GIT}{inodes};
print $res "-------------------------------------------------------------\n\n";
print $res "TOTAL URN REBASES: $rebase_count\n";
close($res);
my $pager = $ENV{PAGER} || 'less';
system("$pager $out_file");
|