、為什么用命令行模式
使用GUI方式啟動jmeter,運行線程較多的測試時,會造成內存和CPU的大量消耗,導致客戶機卡死;
所以一般采用的方式是在GUI模式下調整測試腳本,再用命令行模式執行;
命令行方式支持在多個環境下使用,windosw的dos環境下,也可以在linux環境上執行。
注意:使用命令執行jmeter腳本必須使用jmeter 3.0及以上版本。
2、怎么用
2.1、執行命令
jmeter -n -t <testplan filename> -l <listener filename>
示例: jmeter -n -t testplan.jmx -l test.jtl
示例含義:表示以命令行模式運行testplan.jmx文件,輸出的日志文件為test.jtl
2.2、參數介紹
Jmeter官方手冊給的介紹如下:
-h, –help -> prints usage information and exit
-n, –nongui -> run JMeter in nongui mode
-t, –testfile <argument> -> the jmeter test(.jmx) file to run
-l, –logfile <argument> -> the file to log samples to
-r, –runremote -> Start remote servers (as defined in remote_hosts)
-H, –proxyHost <argument> -> Set a proxy server for JMeter to use
-P, –proxyPort <argument> -> Set proxy server port for JMeter to use
中文釋義:
-h 幫助 -> 打印出有用的信息并退出
-n 非 GUI 模式 -> 在非 GUI 模式下運行 JMeter
-t 測試文件 -> 要運行的 JMeter 測試腳本文件
-l 日志文件 -> 記錄結果的文件
-R 遠程執行 -> 遠程執行機的IP(ip地址)如果有多個ip時,使用-R 192.168.2.170,192.168.2.171(分布式使用)
-r 遠程執行 -> 在Jmter.properties文件中指定的所有遠程服務器(分布式使用)
-H 代理主機 -> 設置 JMeter 使用的代理主機
-P 代理端口 -> 設置 JMeter 使用的代理主機的端口號
2.3、執行過程
命令:jmeter -n -t C:\Users\yzs\Desktop\Unione_performance.jmx -l report-result.jtl
不在jmeter安卓目錄執行腳本的前提是配置了jmeter的環境變量。
上述的命令有測試結果保存到D:\report中,在GUI模式下查看測試報告:
1、在測試計劃下,添加對應的測試報告元件,舉例增加了:查看結果樹、聚合報告
2、在“所有數據寫入一個文件”,選擇加載對應的結果文件
3、下面就會有對應的表格展示,具體見下圖
2.5、命令行傳遞變量值
設置線程組的線程數和循環次數。
注意格式:
${__P(threadNum)}
${__P(threadCount)}
其中P前面是兩個下劃線,()內就是變量名
執行時,在命令行中用-J參數給變量賦值即可:
jmeter -n -t C:\Users\yzs\Desktop\Unione_performance.jmx -J threadNum=10 -J threadCount=2 -l report-result.jtl
此次測試相當于:10個線程,循環2次,共計20個請求。
3、生成HTML報告
生成HTML報告有2種方式,一種是直接在命令行加上-o參數,另一種是已有jtl結果文件,運行命令生成報告
3.1、命令行直接生成報告
jmeter -n -t 【Jmx腳本位置】-l 【中間文件result.jtl位置】-e -o 【報告指定文件夾】
-e:測試結束后,生成測試報告
-o:指定測試報告的存放位置
注意:-o后面跟的文件夾一定是不存在的或者是空文件夾
3.2、已有jtl結果文件,運行命令生成報告
jmeter -g【已經存在的.jtl文件的路徑】-o 【用于存放html報告的目錄】
注意:經實操,windows系統上,以上2種方法都可以生成HTML測試報告,但是在Linux系統上第1種方法,沒有生成報告,只有第二種方法才可以(具體原因后面在慢慢找吧)
3.3、HTML報告注解
用瀏覽器打開index.html
報告詳解
Dashboard:(重點查看)
Test and Report informations:指的是測試和報告信息
APDEX(Application Performance Index):應用程序性能滿意度的標準
其中,
Requests Summary:請求的通過率(OK)與失敗率(KO),百分比顯示
Statistics:數據分析,基本將Summary Report和Aggrerate Report的結果合并(平均響應時間、TPS在此查看)
Errors:錯誤情況,依據不同的錯誤類型,將所有錯誤結果展示
關于Apdex的補充:
性能指數,Apdex(Application Performance Index)是一個國際通用標準,Apdex 是用戶對應用性能滿意度的量化值。它提供了一個統一的測量和報告用戶體驗的方法,把最終用戶的體驗和應用性能作為一個完整的指標進行統一度量。下圖表示為通用用戶滿意度區域,0代表沒有滿意用戶,1則代表所有用戶都滿意。實際業務系統開發過程中,1是團隊的追求目標。
若所有請求的Apdex值都接近1,說明用戶滿意度優秀,也從側面說明了服務器響應速度快。
通常而言,最低要求超過0.5,當然項目組可設定具體需求。
Charts:(輔助分析)
主要有如下特點:
(1)將測試過程中經常使用的數據,用圖表的形式展示,讓測試結果更加直觀
(2)每個圖表數據,有兩種展示形式
(3)支持請求樣例過濾顯示
(4)支持導出PNG圖片格式
Over Time Charts:
Throughput Charts:
Response Times Charts:
3.4、HTML報告的自定義配置
JMeter3.0開始在bin目錄新增了reportgenerator.properties文件保存了所有關于圖形化HTML報告生成模塊的默認配置,要變更配置,建議不要直接編輯該文件,而是推薦在user.properties中去配置和覆蓋。
3.4.1總體配置
總體配置都是以jmeter.reportgenerator.為前綴,如:jmeter.reportgenerator.overall_granularity=60000
# Change this parameter if you want to change the granularity of over time graphs.
jmeter.reportgenerator.overall_granularity=6000
Apdext = (Satisfied Count + Tolerating Count / 2) / Total Samples
另外,在jmeter.properties中,有關于集合報告中的三個百分位的默認值:
aggregate_rpt_pct1 : Defaults to 50
aggregate_rpt_pct2 : Defaults to 70
aggregate_rpt_pct3 : Defaults to 99
3.5、HTML報告的定制
JMeter的HTML報告生成時是使用了固定的模板,模板文件路徑為./bin/report-template。
進入該目錄可以看到報告的每個頁面都有一個.fmkr模板文件,包括index.html.fmkr和./content/pages路徑下的幾個文件。通過查看這些模板文件,就可以知道怎樣去進行報告的輕度定制,比如將一些文本修改得更易懂,或者修改為中文等
頁面的title
默認為"Apache JMeter Dashboard"
可以由reportgenerator.properties中的jmeter.reportgenerator.report_title來統一定義,這種方式就是所有頁面的title都使用同一個。
也可以直接修改對應的.fmkr文件中的title標簽中雙引號內的值,如<title>${reportTitle!"想要設置的title"}</title>,這中方式可以為每個頁面單獨定義title
圖表的名稱
當前版本下,各圖表的名稱是直接在模板文件中定義,要修改也是直接修改模板文件中對應元素的值即可
如要修改Transactions Per Second圖表的名稱,可以直接在./content/pages/Throughput.html.fmkr文件中修改,效果如下圖
html轉為pdf的組件有很多,但是還沒有哪一款能達到這個效果,其只要原因是wkhtmltopdf使用webkit網頁渲染引擎開發的用來將 html轉成 pdf的工具,可以跟多種腳本語言進行集成來轉換文檔。但是就使用簡便性來說還是itext等組件占據優勢,如果你要轉換格式有比較高的要求,那么wkhtmltopdf絕對是不二之選!
下載路徑
官網地址 wkhtmltopdf.org/
github地址 github.com/wkhtmltopdf…
使用方法
java調用demo
public class HtmlToPdfInterceptor extends Thread { private InputStream is; public HtmlToPdfInterceptor(InputStream is){ this.is = is; } public void run(){ try{ InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { System.out.println(line.toString()); //輸出內容 } }catch (IOException e){ e.printStackTrace(); } }}public class HtmlToPdf { //wkhtmltopdf在系統中的路徑 private static final String toPdfTool = "D:\wkhtmltopdf\bin\wkhtmltopdf.exe"; /** * html轉pdf * @param srcPath html路徑,可以是硬盤上的路徑,也可以是網絡路徑 * @param destPath pdf保存路徑 * @return 轉換成功返回true */ public static boolean convert(String srcPath, String destPath){ File file = new File(destPath); File parent = file.getParentFile(); //如果pdf保存路徑不存在,則創建路徑 if(!parent.exists()){ parent.mkdirs(); } StringBuilder cmd = new StringBuilder(); cmd.append(toPdfTool); cmd.append(" "); cmd.append(" --header-line");//頁眉下面的線 cmd.append(" --header-center 這里是頁眉這里是頁眉這里是頁眉這里是頁眉 ");//頁眉中間內容 //cmd.append(" --margin-top 30mm ");//設置頁面上邊距 (default 10mm) cmd.append(" --header-spacing 10 ");//(設置頁眉和內容的距離,默認0) cmd.append(srcPath); cmd.append(" "); cmd.append(destPath); boolean result = true; try{ Process proc = Runtime.getRuntime().exec(cmd.toString()); HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream()); HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream()); error.start(); output.start(); proc.waitFor(); }catch(Exception e){ result = false; e.printStackTrace(); } return result; } public static void main(String[] args) { HtmlToPdf.convert("https://my.oschina.net/papio/blog/835645", "d:/wkhtmltopdf.pdf"); }}復制代碼
wkhtmltopdf 參數詳解
wkhtmltopdf [OPTIONS]... <input file> [More input files] <output file>常規選項 --allow <path> 允許加載從指定的文件夾中的文件或文件(可重復) --book* 設置一會打印一本書的時候,通常設置的選項 --collate 打印多份副本時整理 --cookie <name> <value> 設置一個額外的cookie(可重復) --cookie-jar <path> 讀取和寫入的Cookie,并在提供的cookie jar文件 --copies <number> 復印打印成pdf文件數(默認為1) --cover* <url> 使用HTML文件作為封面。它會帶頁眉和頁腳的TOC之前插入 --custom-header <name> <value> 設置一個附加的HTTP頭(可重復) --debug-javascript 顯示的javascript調試輸出 --default-header* 添加一個缺省的頭部,與頁面的左邊的名稱,頁面數到右邊,例如: --header-left '[webpage]' --header-right '[page]/[toPage]' --header-line --disable-external-links* 禁止生成鏈接到遠程網頁 --disable-internal-links* 禁止使用本地鏈接 --disable-javascript 禁止讓網頁執行JavaScript --disable-pdf-compression* 禁止在PDF對象使用無損壓縮 --disable-smart-shrinking* 禁止使用WebKit的智能戰略收縮,使像素/ DPI比沒有不變 --disallow-local-file-access 禁止允許轉換的本地文件讀取其他本地文件,除非explecitily允許用 --allow --dpi <dpi> 顯式更改DPI(這對基于X11的系統沒有任何影響) --enable-plugins 啟用已安裝的插件(如Flash --encoding <encoding> 設置默認的文字編碼 --extended-help 顯示更廣泛的幫助,詳細介紹了不常見的命令開關 --forms* 打開HTML表單字段轉換為PDF表單域 --grayscale PDF格式將在灰階產生 --help Display help --htmldoc 輸出程序HTML幫助 --ignore-load-errors 忽略claimes加載過程中已經遇到了一個錯誤頁面 --lowquality 產生低品質的PDF/ PS。有用縮小結果文檔的空間 --manpage 輸出程序手冊頁 --margin-bottom <unitreal> 設置頁面下邊距 (default 10mm) --margin-left <unitreal> 將左邊頁邊距 (default 10mm) --margin-right <unitreal> 設置頁面右邊距 (default 10mm) --margin-top <unitreal> 設置頁面上邊距 (default 10mm) --minimum-font-size <int> 最小字體大小 (default 5) --no-background 不打印背景 --orientation <orientation> 設置方向為橫向或縱向 --page-height <unitreal> 頁面高度 (default unit millimeter) --page-offset* <offset> 設置起始頁碼 (default 1) --page-size <size> 設置紙張大小: A4, Letter, etc. --page-width <unitreal> 頁面寬度 (default unit millimeter) --password <password> HTTP驗證密碼 --post <name> <value> Add an additional post field (repeatable) --post-file <name> <path> Post an aditional file (repeatable) --print-media-type* 使用的打印介質類型,而不是屏幕 --proxy <proxy> 使用代理 --quiet Be less verbose --read-args-from-stdin 讀取標準輸入的命令行參數 --readme 輸出程序自述 --redirect-delay <msec> 等待幾毫秒為JS-重定向(default 200) --replace* <name> <value> 替換名稱,值的頁眉和頁腳(可重復) --stop-slow-scripts 停止運行緩慢的JavaScripts --title <text> 生成的PDF文件的標題(第一個文檔的標題使用,如果沒有指定) --toc* 插入的內容的表中的文件的開頭 --use-xserver* 使用X服務器(一些插件和其他的東西沒有X11可能無法正常工作) --user-style-sheet <url> 指定用戶的樣式表,加載在每一頁中 --username <username> HTTP認證的用戶名 --version 輸出版本信息退出 --zoom <float> 使用這個縮放因子 (default 1) 頁眉和頁腳選項--header-center* <text> (設置在中心位置的頁眉內容) --header-font-name* <name> (default Arial) (設置頁眉的字體名稱)--header-font-size* <size> (設置頁眉的字體大小)--header-html* <url> (添加一個HTML頁眉,后面是網址)--header-left* <text> (左對齊的頁眉文本)--header-line* (顯示一條線在頁眉下)--header-right* <text> (右對齊頁眉文本)--header-spacing* <real> (設置頁眉和內容的距離,默認0)--footer-center* <text> (設置在中心位置的頁腳內容) --footer-font-name* <name> (設置頁腳的字體名稱) --footer-font-size* <size> (設置頁腳的字體大小default 11)--footer-html* <url> (添加一個HTML頁腳,后面是網址)--footer-left* <text> (左對齊的頁腳文本)--footer-line* 顯示一條線在頁腳內容上)--footer-right* <text> (右對齊頁腳文本)--footer-spacing* <real> (設置頁腳和內容的距離)./wkhtmltopdf --footer-right '[page]/[topage]' http://www.baidu.com baidu.pdf./wkhtmltopdf --header-center '報表' --header-line --margin-top 2cm --header-line http://192.168.212.139/oma/ oma.pdf表內容選項中 --toc-depth* <level> Set the depth of the toc (default 3) --toc-disable-back-links* Do not link from section header to toc --toc-disable-links* Do not link from toc to sections --toc-font-name* <name> Set the font used for the toc (default Arial) --toc-header-font-name* <name> The font of the toc header (if unset use --toc-font-name) --toc-header-font-size* <size> The font size of the toc header (default 15) --toc-header-text* <text> The header text of the toc (default Table Of Contents) --toc-l1-font-size* <size> Set the font size on level 1 of the toc (default 12) --toc-l1-indentation* <num> Set indentation on level 1 of the toc (default 0) --toc-l2-font-size* <size> Set the font size on level 2 of the toc (default 10) --toc-l2-indentation* <num> Set indentation on level 2 of the toc (default 20) --toc-l3-font-size* <size> Set the font size on level 3 of the toc (default 8) --toc-l3-indentation* <num> Set indentation on level 3 of the toc (default 40) --toc-l4-font-size* <size> Set the font size on level 4 of the toc (default 6) --toc-l4-indentation* <num> Set indentation on level 4 of the toc (default 60) --toc-l5-font-size* <size> Set the font size on level 5 of the toc (default 4) --toc-l5-indentation* <num> Set indentation on level 5 of the toc (default 80) --toc-l6-font-size* <size> Set the font size on level 6 of the toc (default 2) --toc-l6-indentation* <num> Set indentation on level 6 of the toc (default 100) --toc-l7-font-size* <size> Set the font size on level 7 of the toc (default 0) --toc-l7-indentation* <num> Set indentation on level 7 of the toc (default 120) --toc-no-dots* Do not use dots, in the toc輪廓選項 --dump-outline <file> 轉儲目錄到一個文件 --outline 顯示目錄(文章中h1,h2來定) --outline-depth <level> 設置目錄的深度(默認為4)頁腳和頁眉 * [page] 由當前正在打印的頁的數目代替 * [frompage] 由要打印的第一頁的數量取代 * [topage] 由最后一頁要打印的數量取代 * [webpage] 通過正在打印的頁面的URL替換 * [section] 由當前節的名稱替換 * [subsection] 由當前小節的名稱替換 * [date] 由當前日期系統的本地格式取代 * [time] 由當前時間,系統的本地格式取代
作者:曹元
鏈接:https://juejin.im/post/6856547881873047559
來源:掘金
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
下是使用 C# 創建 HTML 文件的代碼示例:
using System;
using System.IO;
namespace CreateHTMLFile
{
class Program
{
static void Main(string[] args)
{
// 設置 HTML 文件路徑及文件名
string filePath = @"C:\Temp\example.html";
// 創建一個新的 HTML 文件
using (StreamWriter writer = new StreamWriter(filePath))
{
// 寫入 HTML 標記
writer.WriteLine("<html>");
writer.WriteLine("<head><title>Example HTML File</title></head>");
writer.WriteLine("<body>");
// 寫入主體內容
writer.WriteLine("<h1>Hello, World!</h1>");
writer.WriteLine("<p>This is an example HTML file created using C#.</p>");
// 寫入 HTML 結束標記
writer.WriteLine("</body>");
writer.WriteLine("</html>");
}
// 輸出成功消息
Console.WriteLine("HTML 文件已創建:{0}", filePath);
}
}
}
此代碼將創建一個名為 “example.html” 的 HTML 文件,并在其中添加一些基本的元素。你可以根據需要修改或擴展它,以滿足你的具體需求。注意,要創建 HTML 文件,你需要使用 System.IO 命名空間中的 StreamWriter 類。該類允許你向文件寫入文本數據。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。