ruby Gett媒体基准测试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Gett媒体基准测试相关的知识,希望对你有一定的参考价值。

require 'benchmark/ips'

redis = Redis.new(:host => "127.0.0.1", :port => 6379, :db => 0)

cache_options = { expires_in: 1 }

keys1 = ('a'..'z').to_a
keys2 = ('aa'..'az').to_a

Benchmark.ips do |x|
  x.config(:time => 15, :warmup => 1)

  x.report('current') do
    noncached_keys = []
        
    result = Rails.cache.fetch_multi(*keys1, cache_options) { |key| noncached_keys << key; nil }

    if noncached_keys.present?
      new_data = noncached_keys.map { |key| [key, key] }
      new_data.each do |key, data|
        result[key] = Rails.cache.write key, data, cache_options
      end
    end
  end

  x.report('pipelined') do
    noncached_keys = []
    values = redis.mget(*keys2)
    result = Hash[keys2.zip(values)]

    result.each { |k, v| v.nil? && (noncached_keys << k) }

    if noncached_keys.present?
      new_data = noncached_keys.map { |key| [key, key] }
      redis.pipelined do
        new_data.each do |key, data|
          result[key] = redis.setex(key, cache_options[:expires_in], data) && data
        end
      end
    end
  end
end

# Calculating -------------------------------------
#             current   133.000  i/100ms
#           pipelined   328.000  i/100ms
# -------------------------------------------------
#             current      1.327k (± 4.4%) i/s -     19.950k
#           pipelined      3.326k (± 3.6%) i/s -     49.856k

以上是关于ruby Gett媒体基准测试的主要内容,如果未能解决你的问题,请参考以下文章

ruby SQL与ActiveRecord基准测试(订阅者)

Ruby基准测试一条多次运行的线[关闭]

rails ruby​​-prof 和基准测试

ruby 基准Ruby代码

ruby 基准ruby控制台命令

ruby ruby运行基准