优化Perl脚本

nhaq1z21  于 8个月前  发布在  Perl
关注(0)|答案(1)|浏览(113)

有没有可能让这个脚本更快?

#!/usr/bin/perl -w
    use strict;
    use CGI;
    package SwitchGUI;

    sub new {
        my ($classe, $nom, $nbports, $gio) = @_;

        my $this = {
            "nom"     => $nom,
            "nbports" => $nbports,
            "gio"     => $gio
        };

        bless($this, $classe);
        $this->afficher();
        return $this;   
    }

    sub afficher {
        my ($this) = @_;
        my @tab = ( 1 .. $this->{nbports} );
        my @odd = grep { $_ % 2 } @tab;
        my @even = grep { not $_ % 2 } @tab;

        my $cgi = new CGI;
        my $i;
        my $j;

        print "<div id=\"$this->{nom}\" class=\"switch\">\n";
        print $cgi->h2("$this->{nom}");

        print "<div class=\"ports\">";
        for my $port (@odd) {
            my $res = `perl ifname-index.pl -h $this->{nom} -i FastEthernet0/$port -c reseau`;

            if ($res =~ /^Erreur /) {
                print $cgi->img({
                src => 'ressources/interface_haut_down.png',  
                alt => "port n°$port",
                }), "\n",
            }
            else {
                print $cgi->a({class=>"tooltip", title=>$res},$cgi->img({
                src => 'ressources/interface_haut_up.png',  
                alt => "port n°$port",
                }), "\n",)
            }
        }

        print "<br/>";
        for my $port (@even) {
            my $res = `perl ifname-index.pl -h $this->{nom} -i FastEthernet0/$port -c reseau`;      
            if ($res =~ /^Erreur/) {
                print $cgi->img({
                src => 'ressources/interface_bas_down.png',  
                alt => "port n°$port",
                }), "\n",
            }
            else {
                if ($this->getDuplex($res)!="Full") {
                    print $cgi->a({class=>"tooltip", title=>$res},$cgi->img({
                    src => 'ressources/interface_bas_duplex.png',  
                    alt => "port n°$port",
                    }), "\n",)
                }
                elsif ($this->getVitesse($res)!="100"){
                    print $cgi->a({class=>"tooltip", title=>$res},$cgi->img({
                    src => 'ressources/interface_bas_speed.png',  
                    alt => "port n°$port",
                    }), "\n",)
                }
                else {
                    print $cgi->a({class=>"tooltip", title=>$res},$cgi->img({
                    src => 'ressources/interface_bas_up.png',  
                    alt => "port n°$port",
                    }), "\n",)
                }
            }
        }
        print "</div>";
        print "<div class=\"gio\">";
        for ($j=0;$j<$this->{gio};$j++) {
            my $req = system("perl ifname-index.pl -h $this->{nom} -i GigabitEthernet0/$j -c reseau &");
            print $cgi->img({
                src => 'ressources/interface_bas_down.png',  
                alt => "port",
                });
        }
        print "</div>\n";

        print "</div>\n";

    }

    1;

它执行一个perl脚本(使用SNMP来查询网络设备),并根据该脚本的返回,显示相应的图像和描述。该脚本用于从另一个cgi脚本进行的aplog调用。
我的问题是:我可以通过添加&或类似的东西来执行多个脚本吗
在下面一行的末尾?

my $res = `perl ifname-index.pl -h $this->{nom} -i FastEthernet0/$port -c reseau`;
mnemlml8

mnemlml81#

虽然我不想评论太多的东西,如使用CGI和“打印”(在2011年是 * 真的 * 过时),我会评论两行:

my $res = `perl ifname-index.pl -h $this->{nom} -i FastEthernet0/$port -c reseau`;
...
my $req = system("perl ifname-index.pl -h $this->{nom} -i GigabitEthernet0/$j -c reseau &");

启动另一个perl-processes确实会减慢速度。
你在做一个显示HTML的包,但不是用来投票的?
ifname-index.pl重构为子例程。因此,

my $res = get_request_interface(name => $this->{nom}, interface => "FastEthernet0/$port");

或到一个包(正确的方式)-类似.

my $interface = My::Interface::Handler->new();
my $res = $interface->get_request;
...
my $another_result = $interface->get_request;
#etc

和ofc,有可能启动更多(多个)进程并与它们通信,但解决方案可能比将ifname-index.pl重构为子例程更复杂。
总结一个“酷”的应用程序-基于评论:

  • 构建一个网页,在其中列出接口,例如N端口的N状态行
  • 该页面将发送N个(并行)请求到服务器的状态与JavaScript
  • 服务器将执行N个并行SNMP请求,并发送N个异步响应
  • 该页将从服务器获得响应并更新正确的div

采用上述方式:

  • 用户立即得到网页
  • 该页面有一个反馈给用户-“等待,我正在获得状态”
  • 服务器对snmp执行N个并行请求
  • 当响应来自服务器时更新页面

对于Web部分是最好使用PGSI类型的服务器。检查CPAN,存在几个。Tatsuhiko Miyagawa“Perl英雄”这些天:)
附言:

相关问题