Merge branch 'facet-eval'
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 26 Jun 2010 22:47:24 +0000 (00:47 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 26 Jun 2010 22:47:24 +0000 (00:47 +0200)
lib/MojoFacets.pm
lib/MojoFacets/Config.pm
lib/MojoFacets/Plugin/NYTProf.pm [new file with mode: 0644]
templates/config/index.html.ep

index fb45ed5..cfddc99 100644 (file)
@@ -38,6 +38,7 @@ sub save_tx {
        }
 }
 
+use MojoFacets::Plugin::NYTProf;
 
 # This method will run once at server start
 sub startup {
@@ -58,6 +59,8 @@ sub startup {
                                save_tx( $self, $tx );
                        }
        );
+
+       MojoFacets::Plugin::NYTProf->register( $self );
 }
 
 
index 4d4a2b2..14f8c51 100644 (file)
@@ -8,14 +8,23 @@ use base 'Mojolicious::Controller';
 sub index {
        my $self = shift;
 
-       foreach my $name ( qw( MASTER MAX_FACETS ) ) {
-               if ( my $val = $self->param($name) ) {
+       my @config = (qw(
+               MASTER
+               MAX_FACETS
+               PROFILE
+       ));
+
+       foreach my $name ( @config ) {
+               my $val = $self->param($name);
+               if ( defined $val ) {
                        $ENV{$name} = $val;
                        warn "$name = $val\n";
                }
        }
 
-       $self->render;
+       $self->render(
+               config => \@config,
+       );
 }
 
 1;
diff --git a/lib/MojoFacets/Plugin/NYTProf.pm b/lib/MojoFacets/Plugin/NYTProf.pm
new file mode 100644 (file)
index 0000000..7cf729a
--- /dev/null
@@ -0,0 +1,42 @@
+package MojoFacets::Plugin::NYTProf;
+
+use Devel::NYTProf;
+use Time::HiRes ();
+
+sub register {
+       my ($self, $app) = @_;
+
+       # Start timer
+       $app->plugins->add_hook(
+               before_dispatch => sub {
+                       my ($self, $c) = @_;
+                       return unless $ENV{PROFILE};
+                       my $id = Time::HiRes::gettimeofday();
+                       $c->stash('nytprof.id' => $id);
+                       my $path = "/tmp/nytprof.$id";
+                       DB::enable_profile($path);
+               }
+       );
+
+       # End timer
+       $app->plugins->add_hook(
+               after_dispatch => sub {
+                       my ($self, $c) = @_;
+                       my $p = $ENV{PROFILE} || return;
+                       DB::disable_profile();
+                       return unless my $id = $c->stash('nytprof.id');
+                       my $duration = Time::HiRes::gettimeofday() - $id;
+                       if ( $duration > $p ) {
+                               my $path = "/tmp/nytprof.$id";
+                               my $new  = "/tmp/MojoFacets.profile-$id-$duration";
+                               rename $path, $new;
+                               warn "profile $new $duration ", -s $new, " bytes\n";
+                       } else {
+                               warn "profile $path $duration < $p unlink\n";
+                               unlink $path;
+                       }
+               }
+       );
+}
+
+1;
index 2a0cb39..6872ff3 100644 (file)
@@ -6,13 +6,16 @@
 
 <ul>
 
-<h2>Replication</h2>
-<li>MASTER
-<input type=text name=MASTER value="<%= $ENV{MASTER} %>">
+% foreach my $name ( @$config ) {
 
-<h2>Facet display</h2>
-<li>MAX_FACETS
-<input type=text name=MAX_FACETS value="<%= $ENV{MAX_FACETS} %>">
+<li>
+<label>
+<%= $name %>
+<input type=text name=<%= $name %> value="<%= $ENV{$name} %>">
+</label>
+</li>
+
+% }
 
 </ul>