summaryrefslogtreecommitdiffstats
path: root/find_regex.cgi
blob: d826c124dd6d9afd7abd0d0f0e4a10faf01b1c81 (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
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
#!/usr/bin/perl

use strict;
use warnings;
use File::Find;
use Time::HiRes qw(gettimeofday tv_interval);
use BSD::Resource;
use Encode qw(decode_utf8);

# 1. Start Benchmark Timer
my $start_time = [gettimeofday];

# Helper to keep HTML output safe
sub escape_html {
	my $str = shift;
	return "" unless defined $str;
	$str =~ s/&/&/g;
	$str =~ s/</&lt;/g;
	$str =~ s/>/&gt;/g;
	$str =~ s/"/&quot;/g;
	$str =~ s/'/&#39;/g;
	return $str;
}

# Parse Query String (q=keyword)
my %params;
if ($ENV{QUERY_STRING}) {
	foreach my $pair (split /&/, $ENV{QUERY_STRING}) {
		my ($key, $value) = split /=/, $pair;
		$value //= '';
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$params{$key} = decode_utf8($value);
	}
}

my $search_text = $params{'q'} || '';
$search_text = substr($search_text, 0, 64);
$search_text =~ s/[^a-zA-Z0-9 ]//g; 

# Configuration
my $directory = '_site/log/';
my @results;
my $files_read = 0;

# 2. The Linear Search (Crawl)
if ($search_text =~ /\S/) { 
	find({
		wanted => sub {
			# Only look at index.html files inside the subdirectories
			return unless -f $_ && $_ eq 'index.html';

			if (open my $fh, '<', $_) {
				$files_read++;
				# Slurp the entire file (approx 16KB per your seed script)
				my $content = do { local $/; <$fh> };
				close $fh;
				
				# Regex match (Case Insensitive)
				if ($content =~ /\Q$search_text\E/i) {
					my ($title) = $content =~ /<title>(.*?)<\/title>/is;
					my ($p_content) = $content =~ /<p[^>]*>(.*?)<\/p>/is;
					
					# Clean up snippet
					my $snippet = $p_content || "";
					$snippet =~ s/<[^>]*>//g; # Strip internal tags
					$snippet =~ s/\s+/ /g;
					$snippet = substr($snippet, 0, 100);

					push @results, { 
						path    => $File::Find::name,
						title   => $title || $File::Find::name, 
						snippet => $snippet . "..."
					};
				}
			}
			# Stop collecting after 20 results for display, 
			# but the benchmark usually looks for unique keywords
			# where only 1 result exists.
		},
		no_chdir => 0, 
	}, $directory);
}

# 3. Calculate Performance Metrics
my $end_time = [gettimeofday];
my $elapsed = tv_interval($start_time, $end_time);

my $rusage = getrusage();
my $user_cpu   = $rusage->utime; 
my $system_cpu = $rusage->stime; 
my $max_rss    = $rusage->maxrss; 

# 4. Generate Output
print "Content-Type: text/html\n\n";

my $list_html = "";
if ($search_text eq '') {
	$list_html = "<p>Please enter a search term.</p>";
} elsif (@results == 0) {
	$list_html = "<p>No results found for \"<b>" . escape_html($search_text) . "</b>\".</p>";
} else {
	$list_html = "<ul>";
	foreach my $res (@results) {
		$list_html .= sprintf('<li><a href="/%s">%s</a><br><small>%s</small></li>', 
			$res->{path}, escape_html($res->{title}), escape_html($res->{snippet}));
	}
	$list_html .= "</ul>";
}

my $safe_q = escape_html($search_text);

print <<"HTML";
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Regex Search Results</title>
	<style>
		body { font-family: sans-serif; line-height: 1.5; padding: 20px; }
		.stats { background: #f4f4f4; padding: 15px; border-radius: 5px; 
		         font-family: monospace; font-size: 0.9em; border: 1px solid #ddd; margin-top: 20px; }
	</style>
</head>
<body>
	<h2>Regex Search (Linear Crawl)</h2>
	<form method="GET">
		<input type="text" name="q" value="$safe_q">
		<input type="submit" value="Search">
	</form>

	$list_html

	<div class="stats">
		<strong>Performance Metrics:</strong><br>
		Total Time:  @{[ sprintf("%.4f", $elapsed) ]} seconds<br>
		User CPU:    $user_cpu s<br>
		System CPU:  $system_cpu s<br>
		Peak RAM:    $max_rss KB<br>
		Files Read:  $files_read (IO Activity)
	</div>
</body>
</html>
HTML