Node.JS、Socket.IO 和大型 XML 文件:极大的性能损失?
前几天我偶然发现了一个丑陋的问题:我使用 XML 字符串在服务器和客户端之间进行通信并交换精确的自定义数据。只要 XML 文件的大小不太大,就可以正常工作。
但在一种情况下,我需要传输大约 35000 行的 XML 文件,该文件在客户端会生成大约 2200 行的 HTML 表。 在服务器上,我使用 fs.readFile() 方法,然后立即发送它的内容。在客户端调用 socket.on('message')
处理程序之前,需要很长时间(或者准确地说:至少 6 秒)。
我是 Node.js 新手,昨天一整天都在谷歌搜索。有谁知道如何加快读取 XML 文件并在给定环境中传输它的速度吗?
I stumbled upon an ugly problem the other day: I'm using XML strings to communicate between server and client and exchange precisely custom defined Data. Works fine so far as long as the size of the XML file isn't too large.
But in one case I need to transmit an XML file with ~35000 rows that, client-side, results in an HTML table with ~2200 table rows.
On the server I'm using the fs.readFile()
method and then immediately send the content of it. It takes ages (or to be precise: min. 6 seconds) until the socket.on('message')
-handler is called on the client.
I'm new to Node.js and have been Googling all day yesterday. Does anyone have an idea on how to speed up reading the XML file and transmitting it in the given environment just the tiniest bit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我得到了 3 个可能提高性能的建议:
使用 JSON 而不是 XML。解析速度要快得多,有时它的大小比 XML 小。您可以使用 node-expat 将 XML 转换为 JSON。请参阅此基准。
在服务器端制作表格。服务器显然比客户端快。然后你也可以使用缓存。
我认为最好使用直接 AJAX 调用,socket.io 很重,我不认为它不是为重对象而设计的。
I got 3 sugggestion that may increase performance:
Use JSON rather than XML. Much faster to parse and at times it has less size than XML. You can use node-expat to convert XML to JSON. See this benchmark.
Make the tables in server-side. Server's are obviously faster than clients. Then you can also use caching.
I think it is better to use direct AJAX calls, socket.io is heavy and I don't think it is not made for heavy objects.
我一直在使用 https://github.com/ astro/node-expat/blob/master/lib/node-expat.js 来解析巨大的 37GB XML 文件。它支持文件流并且运行良好。
设置萨克斯风格的事件监听器非常简单,如果您需要帮助,请告诉我。
I've been using https://github.com/astro/node-expat/blob/master/lib/node-expat.js to parse an enormous 37GB XML file. It supports file streaming and is working great.
Setting up sax-style event listeners is pretty easy, let me know if you need help with it.