perl 的調試和性能測試

perl程序的調試,之前玩php都是print,var_dump(),firebug之類來調試。在perl中,也有不少來方法咱們調試的。在我看來調試分二種,一種是功能調試(排錯之類保證功能完整).一種是性能調試。php

功能調試

1. 最經常使用的方法html

(1. print:這個用戶就不用寫了吧數據結構

(2. 模塊: Data::Dumper
這個使用很容易,主要就是使用Dumper的函數作輸出併發

#!/usr/bin/perl  use strict;  use Data::Dumper;    my $hash = {          foo => 'test1',          bar => {              foo1 => 'test2',                  }      };    print Dumper($hash);

這個會很是很是清楚的輸出數據結構的信息:函數

$VAR1 = {            'bar' => {                       'foo1' => 'test2'                     },            'foo' => 'test1'          };

(3 模塊:Smart::Comments
這個模塊就更加好用了,偉大的CPAN…..這個只須要###加上後面的變量就能輸出變量中的信息。你能夠在調試時打開,調試完了就關掉。超爽。oop

#!/usr/bin/perl  use strict;  use Smart::Comments;    my $hash = {          foo => 'test1',          bar => {              foo1 => 'test2',                  }      };    ### hash: $hash

見到上面###後面的內容沒,這樣很不錯吧。。這個更新清楚的打印出數據結構性能

### hash: {  ###         bar => {  ###                  foo1 => 'test2'  ###                },  ###         foo => 'test1'  ###       }

(2 perl的調試器測試

perl -d yourperl.pl

-s Step 運行一行,跟蹤進入子程序
-n Next 運行一行
-r Return 運行到當前子程序結束
-p 變量名 查看變量
-x 變量名 查看變量,友好格式(hash用引用)
-l/-/w 列出先後的代碼
-c 行號 執行到"行號"
-c 執行,直到遇到斷點
-b 行號 設置斷點(b 子程序名)
-b 行號 條件 設置條件斷點
-d 行號 去除"行號"處的斷點
-L 列出全部的斷點
-t 跟蹤執行url

(3 出錯信號spa

這個不在詳細寫了。在個人另外一個文章中,perl中的併發和信號中

性能調試

這個晚點在寫困死了,其它下次寫

(1. test模塊

(2. Devel::Coverage模塊

perl -d:Coverage yourperl.pl

這個主要能夠測試到代碼的覆蓋範圍。分支的運行狀況。

(3. Devel::SmallProf模塊

這個模塊會生成很是不錯的文本文件samllprof.out來顯示各行運行的時間,真實時間和cpu時間。它的精度要求好些因此須要Time::HiRes這個模塊.

perl -d:SmallProf yourperl.pl

暫時關閉和打開profile:

?
$DB::profile = 0;

這之間的代碼不會被SmallProf跟蹤

?
$DB::profile = 1;

指定要跟蹤的模塊名稱,主程序用’main’表示,例如

?
use Data::Dumper;
use TK;
%DB::package = ('main' => 1,
'Data::Dumper' => 1);

只跟蹤主程序和Data::Dumper的代碼執行狀況 不列出執行計數爲0的行

?
$DB::drop_zeros = 1;

(4. Devel::DProf 模塊來以函數爲範圍測試 Perl 程序的性能

perl -d:DProf yourperl.pl

這個和上面的做者大多相同,但這個會生成本身的格式文件,能夠用它來畫圖之類.

使用 dprofpp 來查看輸出是怎麼樣的

?
$dprofpp tmon.out
Total Elapsed Time = 2.447954 Seconds
User+System Time = 2.437954 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
88.1 2.149 2.164 1 2.1488 2.1645 main::parselog
2.87 0.070 0.119 10 0.0070 0.0119 Spreadsheet::WriteExcel::Workbook:
:BEGIN
2.05 0.050 0.257 11 0.0045 0.0234 main::BEGIN
1.23 0.030 0.149 8 0.0037 0.0186 Spreadsheet::WriteExcel::WorkbookB
ig::BEGIN
0.82 0.020 0.020 10 0.0020 0.0020 Spreadsheet::WriteExcel::Worksheet
::BEGIN
0.82 0.020 0.020 154 0.0001 0.0001 Perl6::Junction::One::str_eq
0.41 0.010 0.010 1 0.0100 0.0100 Smart::Comments::__ANON__
0.41 0.010 0.010 11 0.0009 0.0009 DynaLoader::dl_find_symbol
0.41 0.010 0.010 5 0.0020 0.0020 Perl6::Junction::BEGIN
0.41 0.010 0.010 6 0.0017 0.0017 YAML::Syck::BEGIN
0.41 0.010 0.010 5 0.0020 0.0020 IO::Seekable::BEGIN
0.41 0.010 0.010 6 0.0017 0.0016 Encode::BEGIN
0.41 0.010 0.020 5 0.0020 0.0039 Spreadsheet::WriteExcel::OLEwriter
::BEGIN
0.41 0.010 0.020 6 0.0017 0.0033 Filter::Simple::BEGIN
0.41 0.010 0.030 10 0.0010 0.0030 Smart::Comments::BEGIN

(5 Devel::FastProf

使用

perl -d:FastProf filename.pl

這個是需向行的,能夠見到每行花的時間,須要輸出測試的結果用 fprofpp -t 10, 輸出以下

?
# fprofpp output format is:
# filename:line time count: source
CFDS/File.pm:321 4.44711 1321093: return $self->{block}{$bindex}{$pindex}{$key};
CFDS/File.pm:96 3.15971 1319500: if ( exists $fatchPool{$md5url}{$key} ){
/usr/local/share/perl/5.10.1/POE/Loop/Select.pm:199 3.00868 1633: my $hits = CORE::select(
CFDS/File.pm:94 2.47519 1319500: my ($md5url, $bindex, $pindex) = @_;
CFDS/File.pm:95 2.45544 1319500: my $key = $bindex . '|' . $pindex;
CFDS/File.pm:320 2.42175 1321093: my ($bindex, $pindex, $key) = @_;
CFDS/File.pm:275 2.40340 1319500: my $status = $self->lookupFatchPool( $urlMD5, $bindex, $cursor );
CFDS/File.pm:274 2.37050 1319500: my $cursorStatus = $self->getByPiece($bindex, $cursor, 'data');
CFDS/File.pm:276 2.26949 1319500: next if $status or $cursorStatus;
CFDS/File.pm:319 2.20017 1321093: my $self = shift;

(6 DBI::Profile

測試DBI的,沒用過。。。

在這幾個 Perl 的性能測試模塊中,分別是:

  1. Devel::DProf - 以函數爲單位
  2. Devel::SmallProf - 以行爲單位
  3. Devel::FastProf - 同上,但速度更快
  4. Benchmark – 檢查指定代碼塊
相關文章
相關標籤/搜索