diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2026-05-06 19:42:33 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2026-05-06 19:42:33 +0800 |
| commit | 819bf74c2841fabdcc481e12e13615d48a92cb7f (patch) | |
| tree | 00ba8c9105a96d88536f50f8ef96e838c04408e3 /_site/cgi-bin/find_file.cgi | |
| parent | eddb76ad8c6e850c7e24f97ff27a185d48b104ee (diff) | |
| download | site-search-bm-819bf74c2841fabdcc481e12e13615d48a92cb7f.tar.gz | |
Change directory structure and add benchmark runner.
Diffstat (limited to '_site/cgi-bin/find_file.cgi')
| -rw-r--r-- | _site/cgi-bin/find_file.cgi | 143 |
1 files changed, 0 insertions, 143 deletions
diff --git a/_site/cgi-bin/find_file.cgi b/_site/cgi-bin/find_file.cgi deleted file mode 100644 index 2ffb808..0000000 --- a/_site/cgi-bin/find_file.cgi +++ /dev/null @@ -1,143 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use File::Find; -use Time::HiRes qw(gettimeofday tv_interval); -use BSD::Resource; # For memory and granular CPU usage - -# 1. Start Benchmark Timer -my $start_time = [gettimeofday]; - -sub escape_html { - my $str = shift; - return "" unless defined $str; - $str =~ s/&/&/g; - $str =~ s/</</g; - $str =~ s/>/>/g; - $str =~ s/"/"/g; - $str =~ s/'/'/g; - return $str; -} - -my %params; -if ($ENV{QUERY_STRING}) { - foreach my $pair (split /&/, $ENV{QUERY_STRING}) { - my ($key, $value) = split /=/, $pair; - $value =~ tr/+/ /; - $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; - $params{$key} = $value; - } -} - -my $search_text = $params{'q'} || ''; -$search_text = substr($search_text, 0, 64); -$search_text =~ s/[^a-zA-Z0-9 ]//g; - -my $directory = '../log/'; -my @results; - -my %excluded_files = ( - 'index.html' => 1, -); - -# Track IO operations (simple count) -my $files_read = 0; - -if ($search_text =~ /\S/) { - find({ - wanted => sub { - return unless -f $_ && $_ eq 'index.html'; - my $rel_path = $File::Find::name; - $rel_path =~ s|^\Q$directory\E/?||; - return if $excluded_files{$rel_path}; - - if (open my $fh, '<', $_) { - $files_read++; # Tracking IO - my $content = do { local $/; <$fh> }; - close $fh; - - if ($content =~ /\Q$search_text\E/i) { - my ($title) = $content =~ /<title>(.*?)<\/title>/is; - $title = $title ? escape_html($title) : $rel_path; - my ($p_content) = $content =~ /<p[^>]*>(.*?)<\/p>/is; - my $snippet = $p_content || ""; - $snippet =~ s/<[^>]*>//g; - $snippet =~ s/\s+/ /g; - - $snippet = escape_html(substr($snippet, 0, 50)); - $snippet .= "..." if length($p_content || "") > 50; - - push @results, { - path => $File::Find::name, - title => $title, - snippet => $snippet - }; - } - } - }, - no_chdir => 0, - follow => 0, - }, $directory); -} - -# --- 2. Calculate Metrics --- -my $end_time = [gettimeofday]; -my $elapsed = tv_interval($start_time, $end_time); - -# CPU & RAM Usage via BSD::Resource -my $rusage = getrusage(); -my $user_cpu = $rusage->utime; # User CPU time -my $system_cpu = $rusage->stime; # System CPU time -my $max_rss = $rusage->maxrss; # Peak RAM (usually in KB on Linux) - -# --- 3. Output --- -print "Content-Type: text/html\n\n"; - -my $list; -if ($search_text eq '') { - $list = "<p>Please enter a search term above.</p>"; -} elsif (@results == 0) { - $list = "<p>No results found for \"<b>$search_text</b>\".</p>"; -} else { - $list = "<ul>"; - foreach my $res (@results) { - my $url = $res->{path}; - $list .= "<li><a href=\"/$url\">$res->{title}</a><br><small>$res->{snippet}</small></li>"; - } - $list .= "</ul>"; -} - -my $safe_search_text = escape_html($search_text); - -print <<"HTML"; -<!DOCTYPE html> -<html lang="en-us"> -<head> - <meta charset="utf-8"> - <title>Search Results</title> - <style> - .stats { background: #f4f4f4; padding: 10px; border-radius: 5px; font-family: monospace; font-size: 0.85em; margin-top: 20px; border: 1px solid #ddd; } - </style> -</head> -<body> - <div class="container"> - <h2>Search</h2> - <form action="" method="GET"> - <input type="text" name="q" value="$safe_search_text"> - <input type="submit" value="Search"> - </form> - $list - - <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> - </div> -</body> -</html> -HTML |
