summaryrefslogtreecommitdiffstats
path: root/bm/bm_size.pl
blob: af8c8e7e162c0ca1871f1fc26a8630b07ec3d353 (plain)
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
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use File::Path qw(remove_tree);
use Cwd qw(getcwd abs_path);

my ($files, $depth) = @ARGV;
if (!defined $files || !defined $depth) {
	die "Usage: perl bench.pl <files> <depth>\n";
}

my $base_dir = getcwd();
my $urn_bin  = abs_path(File::Spec->catfile("..", "urn")); 
my $seed_bin = abs_path("seed.pl");
my $bm_repo  = "bm_repo";

# Storage for results: $results{Action}{Tool} = "metrics string"
my %results;
my @actions = qw(Status Add Commit);

sub run_benchmark {
	my ($tool_name) = @_;
	
	print "Running $tool_name...\n";

	# 1. Seed
	system("perl $seed_bin $files $depth > /dev/null 2>&1") == 0 or die "Seeding failed";
	chdir($bm_repo) or die "Could not enter $bm_repo: $!";

	# 2. Init
	my $init_cmd = ($tool_name eq "URN") ? "perl $urn_bin init" : "git init";
	system("$init_cmd > /dev/null 2>&1");

	# 3. Timed Actions
	my %cmds = (
		"Status" => ($tool_name eq "URN") ? "perl $urn_bin status" : "git status",
		"Add"    => ($tool_name eq "URN") ? "perl $urn_bin add ."   : "git add .",
		"Commit" => ($tool_name eq "URN") ? "perl $urn_bin commit -m 'bench'" : "git commit -m 'bench'",
	);

	foreach my $action (@actions) {
		my $cmd = $cmds{$action};
		# Extract just real time and max rss for the table
		my $raw = `(/usr/bin/time -l $cmd > /dev/null) 2>&1`;
		
		# Parse specific metrics: Real time and Max RSS
		my ($real) = $raw =~ /(\d+\.\d+)\s+real/;
		my ($rss)  = $raw =~ /(\d+)\s+maximum resident set size/;
		
		$results{$action}{$tool_name} = { 
			time => $real // "N/A", 
			mem  => $rss  ? sprintf("%.2f MB", $rss / 1024 / 1024) : "N/A" 
		};
	}

	chdir($base_dir) or die $!;
	remove_tree($bm_repo) if -d $bm_repo;
}

# Run the sequence
run_benchmark("URN");
run_benchmark("GIT");

# --- Generate the report ---
my $out_file = "BM_COMPARE_${files}_${depth}.txt";
open(my $res, '>', $out_file) or die $!;

my $header = sprintf("%-12s | %-18s | %-18s", "ACTION", "URN (Time/Mem)", "GIT (Time/Mem)");
my $sep    = "-" x length($header);

print $res "BENCHMARK: $files files, $depth depth\n";
print $res "$sep\n$header\n$sep\n";

foreach my $action (@actions) {
	my $u = $results{$action}{"URN"};
	my $g = $results{$action}{"GIT"};
	
	printf $res "%-12s | %6ss / %-8s | %6ss / %-8s\n", 
		$action, 
		$u->{time}, $u->{mem},
		$g->{time}, $g->{mem};
}

print $res "$sep\n";
close($res);

print "\nComparison complete! Results saved to $out_file\n";
system("cat $out_file");