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`
..更多请期待下一章