brew install wrk
前端
`替換brew.git: cd "$(brew --repo)" git remote set-url origin https://mirrors.ustc.edu.cn/brew.git 替換homebrew-core.git: cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git`
brew install wrk
便可mysql
`const mysql = require('mysql'); const { MYSQL_CONF } = require('./config'); const con = mysql.createConnection(MYSQL_CONF); //創建鏈接 con.connect(); //統一執行sql的方法 function exec(sql) { const promise = new Promise((resolve, reject) => { con.query(sql, (error, result) => { if (error) { reject(error); } resolve(result); }); }); return promise; } //關閉鏈接 function kill() { con.end(); } module.exports = { exec, kill };`
config.js
(能夠根據環境變量區分配置)`//獲取環境變量 const env = process.env.NODE_ENV; let MYSQL_CONF; //開發環境的配置 MYSQL_CONF = { host: 'localhost', user: 'root', password: '123456', port: '3306', database: 'blog', socketPath: '/tmp/mysql.sock', }; module.exports = { MYSQL_CONF };`
`exec('CREATE TABLE IF NOT EXISTS TEST_WRITE(first_column INT , second_column VARCHAR(100));')` ![](https://imgkr.cn-bj.ufileos.com/89f03976-a79d-4242-bdf0-090a53f6438c.png)
Navicat
能夠看到表已經建立成功`app.get('/test', (req, res) => { exec("INSERT INTO first_table(first_column, second_column) VALUES(1, 'aaa');"); res.json(success({ errcode: 0, data: {} })); });`
`使用方法: wrk <選項> <被測HTTP服務的URL> Options: -c, --connections <N> 跟服務器創建並保持的TCP鏈接數量 -d, --duration <T> 壓測時間 -t, --threads <N> 使用多少個線程進行壓測 -s, --script <S> 指定Lua腳本路徑 -H, --header <H> 爲每個HTTP請求添加HTTP頭 --latency 在壓測結束後,打印延遲統計信息 --timeout <T> 超時時間 -v, --version 打印正在使用的wrk的詳細版本信息 <N>表明數字參數,支持國際單位 (1k, 1M, 1G) <T>表明時間參數,支持時間單位 (2s, 2m, 2h)`
http://localhost://8080
`wrk -t8 -c500 -d2s --latency "http://localhost:8080/test"`
`Running 2s test @ http://localhost:8080/test 8 threads and 500 connections Thread Stats Avg Stdev Max +/- Stdev Latency 40.70ms 9.60ms 83.48ms 66.26% Req/Sec 640.89 328.89 1.43k 64.29% Latency Distribution 50% 39.26ms 75% 46.33ms 90% 54.32ms 99% 65.23ms 8980 requests in 2.08s, 3.13MB read Socket errors: connect 253, read 201, write 0, timeout 0 Requests/sec: 4321.60 Transfer/sec: 1.50MB`
`wrk -t15 -c1000 -d30s --latency "http://localhost:8080/test"`
`Running 30s test @ http://localhost:8080/test 15 threads and 1000 connections (平均值) (標準差)(最大值)(正負一個標準差所佔比例) Thread Stats Avg Stdev Max +/- Stdev (延遲) Latency 35.32ms 17.38ms 345.78ms 96.45% Req/Sec 0.95k 661.40 2.38k 54.50% Latency Distribution 50% 33.36ms 75% 37.61ms 90% 42.49ms 99% 76.00ms 197231 requests in 30.09s, 68.65MB read Socket errors: connect 754, read 188, write 0, timeout 0 Requests/sec: 6554.26 Transfer/sec: 2.28MB`
..更多請期待下一章node