分享到plurk 分享到twitter 分享到facebook

版本 c1ba805605f8773751fa5df9ae4f33076f6cc1a2

embedded/2016q1h3

Changes from c1ba805605f8773751fa5df9ae4f33076f6cc1a2 to 01bb1020630e93b39bf9b401a9a9ac9312d6f980

---
title: 2016q1 Homework #3
toc: no
...

預期目標
------------
- 學習多執行緒程式開發
- 應用物件導向程式開發方法
- 透過 Doxygen 自動建立技術文件
- 培養專業程式開發風格

預先安裝的套件
------------
```
$ sudo apt-get install apache2-utils check
$ sudo apt-get install doxygen graphviz
```

Server Framework in modern C
-----------------------------
* [server-framework](https://github.com/embedded2016/server-framework): 用 C99 撰寫、具備物件導向程式設計風格的伺服器開發框架
* 由三部份組成:
    - [async](https://github.com/embedded2016/server-framework/blob/master/async.h)
    - [reactor](https://github.com/embedded2016/server-framework/blob/master/reactor.h)
    - [protocol-server](https://github.com/embedded2016/server-framework/blob/master/protocol-server.h)
* 取得原始碼並編譯:
```
$ git clone https://github.com/embedded2016/server-framework.git
$ cd server-framework
$ make
```
* 測試 [async](https://github.com/embedded2016/server-framework/blob/master/async.h)
```
$ ./test-async
```
    - 預期將看到類似以下輸出:
```
wrote task 8190
wrote task 8191
Hi! 16377
Hi! 16378
# signal finish at (711.426132) ms
# elapsed time: (711.728594) ms
```
* 測試 [reactor](https://github.com/embedded2016/server-framework/blob/master/reactor.h)
```
$ ./test-reactor
```
    - 預期將看到以下訊息:
```
Serving HTTP on 127.0.0.1 port 8080
now open up the URL http://localhost:8080 on your browser
```
    - 依據指示,在網頁瀏覽器裡頭打開網址 `http://localhost:8080`,將會看到 "Hello World!" 字樣
    - 回到原本執行 `./test-reactor` 的終端機視窗,按下 `Ctrl-C` 以結束程式
* 測試 [protocol-server](https://github.com/embedded2016/server-framework/blob/master/protocol-server.h)
```
$ ./test-protocol-server
```
    - 預期將看到類似以下訊息:
```
(pid 4130 : 8 threads) Listening on port 8080 (max sockets: 65536, ~5 used)
```
    - 這時再次透過網頁瀏覽器開啟網址 `http://localhost:8080`,可以發現原本終端機印出類似以下訊息:
```
A connection was accepted on socket #7.
A connection was accepted on socket #8.
Socket #7 is now disconnected.
Socket #8 is now disconnected.
```
    - 重複上述動作幾次,又會看到新的訊息,指出 socket 編號的更動

測試網頁伺服器的效能
-----------------
* 確保之前的測試程式均已關閉,而且 port 8080 沒被佔用
    - 可透過 `$ pkill -9 httpd` 來強制關閉伺服器
* 執行 ``$ ./httpd``,預期將看到類似以下輸出:
```
(pid 4223 : 1 threads) Listening on port 8080 (max sockets: 65536, ~5 used)
Clients: 0
# Total Clients: 0
```
* 不要關閉執行 `httpd` 的終端機視窗,為了後續說明方面,我們稱為這個終端機畫面為「伺服器」
* 開啟新的終端機視窗,並執行以下指令:
```
$ ab -c 32 -n 100 http://localhost:8080/
```
    - 預期將看到以下輸出,請抱持耐心,等待且不要執行其他程式
```
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
```
    - 等待一陣子之後,會看到類似以下輸出:
```
Concurrency Level:      32
Time taken for tests:   15.621 seconds
Complete requests:      100
Failed requests:        6
   (Connect: 0, Receive: 0, Length: 6, Exceptions: 0)
Total transferred:      9900 bytes
HTML transferred:       1300 bytes
Requests per second:    6.40 [#/sec] (mean)
Time per request:       4998.873 [ms] (mean)
Time per request:       156.215 [ms] (mean, across all concurrent requests)
Transfer rate:          0.62 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   0.7      2       3
Processing:  3619 3877 177.2   3996    4001
Waiting:        0    1   0.5      0       3
Total:       3621 3879 177.2   3998    4001
```