因此,我在eventmachine中有一个 TCP 服务器,而rubyracer被用作将操作(如过滤器或扩展)预置到服务器的一种方式。当服务器没有接收到大量数据时,这一切都很有趣,但是当它被淹没(有时需要)时,它变得非常慢。
所以,我做了一个小基准测试,看看 rubyracer 与 Ruby 相比有多慢,当我看到结果时我很震惊:
user system total real
V8: 0.060000 0.000000 0.060000 ( 0.059903)
Ruby: 0.000000 0.000000 0.000000 ( 0.000524)
老实说,我不介意它是否很慢,但我不希望它在完成数据处理之前锁定我的整个服务器。使用EM::defer
并不是一个真正的选择(我试过了,但它有时会产生大量线程,具体取决于洪水的强度)。我无法绕过洪水,因为我没有设计协议,而客户端要求它们像那样(尽管它很可怕)。
基准代码:
require 'v8'
require 'benchmark'
class User
def initialize
@name = "smack"
@sex = "female"
@age = rand(100)
@health = rand(100)
@level = rand(100)
@colour = rand(14)
end
attr_accessor :name, :sex, :age, :health, :level, :colour
end
# Create context and the function
context = V8::Context.new
code = "obj = {
__incybincy__: function() {
user.name + '' + '' + ''
user.sex + '' + '' + ''
user.age + '' + '' + ''
user.health + '' + '' + ''
user.level + '' + '' + ''
user.colour + '' + '' + ''
}
}"
context.eval(code)
# Insert the user into the context
user = User.new
context["user"] = user
# Benchmark
n = 100
Benchmark.bm do |x|
x.report("V8: ") do
n.times do
context['obj'].__incybincy__
end
end
x.report("Ruby: ") do
n.times do
user.name + "" + ""
user.sex + "" + ""
user.age.to_s + "" + ""
user.health.to_s + "" + ""
user.level.to_s + "" + ""
user.colour.to_s + "" + ""
end
end
end
编辑
问题:有没有办法消除由 therubyracer 引起的瓶颈?通过其他方式将 JavaScript 实现到 Ruby 中是可以接受的。
2012 年 3 月 7 日更新
因此,我设法优化了代码,因为我发现导致瓶颈的原因是 Ruby<->JS 通信,每次[native code]
执行时都会发生这种情况,因为 ruby 对类使用 getter 和 setter 方法,或者当对象在语言之间直接传递时。
user system total real
V8-optimized: 0.050000 0.000000 0.050000 ( 0.049733)
V8-normal: 0.870000 0.050000 0.920000 ( 0.885439)
Ruby: 0.010000 0.000000 0.010000 ( 0.015064)
#where n is 1000
因此,我通过在 JS 端缓存来减少 Ruby 和 JS 之间的调用次数,但这并没有像我希望的那样优化它,因为至少必须将一个对象传递给函数:aHash
或至少一个 JSON String
,我什至达到了传递 a 的长度Fixnum
——这让我惊呼 FML——这并没有比传递一个字符串(如果有的话)有很大的改进。
我仍然希望有一个比我更好更快的解决方案。