使用 PHP 實現的的內網穿透工具 “Spike”

slince發表於2019-02-16

Spike https://github.com/slince/spike

之前由於要與一個同事遠端協作開發一款 app 需要用到內網穿透服務,在網上找到了 frp 與 ngrok ;後來我在想能不能用 php 也寫出來一個這樣的服務軟體?大家都知道 php 多程式多執行緒不夠友好,在 window 上還不支援;寫服務確實很吃力;不過幸運的是有ReactPHP的存在,關於 ReactPHP 不做贅述有興趣的同學可以自行百度。

基於 ReactPHP 的 IO 多路複用,使得 Spike 並沒有比 Frp 效能差太多;下面是我簡單做的一個 benchmark,基於 apache ab 檢驗 http 隧道的服務效能;客戶端與服務端都搭在本地,代理同事電腦上的 http 服務。不是特別符合應用場景,大家簡單看一下。

從下面的資訊可以看出 Spike 效能似乎是稍微好點的,不過這個地方有點不公平,我在做 spike 的測試時只開啟了服務端的日誌,客戶端的日誌是關閉的;而 FRP 的兩端日誌都是開啟的;我不知道怎麼關 frp 的日誌;

在這裡簡單提一點由於 Spike 的日誌 IO 是同步的所以日誌的讀寫會耗掉部分效能,提升日誌等級減少日誌寫入可以提升不少的效能;

這個專案是我比較上心的一個作品,算是證明了一點,php 除了可以做網站也可以做服務,並且也沒有太差。 最後再次附上專案地址: https://github.com/slince/spike 歡迎 star,歡迎 fork

Spike:

Concurrency Level:      10
Time taken for tests:   37.727 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      2569900 bytes
HTML transferred:       2514600 bytes
Requests per second:    2.65 [#/sec] (mean)
Time per request:       3772.747 [ms] (mean)
Time per request:       377.275 [ms] (mean, across all concurrent requests)
Transfer rate:          66.52 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       3
Processing:   533 3602 591.9   3714    4096
Waiting:      516 3587 592.3   3701    4076
Total:        534 3602 591.9   3715    4097

Percentage of the requests served within a certain time (ms)
  50%   3715
  66%   3791
  75%   3822
  80%   3844
  90%   3970
  95%   4015
  98%   4053
  99%   4097
 100%   4097 (longest request)

Frp:

Concurrency Level:      10
Time taken for tests:   38.230 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      2569900 bytes
HTML transferred:       2514600 bytes
Requests per second:    2.62 [#/sec] (mean)
Time per request:       3823.045 [ms] (mean)
Time per request:       382.304 [ms] (mean, across all concurrent requests)
Transfer rate:          65.65 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:   379 3650 644.4   3809    4140
Waiting:      360 3633 645.5   3789    4124
Total:        380 3650 644.4   3809    4140

Percentage of the requests served within a certain time (ms)
  50%   3809
  66%   3847
  75%   3909
  80%   3923
  90%   4026
  95%   4053
  98%   4129
  99%   4140
 100%   4140 (longest request)

相關文章