
我正在虛擬雲端託管 (CityCloud) 上運行帶有 LAMP 的 Ubuntu 14 伺服器。
伺服器通常表現良好,回應時間<0.5s。然而,問題出現在高峰時段,主要是週一至週三/週四,反應時間約為 7-8 秒或更長。透過傳遞給工作者 MPM 和 php-fpm,這些時間或多或少減少了一半,感謝 stackoverflow 上的 @drew010。
現在,我想看看是否可以進行最後的調整,以消除繁忙時段剩餘的 3 秒平均載入時間。
根據 Google Analytics 的數據:該網站每月的頁面瀏覽量約為 500 萬次,高峰時段每小時的頁面瀏覽量約為 1200-13,000 萬次。
這是我的設定:
- Ubuntu 14.04.3 LTS
- PHP 版本 5.5.9、FPM/FastCGI
- 4個CPU核心
- 8記憶體
- 限制為每月 1 Gbps 頻寬和 1 TB 傳輸
- 尖峰時段 CPU 使用率 < 6%
- 使用記憶體 < 2GB
- 尖峰時段約 2M txbps
- 靜態內容由單獨的子網域(同一台電腦)提供,其過期標頭為「訪問加 1 個月」。
- MySQL運作在同一台機器上。
- MySQL 查詢經測量速度很快,不是問題。
顯然,4 核心/8 RAM 過多了。如果性能良好的話,我的意圖是回退到最多 2 核/4 RAM。
這是高峰時段的 netstat 輸出:
$ netstat -ant | awk '{print $6}' |排序| uniq-c|排序-n 3 結束 5 聽 8 FIN_等待1 69 最後確認 80 同步接收 117 成立 147 FIN_等待2 第1028章
一些伺服器資訊:
在檔案中:/etc/apache2/mods-enabled/mpm_worker.conf 13: 啟動伺服器 2 14:最小備用線程 25 15:最大備用線數 75 16:線程限制 64 17:每個子執行緒數 25 18:最大請求工作者數 150
一些內核選項值:
/proc/sys/net/core/somaxconn: 128 /proc/sys/net/core/netdev_max_backlog: 1000 /proc/sys/net/ipv4/tcp_max_syn_backlog:256 /proc/sys/net/ipv4/ip_local_port_range: 32768 61000
MySQL「顯示全域狀態」:
已中止_客戶端 7 中止_連接 14 Binlog_cache_disk_use 0 Binlog_cache_use 0 binlog_stmt_cache_disk_use 0 Binlog_stmt_cache_use 0 收到的位元組數 7324492238 已發送位元組數 33143229342 com_admin_commands 0 com_assign_to_keycache 0 com_alter_db 0 com_alter_db_upgrade 0 com_alter_event 0 com_alter_function 0 com_alter_procedure 0 com_alter_server 0 com_alter_table 2 com_alter_表空間 0 com_分析 0 com_開始 0 com_binlog 0 com_call_procedure 0 com_change_db 1277679 com_change_master 0 com_檢查 0 com_checksum 0 com_commit 0 com_create_db 0 com_create_event 0 com_建立_函數 0 com_create_index 0 com_create_procedure 0 com_建立_伺服器 0 com_建立_表 0 com_create_trigger 0 com_create_udf 0 com_建立_使用者 0 com_create_view 0 com_dealloc_sql 0 com_刪除 19 com_delete_multi 0 com_do 0 com_drop_db 0 com_drop_事件 0 com_drop_function 0 com_drop_index 0 com_drop_procedure 0 com_drop_伺服器 0 com_drop_表 0 com_drop_trigger 0 com_drop_用戶 0 com_drop_view 0 com_empty_query 0 com_execute_sql 0 com_flush 0 com_grant 0 com_ha_close 0 com_ha_open 0 com_ha_read 0 com_幫助 0 com_插入1301 com_插入_選擇 0 com_install_plugin 0 com_kill 0 com_負載0 com_lock_tables 18 com_優化 1 com_preload_keys 0 com_prepare_sql 0 com_purge 0 com_purge_before_date 0 com_release_savepoint 0 com_rename_table 0 com_rename_user 0 com_修復 0 com_替換 7 com_replace_select 0 com_重置0 com_resignal 0 com_撤銷 0 com_revoke_all 0 com_rollback 0 com_rollback_to_savepoint 0 com_保存點 0 com_select 8939751 com_set_option 2555998 通訊訊號 0 com_show_authors 0 com_show_binlog_events 0 com_show_binlogs 94 com_show_charsets 0 com_show_collations 0 com_show_contributors 0 com_show_create_db 18 com_show_create_event 0 com_show_create_func 0 com_show_create_proc 0 com_show_create_table 356 com_show_create_trigger 0 com_show_databases 28 com_show_engine_logs 0 com_show_engine_mutex 0 com_show_engine_status 0 com_show_events 5 com_show_errors 0 com_show_fields 381 com_show_function_status 5 com_show_grants 5 com_show_keys 40 com_show_master_status 43 com_show_open_tables 0 com_show_plugins 0 com_show_privileges 0 com_show_procedure_status 5 com_show_processlist 1099 com_show_profile 0 com_show_profiles 0 com_show_relaylog_events 0 com_show_slave_hosts 0 com_show_slave_status 1118 com_show_status 1120 com_show_storage_engines 0 com_show_table_status 366 com_show_tables 77 com_show_triggers 324 com_show_variables 1135 com_show_warnings 0 com_slave_start 0 com_slave_stop 0 com_stmt_close 0 com_stmt_執行 0 com_stmt_fetch 0 com_stmt_prepare 0 com_stmt_reprepare 0 com_stmt_reset 0 com_stmt_send_long_data 0 com_截斷 0 com_uninstall_plugin 0 com_unlock_tables 18 com_update 41612 com_update_multi 0 com_xa_commit 0 com_xa_end 0 com_xa_prepare 0 com_xa_recover 0 com_xa_rollback 0 com_xa_start 0 壓縮關閉 連接 1277451 建立_tmp_disk_表 731 建立的臨時檔案 9 建立_tmp_表3602 延遲錯誤 0 延遲插入線程 0 延遲寫入 0 刷新命令 1 處理程序_提交 284 處理程序_刪除 19 處理程序_發現 0 處理程序_準備 0 第 485 章 處理程序_讀取_密鑰 7814753 處理程序_讀取_最後 48790 處理程序_讀取_下一個 55898210 Handler_read_prev 457247 處理程序_讀_rnd 223213 處理程序_read_rnd_next 4231962515 處理程序回滾 0 處理程序保存點 0 處理程序_保存點_回滾 0 處理程序_更新 40888 處理程序_寫入377375 Innodb_buffer_pool_pages_data 202 Innodb_buffer_pool_bytes_data 3309568 Innodb_buffer_pool_pages_dirty 0 Innodb_buffer_pool_bytes_dirty 0 Innodb_buffer_pool_pages_flushed 37 Innodb_buffer_pool_pages_free 7989 Innodb_buffer_pool_pages_misc 0 Innodb_buffer_pool_pages_total 8191 Innodb_buffer_pool_read_ahead_rnd 0 Innodb_buffer_pool_read_ahead 0 Innodb_buffer_pool_read_ahead_evicted 0 Innodb_buffer_pool_read_requests 2817 Innodb_buffer_pool_reads 196 Innodb_buffer_pool_wait_free 0 Innodb_buffer_pool_write_requests 86 Innodb_data_fsyncs 39 Innodb_data_pending_fsyncs 0 Innodb_data_pending_reads 0 Innodb_data_pending_writes 0 Innodb_data_read 5394432 Innodb_data_reads 206 Innodb_data_writes 67 Innodb_data_writing 1235456 Innodb_dblwr_pages_writing 37 Innodb_dblwr_writes 9 Innodb_have_atomic_builtins 開啟 Innodb_log_waits 0 Innodb_log_write_requests 35 Innodb_log_writes 10 Innodb_os_log_fsyncs 21 Innodb_os_log_pending_fsyncs 0 Innodb_os_log_pending_writes 0 Innodb_os_log_writing 17408 Innodb_page_size 16384 Innodb_pages_created 7 Innodb_pages_read 195 Innodb_pages_writing 37 Innodb_row_lock_current_waits 0 Innodb_row_lock_time 0 Innodb_row_lock_time_avg 0 Innodb_row_lock_time_max 0 Innodb_row_lock_waits 0 Innodb_rows_deleted 0 Innodb_rows_inserted 0 Innodb_rows_read 59 Innodb_rows_updated 7 Innodb_truncated_status_writes 0 Key_blocks_not_flushed 0 Key_blocks_unused 10770 Key_blocks_used 2626 Key_read_requests 167086090 按鍵讀取次數 2430 Key_write_requests 376494 Key_writes 303134 最後查詢成本 0 最大已使用連線數 8 Not_flushed_delayed_rows 0 開啟文件 141 開放流 0 開啟表定義 73 開啟_表 137 開啟的文件數 5062 開啟表定義 80 打開的表 150 Performance_schema_cond_classes_lost 0 Performance_schema_cond_instances_lost 0 Performance_schema_file_classes_lost 0 Performance_schema_file_handles_lost 0 Performance_schema_file_instances_lost 0 Performance_schema_locker_lost 0 Performance_schema_mutex_classes_lost 0 Performance_schema_mutex_instances_lost 0 Performance_schema_rwlock_classes_lost 0 Performance_schema_rwlock_instances_lost 0 Performance_schema_table_handles_lost 0 Performance_schema_table_instances_lost 0 Performance_schema_thread_classes_lost 0 Performance_schema_thread_instances_lost 0 準備好的stmt_count 0 Qcache_free_blocks 2242 Qcache_free_內存 7441152 Qcache_hits 68078488 Qcache_插入8868017 Qcache_lowmem_prunes 4451717 Qcache_not_cached 71091 Qcache_queries_in_cache 6797 Qcache_total_blocks 16056 查詢82178544 提問82178543 Rpl_status AUTH_MASTER 選擇_完整_加入 0 選擇_full_range_join 0 選擇範圍2041371 選擇範圍檢查 0 選擇掃描1151738 Slave_heartbeat_period 0 Slave_open_temp_tables 0 Slave_received_heartbeats 0 Slave_retried_transactions 0 Slave_running關閉 Slow_launch_threads 0 慢速查詢 0 排序合併通道 5 排序範圍 878229 排序_行 236531 排序_掃描 869 ssl_accept_renegotiates 0 ssl_接受 0 ssl_callback_cache_hits 0 SSL_密碼 SSL_密碼_列表 ssl_client_connects 0 ssl_connect_renegotiates 0 ssl_ctx_verify_深度 0 ssl_ctx_verify_mode 0 ssl_default_timeout 0 ssl_finished_accepts 0 ssl_finished_connects 0 ssl_session_cache_hits 0 ssl_session_cache_misses 0 ssl_session_cache_mode 無 ssl_session_cache_overflows 0 ssl_session_cache_size 0 ssl_session_cache_timeouts 0 ssl_sessions_reused 0 ssl_used_session_cache_entries 0 ssl_verify_深度 0 ssl_verify_mode 0 SSL_版本 表_鎖_立即 9116984 表_鎖_等待 178 Tc_log_max_pages_used 0 Tc_log_page_size 0 tc_log_page_waits 0 線程緩存 6 線程_連接 2 線程_創建 8 執行緒運行 1 正常運轉時間 423398 Uptime_since_flush_status 423398
iostat
尖峰時段執行:
$ iostat -xdk 1 設備:rrqm/s wrqm/sr/sw/s rkB/s wkB/s avgrq-sz avgqu-sz 等待 r_await w_await svctm %util 0.00 0.00 0.00 1.00 0.00 4.00 8.00 0.00 4.00 0.00 4.00 4.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 瓦達 0.00 0.00 0.00 2.00 0.00 8.00 8.00 0.00 2.00 0.00 2.00 2.00 0.40 瓦達 0.00 0.00 0.00 17.00 0.00 96.00 11.29 0.00 0.00 0.00 0.00 0.00 0.00 瓦達 0.00 34.00 0.00 3.00 0.00 152.00 101.33 0.00 1.33 0.00 1.33 1.33 0.40 0.00 0.00 0.00 1.00 0.00 4.00 8.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 12.00 24.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 瓦達 0.00 0.00 0.00 20.00 0.00 152.00 15.20 0.02 1.20 0.00 1.20 0.60 1.20 VDA 0.00 44.00 0.00 2.00 0.00 188.00 188.00 0.00 2.00 0.00 2.00 2.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 瓦達 0.00 0.00 0.00 2.00 0.00 8.00 8.00 0.00 2.00 0.00 2.00 2.00 0.40 0.00 0.00 0.00 1.00 0.00 4.00 8.00 0.00 4.00 0.00 4.00 4.00 0.40 瓦達 0.00 74.00 0.00 146.00 0.00 908.00 12.44 0.30 2.08 0.00 2.08 0.05 0.80 瓦達 0.00 46.00 0.00 3.00 0.00 200.00 133.33 0.00 1.33 0.00 1.33 1.33 0.40 瓦達 0.00 0.00 0.00 4.00 0.00 16.00 8.00 0.00 1.00 0.00 1.00 1.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 vda 0.00 0.00 0.00 20.00 0.00 232.00 23.20 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 VDA 0.00 41.00 0.00 2.00 0.00 176.00 176.00 0.00 2.00 0.00 2.00 2.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 瓦達 0.00 0.00 0.00 17.82 0.00 118.81 13.33 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 4.00 8.00 0.00 0.00 0.00 0.00 0.00 0.00 vda 0.00 42.00 0.00 4.00 0.00 188.00 94.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 4.00 8.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 12.00 0.00 60.00 10.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 VDA 0.00 39.00 0.00 2.00 0.00 168.00 168.00 0.00 2.00 0.00 2.00 2.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 16.00 0.00 76.00 9.50 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 VDA 0.00 32.00 0.00 2.00 0.00 140.00 140.00 0.00 2.00 0.00 2.00 2.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 vda 0.00 0.00 0.00 18.00 0.00 100.00 11.11 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 4.00 8.00 0.00 0.00 0.00 0.00 0.00 0.00 VDA 0.00 30.00 0.00 2.00 0.00 132.00 132.00 0.00 2.00 0.00 2.00 2.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 12.00 0.00 48.00 8.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 瓦達 0.00 37.00 0.00 3.00 0.00 164.00 109.33 0.01 2.67 0.00 2.67 2.67 0.80 0.00 0.00 0.00 1.00 0.00 4.00 8.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 瓦達 0.00 48.00 0.00 130.00 0.00 744.00 11.45 0.24 1.88 0.00 1.88 0.06 0.80 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 VDA 0.00 35.00 0.00 4.00 0.00 160.00 80.00 0.00 1.00 0.00 1.00 1.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 4.00 8.00 0.00 0.00 0.00 0.00 0.00 0.00 vda 0.00 0.00 0.00 19.00 0.00 172.00 18.11 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 4.00 8.00 0.00 4.00 0.00 4.00 4.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 VDA 0.00 39.00 0.00 2.00 0.00 168.00 168.00 0.00 2.00 0.00 2.00 2.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 vda 0.00 0.00 0.00 25.00 0.00 184.00 14.72 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 vda 0.00 48.00 0.00 2.00 0.00 204.00 204.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 瓦達 0.00 0.00 0.00 18.00 0.00 104.00 11.56 0.00 0.22 0.00 0.22 0.22 0.40 瓦達 0.00 0.00 0.00 1.00 0.00 4.00 8.00 0.02 24.00 0.00 24.00 24.00 2.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 vda 0.00 39.00 0.00 4.00 0.00 176.00 88.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 4.00 8.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 17.00 0.00 100.00 11.76 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 vda 0.00 38.00 0.00 3.00 0.00 168.00 112.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 2.00 0.00 8.00 8.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 16.00 0.00 100.00 12.50 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 VDA 0.00 40.00 0.00 2.00 0.00 172.00 172.00 0.00 2.00 0.00 2.00 2.00 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 瓦達 0.00 0.00 0.00 10.00 0.00 64.00 12.80 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 vda 0.00 34.00 0.00 2.00 0.00 144.00 144.00 0.00 2.00 0.00 2.00 2.00 0.40 0.00 0.00 0.00 2.00 0.00 12.00 12.00 0.00 0.00 0.00 0.00 0.00 0.00 瓦達 0.00 55.00 0.00 117.00 0.00 732.00 12.51 0.28 2.43 0.00 2.43 0.10 1.20 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
任何幫助表示讚賞! :-)
答案1
你需要增加/proc/sys/net/core/somaxconn
。預設值為 128。