While evaluating performance of PHP frameworks I came across a strange problem
在评估PHP框架的性能时,我遇到了一个奇怪的问题
Sending a JSON as application/json
seems to be much slower than sending with no extra header (which seems to fallback to text/html
)
将JSON作为应用程序/ JSON发送似乎要比不发送任何额外的头(似乎回退到文本/html)慢得多。
Example #1 (application/json)
示例# 1(application / json)
header('Content-Type: application/json');
echo json_encode($data);
Example #2 (text/html)
例# 2(text / html)
echo json_encode($data);
Testing with apache bench (ab -c10 -n1000
) gives me:
使用apache bench (ab -c10 -n1000)进行测试,得到:
Example #1: 350 #/sec
例# 1:350 # /秒
Example #2: 440 #/sec
例2:440 # /秒
which shows that setting the extra header seems to be a little bit slower.
这表明设置额外的标题似乎有点慢。
But:
但是:
Getting the same JSONs via "ajax" (jQuery.getJSON('url', function(j){console.log(j)});
) makes the difference very big (timing as seen in Chrome Web Inspector):
通过“ajax”(jQuery)获得相同的JSONs。getJSON('url',函数(j){console.log(j)});
Example #1: 340 ms / request
示例#1:340 ms /请求
Example #2: 980 ms / request
示例#2:980 ms /请求
Whats the matter of this difference?
这种差异有什么关系?
Is there a reason to use application/json despite the performance difference?
尽管存在性能差异,使用application/json有理由吗?
2 个解决方案
#1
1
Does your server handle gzipping/deflate differently depending on content-type? Mine does. Believe ab does not accept gzip by default. (You can set this in ab with a custom header with the -H flag). But Chrome will always say it accepts gzipping.
您的服务器是否根据内容类型处理gzipping/deflate ?我所做的。相信ab不接受gzip默认值。(你可以在ab中设置一个带有-H标志的自定义标头)。但是Chrome总是说它接受gzipping。
You can use curl test to see if the files are different sizes:
您可以使用curl测试查看文件大小是否不同:
curl http://www.example.com/whatever --silent -H "Accept-Encoding: gzip,deflate" --write-out "size_download=%{size_download}\n" --output /dev/null
You can also look at the headers to see if gzipping is applied:
您还可以查看标题,看看是否应用了gzipping:
curl http://www.example.com/whatever -I -H "Accept-Encoding: gzip,deflate"
#2
2
I'll take up the last part of question:
我将讨论问题的最后一部分:
Is there a reason to use application/json despite the performance difference?
尽管存在性能差异,使用application/json有理由吗?
Answer: Yes
答:是的
Why: 1) text/html can often be malformed json and will go uncaught until you try parsing it. application/json will fail and you can easily debug whenever the json is malformed
原因:1)文本/html通常是格式不正确的json,在您尝试解析它之前,不会被发现。应用程序/json将失败,只要json出现问题,您就可以轻松地进行调试
2) If you are viewing json in browser, having the header type will format it in a user-friendly formatting. text/html will show it more as a blob.
2)如果您在浏览器中查看json,有标题类型将格式化为用户友好的格式。文本/html将更多地显示为blob。
3) If you are consuming this json on your webpage, application/json will immediately be converted into js object and you may access them as obj.firstnode.childnode
etc.
3)如果您在网页上使用这个json,应用程序/json将立即转换为js对象,您可以将其作为object .firstnode访问。childnode等等。
4) callback feature can work on application/json
, but not on text/html
回调特性可以在应用程序/json上工作,但不能在文本/html上工作
Note: Using gzip will sufficiently alleviate the performance problem. text/html
will still be bit faster, but not the recommended way for fetching json objects
注意:使用gzip可以充分缓解性能问题。文本/html仍然会快一点,但不是获取json对象的推荐方式
Would like to see more insight on performance though. Header length is definitely not causing performance issue. More to do with your webserver analyzing the header format.
不过,我想了解更多关于性能的信息。标题长度绝对不会导致性能问题。更多关于您的webserver分析标题格式。
#1
1
Does your server handle gzipping/deflate differently depending on content-type? Mine does. Believe ab does not accept gzip by default. (You can set this in ab with a custom header with the -H flag). But Chrome will always say it accepts gzipping.
您的服务器是否根据内容类型处理gzipping/deflate ?我所做的。相信ab不接受gzip默认值。(你可以在ab中设置一个带有-H标志的自定义标头)。但是Chrome总是说它接受gzipping。
You can use curl test to see if the files are different sizes:
您可以使用curl测试查看文件大小是否不同:
curl http://www.example.com/whatever --silent -H "Accept-Encoding: gzip,deflate" --write-out "size_download=%{size_download}\n" --output /dev/null
You can also look at the headers to see if gzipping is applied:
您还可以查看标题,看看是否应用了gzipping:
curl http://www.example.com/whatever -I -H "Accept-Encoding: gzip,deflate"
#2
2
I'll take up the last part of question:
我将讨论问题的最后一部分:
Is there a reason to use application/json despite the performance difference?
尽管存在性能差异,使用application/json有理由吗?
Answer: Yes
答:是的
Why: 1) text/html can often be malformed json and will go uncaught until you try parsing it. application/json will fail and you can easily debug whenever the json is malformed
原因:1)文本/html通常是格式不正确的json,在您尝试解析它之前,不会被发现。应用程序/json将失败,只要json出现问题,您就可以轻松地进行调试
2) If you are viewing json in browser, having the header type will format it in a user-friendly formatting. text/html will show it more as a blob.
2)如果您在浏览器中查看json,有标题类型将格式化为用户友好的格式。文本/html将更多地显示为blob。
3) If you are consuming this json on your webpage, application/json will immediately be converted into js object and you may access them as obj.firstnode.childnode
etc.
3)如果您在网页上使用这个json,应用程序/json将立即转换为js对象,您可以将其作为object .firstnode访问。childnode等等。
4) callback feature can work on application/json
, but not on text/html
回调特性可以在应用程序/json上工作,但不能在文本/html上工作
Note: Using gzip will sufficiently alleviate the performance problem. text/html
will still be bit faster, but not the recommended way for fetching json objects
注意:使用gzip可以充分缓解性能问题。文本/html仍然会快一点,但不是获取json对象的推荐方式
Would like to see more insight on performance though. Header length is definitely not causing performance issue. More to do with your webserver analyzing the header format.
不过,我想了解更多关于性能的信息。标题长度绝对不会导致性能问题。更多关于您的webserver分析标题格式。