1#!/usr/local/bin/perl -w
2###########################################
3# Log4perl Benchmarks
4# Mike Schilli, 2008 (m@perlmeister.com)
5###########################################
6use strict;
7use Benchmark qw(timeit timestr);
8use Log::Log4perl qw(:easy);
9use Sysadm::Install qw(:all);
10use Data::Dumper;
11use File::Temp qw(tempfile);
12
13my($tmp_fh, $tmp_file) = tempfile( UNLINK => 1 );
14
15my $nof_tests = 100000;
16
17print "sp=suppressed w=watch sc=subcategory\n\n";
18
19for my $watch (0, 1) {
20    test_init({ level => "DEBUG", watch => $watch });
21    run("sp0 sc0 w$watch", \&debug_logger);
22
23    test_init({ level => "ERROR", watch => $watch });
24    run("sp1 sc0 w$watch", \&debug_logger);
25
26    test_init({ level => "DEBUG", watch => $watch });
27    run("sp0 sc1 w$watch", \&subcat_logger);
28
29    test_init({ level => "ERROR", watch => $watch });
30    run("sp1 sc1 w$watch", \&subcat_logger);
31}
32
33###########################################
34sub run {
35###########################################
36    my($name, $sub) = @_;
37
38    my $t = timeit(1, $sub);
39    printf "$name: %8.0f per sec\n", $nof_tests/$t->[1];
40}
41
42###########################################
43sub test_init {
44###########################################
45    my($opts) = @_;
46
47    my $conf = qq{
48        log4perl.logger = $opts->{level}, testapp
49        log4perl.appender.testapp       = Log::Log4perl::Appender::TestBuffer
50        log4perl.appender.testapp.layout= SimpleLayout
51    };
52
53    if($opts->{watch}) {
54        blurt $conf, $tmp_file;
55        Log::Log4perl->init_and_watch( $tmp_file );
56    } else {
57        Log::Log4perl->init( \$conf );
58    }
59}
60
61###########################################
62sub debug_logger {
63###########################################
64    my $logger = get_logger("");
65
66    for(1..$nof_tests) {
67        $logger->debug( "message" );
68    }
69}
70
71###########################################
72sub subcat_logger {
73###########################################
74    my $logger = get_logger("a.b.c.d.e.f.g.h.i.j.k");
75
76    for(1..$nof_tests) {
77        $logger->debug( "message" );
78    }
79}
80