在交付健壯產品質量的漫長過程中,編寫代碼只是第一階段。必須對代碼進行測試,檢驗操作與準確性;往往還需分析代碼,消除性能瓶頸與資源浪費(特別是記憶體);還必須監視代碼,定位故障、識別使用模式、尋找優化機會、探測入侵嘗試。
Eclipse Test and Performance Tools Platform (TPTP) 是一種軟體架構及擴展 Eclipse 平台的組件,包含測試、效能與監視工具。本系列教程講解 TPTP 功能:第 1 部分示範分析 Java 應用;第 2 部分講解捕獲日誌並轉換為 CBE 格式;第 3 部分解釋如何管理應用程式測試。
本教程演示如何使用 TPTP 測試特性度量 Web 應用響應時間,講解創建與管理改進的 JUnit 測試,描述創建計算機引導的手工測試。
學習使用 Eclipse TPTP 測試特性度量 Web 應用響應時間;創建與管理改進的 JUnit 測試;創建計算機引導的手工測試。
具備軟體開發經驗並了解完整軟體開發生命週期;熟悉命令列安裝軟體、設置與管理 shell 和系統環境變量(PATH、LD_LIBRARY_PATH、CLASSPATH);了解 Java、Eclipse IDE 與 UI 有幫助但非必需。
可在支援 JVM 的任何系統運行 Eclipse(Solaris、Linux、Mac OS X、Windows)。無 JVM 需預留 250 MB 磁碟空間。核心 Eclipse 與插件需 50 MB。建議 64 MB 以上空閒物理記憶體。
下載所需組件後安裝。以下說明適用 Linux/UNIX;Windows 請參閱 Eclipse.org。假設檔案放在 /tmp:
# 解壓 JRE 到 ~/java-tptp
mkdir ~/java-tptp; cd ~/java-tptp
sh /tmp/jre-1_5_0_07-linux-i586.bin
# 解壓 Eclipse、TPTP 及前置套件
tar xzvf /tmp/eclipse-SDK-3.2-linux-gtk.tar.gz
unzip /tmp/xsd-runtime-2.2.0.zip
unzip /tmp/tptp.platform.runtime-TPTP-4.2.0.zip
unzip /tmp/tptp.test.runtime-TPTP-4.2.0.zip
unzip /tmp/emf-sdo-runtime-2.2.0.zip
# 解壓、配置、啟動 Agent Controller
mkdir tptpd; cd tptpd
unzip /tmp/tptpdc.linux_ia32-TPTP-4.2.0.zip
chmod +x ./{bin,lib}/*
# 設定環境變量
export JAVA_HOME=~/java-tptp/jre1.5.0_07
export CLASSPATH=$JAVA_HOME
export PATH=$JAVA_HOME/bin:$JAVA_HOME/tptpd/bin:$PATH
export MOZILLA_FIVE_HOME=/usr/lib/mozilla
export LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME:$JAVA_HOME/tptpd/lib:$LD_LIBRARY_PATH
# 啟動 Agent Controller
cd ./bin
./SetConfig.sh
./RAStart.sh
# 啟動 Eclipse
~/java-tptp/eclipse/eclipse
或使用 Eclipse 內建 Update Manager 安裝 TPTP:Software Updates → Find and Install → Search for new features → New Remote Site,名稱 TPTP Update Site,URL https://eclipse.org/tptp/updates/site.xml,完成安裝後重啟 Eclipse。
提供四種測試軟體:
本教程演示 URL 工具、JUnit 工具、手工測試工具。
Web 應用性能測試因組件眾多(瀏覽器、網絡、Web 伺服器、遠程主機、應用、資料庫等)而複雜。TPTP URL 工具旨在記錄與自動回放用戶交互並基準測試,能記錄每次「點擊」並度量請求響應延遲。
範例 PHP 應用(清單 2)消耗 CPU 週期並連接 Web 伺服器,可選擇休眠 0.25s、0.5s、1s。部署為 $DOCROOT/burn/index.php,瀏覽器訪問 https://localhost/burn/index.php。
<!DOCTYPE html ...>
<html ...>
<head><title>Consume Cycles Example</title></head>
<body>
<?php
$time = 0;
switch($_GET['mode']) {
case 'short': $time=250; break;
case 'medium': $time=500; break;
case 'long': $time=1000; break;
}
$time *= 1000;
if ($time > 0) { usleep($time); print "<p>I snoozed for $time microseconds</p>"; }
?>
<h3>Choose how long you want the application to sleep:</h3>
<ul>
<li><a href="index.php?mode=short">A short time</a></li>
<li><a href="index.php?mode=medium">A little while</a></li>
<li><a href="index.php?mode=long">A long time</a></li>
</ul>
</body></html>
PHP 的一般專案。Simple,完成。Simple.testsuite。Simple 選 Generate,選擇 Java 專案 Testing,生成 JUnit 測試代碼。Simple.java 選 Run As → JUnit test 驗證功能;再切回 Test Perspective,右鍵 test 資料夾選 Run As → Run,選擇 Simple 執行性能測試。Simple,Events 標籤查看響應時間(毫秒)。Simple 選 Options → Report → HTTP Page Response Time 查看總結。範例類 Account(清單 3)建模銀行帳戶,含 balance、deposit、withdrawal、compoundAnnually 方法。
package demo;
import java.lang.Math;
public class Account {
private double balance = 0;
public Account() { this.balance = 0; }
public Account(double deposit) { this.balance = 0; if (deposit > 0) this.balance = deposit; }
public double deposit(double deposit) { if (deposit < 0) return this.withdrawal(deposit); this.balance += deposit; return this.balance; }
public double withdrawal(double withdrawal) { if (withdrawal < 0) return this.deposit(-withdrawal); if (this.balance >= withdrawal) this.balance -= withdrawal; return this.balance; }
public double balance() { return this.balance; }
public double compoundAnnually(double rate) { int n = 356 * 24; if (rate > 0) this.balance *= Math.pow((1 + (rate/n)), n); return this.balance; }
}
Demo,包 demo 下建立 Account 類貼上代碼。Account.java 選 New → Other → Test → TPTP JUnit Test,命名 AccountTest,添加三個測試方法:testCreateEmpty、testCreateBalance、testVerifyDeposit。public void testCreateEmpty() throws Exception {
Account zeroAccount = new Account();
assertEquals("balance not zero", zeroAccount.balance(), 0.0, 0.01);
}
public void testCreateBalance() throws Exception {
double startingBalance = 510.96;
Account newAccount = new Account(startingBalance);
assertEquals("starting balance incorrect", newAccount.balance(), startingBalance, 0.01);
}
public void testVerifyDeposit() throws Exception {
double startingBalance = 987.91, deposit = 1560.98;
Account newAccount = new Account(startingBalance);
assertEquals("deposit not credited", newAccount.deposit(deposit), deposit+startingBalance, 0.01);
}
AccountTest.testsuite 選 Run As → Test,生成執行日誌 AccountTest_#.execution。testVerifyDeposit 斷言),可看到詳細錯誤與堆疊追蹤。並非所有測試可自動化。TPTP 提供工具創建計算機引導的手工測試與測試套件。可創建測試案例在多套件重用,調整步驟順序,指定循環重複,「運行」手工測試套件時 TPTP 引導執行每步並記錄結果。
PHP 資料夾 New → Other → Manual TPTP Test,命名描述性名稱。Manual Web test 選 Run As → Run,選擇 Manual Web test 與 local_deployment 執行。軟體構建兼具藝術與科學。若開發者不發揮創造力,代碼無法最快運行,問題糾纏不清,電腦可能比打字機強不了多少。雖軟體非齒輪發動機組成的物理機器,但有許多必須緊密咬合的「運動部件」。應善用 Eclipse TPTP 編輯構建代碼、測試、調整、觀察性能與穩定性。精明的開發者應好好利用 Eclipse。