Tsung-Han Ho
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 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

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully