开发过程中的一些小技巧



记录下开发过程中的一些小技巧,在 MAC 系统上举例展示

命令行中格式化显示 JSON

  • 安装 jq
1
brew install jq

然后可以如下使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
curl https://randomuser.me/api/ | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1165 0 1165 0 0 124 0 --:--:-- 0:00:09 --:--:-- 285
{
"results": [
{
"gender": "female",
"name": {
"title": "Mrs",
"first": "Sofia",
"last": "Stone"
},
"location": {
"street": {
"number": 7396,
"name": "Killarney Road"
},
"city": "Wicklow",
"state": "Carlow",
"country": "Ireland",
"postcode": 69102,
"coordinates": {
"latitude": "19.9220",
"longitude": "35.9099"
},
"timezone": {
"offset": "+5:30",
"description": "Bombay, Calcutta, Madras, New Delhi"
}
},
"email": "sofia.stone@example.com",
"login": {
"uuid": "ee7bb5f0-a40f-4104-9b13-b0ea576cf7be",
"username": "brownpeacock141",
"password": "andreas",
"salt": "qb8farwU",
"md5": "bd0e52955f418f08d0b06922055d2fd8",
"sha1": "507e2d674df1782229e1890d73268747cdef4cb6",
"sha256": "08f22447026147739d18f60495db1da6cc531f764ffcf1aa5ec7538acf12b004"
},
"dob": {
"date": "1993-09-07T04:14:06.050Z",
"age": 28
},
"registered": {
"date": "2013-06-22T12:09:59.986Z",
"age": 8
},
"phone": "041-479-4791",
"cell": "081-418-9241",
"id": {
"name": "PPS",
"value": "5604953T"
},
"picture": {
"large": "https://randomuser.me/api/portraits/women/30.jpg",
"medium": "https://randomuser.me/api/portraits/med/women/30.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/women/30.jpg"
},
"nat": "IE"
}
],
"info": {
"seed": "71221d63efd397af",
"results": 1,
"page": 1,
"version": "1.3"
}
}

另一种方式

1
curl https://randomuser.me/api/ | python -m json.tool

这是通过 Python 中的 json.tool 模块对 JSON(文件)进行格式化

ab 压测工具

ab(Apache HTTP server benchmarking tool) 是一个压测工具,默认 MAC 上已经安装。

如需要压测一个 url,最简单的方式:ab -n 100 -c 10 www.iamwr.com/,注意,该命令最后有一个 /。返回:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
ab -n 100 -c 10 www.iamwr.com/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.iamwr.com (be patient).....done


Server Software: nginx/1.10.3
Server Hostname: www.iamwr.com
Server Port: 80

Document Path: /
Document Length: 63177 bytes

Concurrency Level: 10
Time taken for tests: 8.784 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 6342200 bytes
HTML transferred: 6317700 bytes
Requests per second: 11.38 [#/sec] (mean)
Time per request: 878.442 [ms] (mean)
Time per request: 87.844 [ms] (mean, across all concurrent requests)
Transfer rate: 705.06 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 31 57 49.3 37 262
Processing: 105 775 514.0 659 2387
Waiting: 31 80 84.3 37 407
Total: 142 832 516.2 728 2432

Percentage of the requests served within a certain time (ms)
50% 728
66% 975
75% 1119
80% 1143
90% 1613
95% 1965
98% 2288
99% 2432
100% 2432 (longest request)

ab 命令更多使用方式参看 ab -h,另一个常见的压测命令行工具是 siege

本文结束 感谢阅读