【问题标题】:Difference between curl expressionscurl表达式之间的区别
【发布时间】:2016-07-07 20:11:02
【问题描述】:

我有一个在 localhost:3000 运行的 API 服务器,我正在尝试使用以下两个表达式对其进行查询:

[wani@lenovo ilparser-docker]$ time (curl "localhost:3000/parse?lang=hin&data=देश" )
{"tokenizer":"<Sentence id=\"1\">\n1\tदेश\tunk\n<\/Sentence>\n"}
real    0m0.023s
user    0m0.009s
sys 0m0.004s
[wani@lenovo ilparser-docker]$ time (curl -XGET localhost:3000/parse -F lang=hin -F data="देश" )
{"tokenizer":"<Sentence id=\"1\">\n1\tदेश\tunk\n<\/Sentence>\n"}
real    0m1.101s
user    0m0.020s
sys 0m0.070s

为什么第二个表达式要花这么多时间?

更加冗长:

[wani@lenovo ilparser-docker]$ time curl -v localhost:3000/parse -F lang=hin -F data="देश"
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
> POST /parse HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 244
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------1eb5e5991b976cb1
> 
* Done waiting for 100-continue
< HTTP/1.1 200 OK
< Content-Length: 70
< Server: Mojolicious (Perl)
< Content-Type: application/json;charset=UTF-8
< Date: Mon, 21 Mar 2016 11:06:09 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
{"tokenizer":"<Sentence id=\"1\">\n1\tदेश\tunk\n<\/Sentence>\n"}
real    0m1.106s
user    0m0.027s
sys 0m0.068s
[wani@lenovo ilparser-docker]$ time curl -v localhost:3000/parse --data lang=hin --data data="देश"
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /parse HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 23
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 23 out of 23 bytes
< HTTP/1.1 200 OK
< Server: Mojolicious (Perl)
< Content-Length: 70
< Connection: keep-alive
< Date: Mon, 21 Mar 2016 11:06:24 GMT
< Content-Type: application/json;charset=UTF-8
< 
* Connection #0 to host localhost left intact
{"tokenizer":"<Sentence id=\"1\">\n1\tदेश\tunk\n<\/Sentence>\n"}
real    0m0.031s
user    0m0.011s
sys 0m0.003s

Expect: 100-continue 听起来很可疑,所以我清除了那个标题:

[wani@lenovo ilparser-docker]$ time curl -v -F lang=hin -F data="देश" "localhost:3000/parse" -H Expect: --trace-time
16:48:04.513691 *   Trying 127.0.0.1...
16:48:04.513933 * Connected to localhost (127.0.0.1) port 3000 (#0)
16:48:04.514083 * Initializing NSS with certpath: sql:/etc/pki/nssdb
16:48:04.610095 > POST /parse HTTP/1.1
16:48:04.610095 > Host: localhost:3000
16:48:04.610095 > User-Agent: curl/7.43.0
16:48:04.610095 > Accept: */*
16:48:04.610095 > Content-Length: 244
16:48:04.610095 > Content-Type: multipart/form-data; boundary=------------------------24f30647b16ba82d
16:48:04.610095 > 
16:48:04.618107 < HTTP/1.1 200 OK
16:48:04.618194 < Content-Length: 70
16:48:04.618249 < Server: Mojolicious (Perl)
16:48:04.618306 < Content-Type: application/json;charset=UTF-8
16:48:04.618370 < Date: Mon, 21 Mar 2016 11:18:04 GMT
16:48:04.618430 < Connection: keep-alive
16:48:04.618492 < 
16:48:04.618590 * Connection #0 to host localhost left intact
{"tokenizer":"<Sentence id=\"1\">\n1\tदेश\tunk\n<\/Sentence>\n"}
real    0m0.117s
user    0m0.023s
sys 0m0.082s

现在剩下的唯一时间是:Initializing NSS with certpath: sql:/etc/pki/nssdb。为什么 curl 在这种情况下会这样做?

【问题讨论】:

    标签: linux curl time libcurl


    【解决方案1】:

    在 @DanielStenberg 对 IRC 的一些帮助之后,我开始知道存在 db 负载,因为 curl 在这种情况下会初始化 nss,因为 curl 需要一个好的随机源来用于 -F 的边界分隔符。 Curl 可以使用 getrandom() 系统调用或从 /dev/urandom 读取位,因为边界分隔符不需要以任何方式加密,但 curl 只希望在其他一些地方安全随机,因此 curl 重用它已经使用的随机函数有。

    【讨论】:

      猜你喜欢
      • 2018-08-22
      • 2012-06-01
      • 2013-09-18
      • 1970-01-01
      • 2021-01-24
      • 2013-12-14
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多