1. 程式人生 > >perl 的除錯和效能測試

perl 的除錯和效能測試

perl程式的除錯,以前玩php都是print,var_dump(),firebug之類來除錯。在perl中,也有很多來方法我們除錯的。在我看來除錯分二種,一種是功能除錯(排錯之類保證功能完整).一種是效能除錯。

功能除錯

1. 最常用的方法

(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…..這個只需要###加上後面的變數就能輸出變數中的資訊。你可以在除錯時開啟,除錯完了就關掉。超爽。

#!/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 跟蹤執行

(3 出錯訊號

這個不在詳細寫了。在我的另一個文章中,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 的效能測試模組中,分別是: