优化Perl脚本

eou*_*uti 0 optimization perl cgi


是否有可能使这个脚本更快?

#!/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;
Run Code Online (Sandbox Code Playgroud)

它执行perl脚本(使用SNMP查询网络设备),并根据此脚本的返回,显示适当的图像和描述.此脚本用于另一个cgi脚本的ajax调用.

我的问题是:我可以通过 在以下行的末尾添加&或类似
的东西来执行多个脚本吗?

my $res = `perl ifname-index.pl -h $this->{nom} -i FastEthernet0/$port -c reseau`;
Run Code Online (Sandbox Code Playgroud)

jm6*_*666 7

虽然我不想评论很多像使用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 &");
Run Code Online (Sandbox Code Playgroud)

启动另一个perl进程确实会降低速度.
您正在制作用于显示HTML的包,但不用于轮询?

重新ifname-index.pl分配子程序.所以,

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

或者包裹(正确的方式) - 像......

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

而且,有可能启动更多(多个)进程并与它们通信,但解决方案可能比将ifname-index.pl重构为子例程更复杂.(请阅读:http://faq.perl.org/perlfaq8.html#How_do_I_start_a_pro)

摘要"酷"应用程序 - 基于评论:

  • 构建一个列出接口的网页,例如N个端口的N状态行
  • 该页面将使用javascript向服务器发送N ajax(并行)请求以获取状态
  • 服务器将执行N个并行SNMP请求,并发送N个ajax响应
  • 该页面将从服务器获得响应并更新正确的div

以上方式:

  • 用户立即获得一个网页
  • 该页面有一个用户反馈 - "等等,我正在努力获取状态"
  • 服务器执行N个并行请求snmp
  • ajax响应更新页面,因为它们来自服务器

对于Web部件,最好使用PGSI型服务器.检查CPAN,存在几个. 这些天Tatsuhiko Miyagawa "Perl Hero":)

PS: