# Faceboo"C" - 正確性驗證 contributed by < `dalaoqi` > 在開始量化分析效能前,需要確保 faceboo"C" 能夠正確的去執行相關的功能。這裡會使用 [cURL](https://curl.se/) 來做自動化測試,模擬用戶端的行為。 ## cURL 全名為 CommandLine URL ,顧名思義就是可以在 Command Line 環境上,利用 URL 及 HTTP 協定進行資訊間傳遞的工具,也就是說可以透過 cURL 指令來進行 request 及 response 的操作,其優點在於: * 可以支援檔案上傳和下載 * 只需要短短幾行命令就可以做到各種網路相關的操作 * 免費取得且[開放原始碼](https://github.com/curl/curl) ### cURL 命令 主要格式為 `curl [options] [URL]` 例如在 Command Line 輸入 Faceboo"C" `curl 140.116.80.6:8080` 會得到 response body 。 ![](https://i.imgur.com/mM3fYnR.png) ## 自動化測試 ### Faceboo"C" 功能目前大致上可分為這幾種: * signup * login * logout * post * like * unlike * dashboard ### 註冊 ![](https://i.imgur.com/GSNKpiK.png) 目前資料庫中有六位已註冊的使用者 先進到 `140.116.82.6:8080/signup/` 註冊頁面,可以看到一個 HTML 表單 ![](https://i.imgur.com/g1X2ikx.png) 必須要先知道每個欄位的 `name` 屬性後才可以下 curl 指令。若瀏覽器為 Google Chrome 可以藉由 `F12 開發者工具` 或是對表格欄位按滑鼠右鍵 `檢查` 即可找到。 所以對應的欄位 `name` 屬性如下: * Name: "name" * Email: "email" * Username: "username" * Password: "password" * Confirm password: "confirm-password" 下指令 ``` curl -X POST 140.116.82.6:8080/signup/ -d "name=12345678&email=test12312348@gmail.com&username=test12312348&password=0987654321&confirm-password=0987654321" ``` 在 `-X` 後方加上 HTTP method 可以更改 curl 的方法關鍵字。 `-d, --data` 代表我要上傳上去的資料,資料間用 `&` 串接。 ![](https://i.imgur.com/NuAM73l.png) 資料庫中新增一位註冊成功的使用者。 ### 登入 & 發文 [Cookie](https://zh.wikipedia.org/wiki/Cookie) 是伺服器端透過瀏覽器,將使用電腦或智慧型手機存取網站的使用者相關資訊加以紀錄與辨識,並進行暫存的機制。好處是下一次瀏覽相同的來源的網站時, `Cookie` 會透過瀏覽器遞交給伺服器端,網站可透過此 `Cookie` 辨別存取使用者的身分。 在登入的同時將使用者的 `Cookie` 存在本地端,方便之後的操作。 利用 `curl` 中的語法 `-c` 將 `Cookie` 存在本地端。 ``` curl -c cookie 140.116.82.6:8080/login/ -d "username=test12312348&password=0987654321" ``` 輸入下列指令做測試,`curl` 指令 `-b` 表示讀取剛剛存下來的 `Cookie` 檔。 ``` curl -b cookie 140.116.82.6:8080/post/ -d "post=test" ``` 成功利用 `curl` 指令發文,但時間不正確。 ![](https://i.imgur.com/lzvX24H.png) ### 更改系統時間 輸入 `timedatectl` 指令檢查時區是否設定正確 ``` Local time: Tue 2020-12-29 14:51:25 UTC Universal time: Tue 2020-12-29 14:51:25 UTC RTC time: n/a Time zone: Etc/UTC (UTC, +0000) System clock synchronized: yes NTP service: active RTC in local TZ: no ``` 輸入指令 `timedatectl set-timezone Asia/Taipei` 更改時區後,再輸入 `timedatectl` 檢查 ``` Local time: Tue 2020-12-29 22:54:31 CST Universal time: Tue 2020-12-29 14:54:31 UTC RTC time: n/a Time zone: Asia/Taipei (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: no ``` 重新發文測試 ![](https://i.imgur.com/yDfT0Zc.png) 更改程式碼 `main.c` ``` c=225 t = post->createdAt; - strftime(sbuff, 128, "%c GMT", gmtime(&t)); + info = gmtime(&t); + info->tm_hour = info->tm_hour + 8; + strftime(sbuff, 128, "%c GMT+8", info); bsLCat(&res, sbuff); bsLCat(&res, "</li>"); ``` ![](https://i.imgur.com/LyDSnJP.png) ### 性能測試 Apache Bench 又被簡稱為 ab 是由 Apache 開發的其中一個開源測試工具 主要用來測試 server (網站或 API) 的執行效能 並藉此查看執行後 server 的性能測試結果 #### 安裝 Apache Bench ``` $ apt-get install apache2-utils ``` #### 使用 Apache Bench 最簡單方法是運行 `ab <url>` ``` ab http://140.116.82.6:8080/ ``` 執行結果 ``` This is ApacheBench, Version 2.3 <$Revision: 1843412 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 140.116.82.6 (be patient).....done Server Software: Server Hostname: 140.116.82.6 Server Port: 8080 Document Path: / Document Length: 3658 bytes Concurrency Level: 1 Time taken for tests: 0.001 seconds Complete requests: 1 Failed requests: 0 Total transferred: 3677 bytes HTML transferred: 3658 bytes Requests per second: 1264.22 [#/sec] (mean) Time per request: 0.791 [ms] (mean) Time per request: 0.791 [ms] (mean, across all concurrent requests) Transfer rate: 4539.60 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.0 0 0 Processing: 1 1 0.0 1 1 Waiting: 0 0 0.0 0 0 Total: 1 1 0.0 1 1 ``` 上述的指令只會送出一個 http request 給 server,我們可以更改 http request 數量以及 Concurrency 大小 (一次執行多請求的數量),例如: ``` ab -n 100 -c 100 http://140.116.82.6:8080/ ``` 表示在短時間內送出 100 次請求,模擬 100 人同時進行。 >-n:測試執行的總請求數,默認值為1 -c:一次執行多請求的數量,也就是模擬連線使用者數,默認值為1 ``` This is ApacheBench, Version 2.3 <$Revision: 1843412 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 140.116.82.6 (be patient).....done Server Software: Server Hostname: 140.116.82.6 Server Port: 8080 Document Path: / Document Length: 3658 bytes Concurrency Level: 100 Time taken for tests: 0.045 seconds Complete requests: 100 Failed requests: 0 Total transferred: 367700 bytes HTML transferred: 365800 bytes Requests per second: 2245.68 [#/sec] (mean) Time per request: 44.530 [ms] (mean) Time per request: 0.445 [ms] (mean, across all concurrent requests) Transfer rate: 8063.82 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 8 1.8 8 11 Processing: 5 15 7.6 15 29 Waiting: 0 15 7.8 15 29 Total: 15 23 6.0 23 34 Percentage of the requests served within a certain time (ms) 50% 23 66% 26 75% 28 80% 29 90% 32 95% 33 98% 33 99% 34 100% 34 (longest request) ``` request 數量1000 ``` ab -c 100 -n 1000 http://140.116.82.6:8080/ ``` ``` This is ApacheBench, Version 2.3 <$Revision: 1843412 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 140.116.82.6 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: Server Hostname: 140.116.82.6 Server Port: 8080 Document Path: / Document Length: 3658 bytes Concurrency Level: 100 Time taken for tests: 0.367 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 3677000 bytes HTML transferred: 3658000 bytes Requests per second: 2722.54 [#/sec] (mean) Time per request: 36.730 [ms] (mean) Time per request: 0.367 [ms] (mean, across all concurrent requests) Transfer rate: 9776.15 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 1 2.3 0 10 Processing: 0 2 5.1 1 29 Waiting: 0 2 5.1 1 29 Total: 1 3 7.0 1 34 Percentage of the requests served within a certain time (ms) 50% 1 66% 1 75% 1 80% 1 90% 16 95% 23 98% 30 99% 32 100% 34 (longest request) ``` request 數量10000,設定 timeout 參數 `-s 120` 及 `-r` -r :Don't exit on socket receive errors ``` ab -c 100 -n 10000 -r -s 120 http://140.116.82.6:8080/ ``` ``` This is ApacheBench, Version 2.3 <$Revision: 1843412 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 140.116.82.6 (be patient) Completed 1000 requests Completed 2000 requests Completed 3000 requests Completed 4000 requests Completed 5000 requests Completed 6000 requests Completed 7000 requests Completed 8000 requests Completed 9000 requests Completed 10000 requests Finished 10000 requests Server Software: Server Hostname: 140.116.82.6 Server Port: 8080 Document Path: / Document Length: 3658 bytes Concurrency Level: 100 Time taken for tests: 108.114 seconds Complete requests: 10000 Failed requests: 33 (Connect: 0, Receive: 11, Length: 11, Exceptions: 11) Total transferred: 36729553 bytes HTML transferred: 36539762 bytes Requests per second: 92.50 [#/sec] (mean) Time per request: 1081.139 [ms] (mean) Time per request: 10.811 [ms] (mean, across all concurrent requests) Transfer rate: 331.77 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 14 158.5 0 3048 Processing: 0 169 3763.1 1 107071 Waiting: 0 51 1255.1 1 53826 Total: 1 183 3811.4 1 108086 Percentage of the requests served within a certain time (ms) 50% 1 66% 1 75% 1 80% 1 90% 1 95% 2 98% 18 99% 1006 100% 108086 (longest request) ``` 以下分別為總花費時間及每秒 Facebooc 能處理的 request 數量,發現當 request 的數量增加到 10000 時,很明顯的,處理時間變長且每秒能處理的 request 數量變小。 ![](https://i.imgur.com/H0oaGsQ.png) ![](https://i.imgur.com/pugtJGZ.png) ### 腳本測試 scenario: 使用者進入頁面,登入,發文,按讚,取消按讚 `facebooc_benchmark.sh` ```=shell #!/bin/bash function test_unit { for (( i=0; i<$1; i=i+1 )) do curl -s http://localhost:8080/ \ -s --next -c cookie http://localhost:8080/login/ -d "username=test12312348&password=0987654321" \ -s --next -b cookie http://localhost:8080/dashboard/ \ -s --next -b cookie --location --data "post=Facebooc Benchmark" http://localhost:8080/post/ \ -s --next -b cookie --location http://localhost:8080/like/1/ \ -s --next -b cookie --location http://localhost:8080/unlike/1/ done } if [ -z "$1" ] then echo "No arg1, please input times of per concurrence" exit 1 fi if [ -z "$2" ] then echo "No arg2, please input the number of concurrence." exit 1 fi times=$1 concurrent=$2 TEST_COMMAND="test_unit $times " STARTTIME=$(date +%s%N) while [ $concurrent -gt 0 ] do $TEST_COMMAND"$concurrent" & concurrent=$(($concurrent-1)) done wait ENDTIME=$(date +%s%N) echo "It takes $(( (($ENDTIME - $STARTTIME) / 1000000) / ($1 * $2) )) milliseconds per scenario." ``` 使用方式 `bash facebooc_benchmark.sh (每個同步要測試的情境次數) (同步的個數)` `bash facebooc_benchmark.sh 50 10` 測試結果 `It takes 105 milliseconds per scenario.` ## 參考文件 https://miahsuwork.medium.com/curl-%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C%E8%88%87%E6%8C%87%E4%BB%A4-%E6%90%AD%E9%85%8D-lidemy-http-challenge-%E5%AF%A6%E4%BD%9C-39b79511eb9e https://blog.techbridge.cc/2019/02/01/linux-curl-command-tutorial/ https://maidot.blogspot.com/2017/04/curl-cookie.html http://linux.vbird.org/linux_basic/0340bashshell-scripts.php https://www.opencli.com/linux/ubuntu-16-04-change-timezone-setting https://ithelp.ithome.com.tw/articles/10233147 https://paper.dropbox.com/doc/Web-server-with-cgi-on-linkit-smart-7688-sU8wcCn47CmrGsY3gYob0