如何应对Mysql频繁写入问题?揭秘最清晰的“神操作”
发表时间: 2020-06-09 22:00
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`
`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 };`
`//获取环境变量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)
`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)`
`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 0Requests/sec: 4321.60Transfer/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 0Requests/sec: 6554.26Transfer/sec: 2.28MB`