<commit_before>require 'helper'
require 'benchmark'
require 'active_support/cache/dalli_store'

describe 'performance' do
  before do
    puts "Testing #{Dalli::VERSION} with #{RUBY_DESCRIPTION}"
    # We'll use a simple @value to try to avoid spending time in Marshal,
    # which is a constant penalty that both clients have to pay
    @value = []
    @marshalled = Marshal.dump(@value)

    @servers = ['127.0.0.1:19122', 'localhost:19122']
    @key1 = "Short"
    @key2 = "<KEY>
    @key3 = "Long"*40
    @key4 = "Medium"*8
    # 5 and 6 are only used for multiget miss test
    @key5 = "Medium2"*8
    @key6 = "Long3"*40
    @counter = 'counter'
  end

  should 'run benchmarks' do
    memcached do

      Benchmark.bm(32) do |x|

        n = 2500

        @ds = ActiveSupport::Cache::DalliStore.new(@servers)
        x.report("mixed:rails:dalli") do
          n.times do
            @ds.read @key1
            @ds.write @key2, @value
            @ds.fetch(@key3) { @value }
            @ds.fetch(@key2) { @value }
            @ds.fetch(@key1) { @value }
            @ds.write @key2, @value, :unless_exists => true
            @ds.delete @key2
            @ds.increment @counter, 1, :initial => 100
            @ds.increment @counter, 1, :expires_in => 12
            @ds.decrement @counter, 1
          end
        end

        x.report("mixed:rails-localcache:dalli") do
          n.times do
            @ds.with_local_cache do
              @ds.read @key1
              @ds.write @key2, @value
              @ds.fetch(@key3) { @value }
              @ds.fetch(@key2) { @value }
              @ds.fetch(@key1) { @value }
              @ds.write @key2, @value, :unless_exists => true
              @ds.delete @key2
              @ds.increment @counter, 1, :initial => 100
              @ds.increment @counter, 1, :expires_in => 12
              @ds.decrement @counter, 1
            end
          end
        end

        x.report("read_multi:rails-localstore:dalli") do
          n.times do
            @ds.with_local_cache do
              @ds.read_multi @key1, @key2, @key3
              @ds.read @key1
              @ds.read @key2
              @ds.read @key3
            end
            @ds.with_local_cache do
              @ds.read @key1
              @ds.read @key2
              @ds.read @key3
              @ds.read_multi @key1, @key2, @key3
            end
          end
        end

        @m = Dalli::Client.new(@servers)
        x.report("set:plain:dalli") do
          n.times do
            @m.set @key1, @marshalled, 0, :raw => true
            @m.set @key2, @marshalled, 0, :raw => true
            @m.set @key3, @marshalled, 0, :raw => true
            @m.set @key1, @marshalled, 0, :raw => true
            @m.set @key2, @marshalled, 0, :raw => true
            @m.set @key3, @marshalled, 0, :raw => true
          end
        end

        @m = Dalli::Client.new(@servers)<commit_msg>Add benchmarks to test LocalCache and read_multi with LocalCache<commit_after>require 'helper'
require 'benchmark'
require 'active_support/cache/dalli_store'

describe 'performance' do
  before do
    puts "Testing #{Dalli::VERSION} with #{RUBY_DESCRIPTION}"
    # We'll use a simple @value to try to avoid spending time in Marshal,
    # which is a constant penalty that both clients have to pay
    @value = []
    @marshalled = Marshal.dump(@value)

    @servers = ['127.0.0.1:19122', 'localhost:19122']
    @key1 = "Short"
    @key2 = "<KEY>
    @key3 = "Long"*40
    @key4 = "Medium"*8
    # 5 and 6 are only used for multiget miss test
    @key5 = "Medium2"*8
    @key6 = "Long3"*40
    @counter = 'counter'
  end

  should 'run benchmarks' do
    memcached do

      Benchmark.bm(37) do |x|

        n = 2500

        @ds = ActiveSupport::Cache::DalliStore.new(@servers)
        x.report("mixed:rails:dalli") do
          n.times do
            @ds.read @key1
            @ds.write @key2, @value
            @ds.fetch(@key3) { @value }
            @ds.fetch(@key2) { @value }
            @ds.fetch(@key1) { @value }
            @ds.write @key2, @value, :unless_exists => true
            @ds.delete @key2
            @ds.increment @counter, 1, :initial => 100
            @ds.increment @counter, 1, :expires_in => 12
            @ds.decrement @counter, 1
          end
        end

        x.report("mixed:rails-localcache:dalli") do
          n.times do
            @ds.with_local_cache do
              @ds.read @key1
              @ds.write @key2, @value
              @ds.fetch(@key3) { @value }
              @ds.fetch(@key2) { @value }
              @ds.fetch(@key1) { @value }
              @ds.write @key2, @value, :unless_exists => true
              @ds.delete @key2
              @ds.increment @counter, 1, :initial => 100
              @ds.increment @counter, 1, :expires_in => 12
              @ds.decrement @counter, 1
            end
          end
        end

        x.report("read_multi:rails-localstore:dalli") do
          n.times do
            @ds.with_local_cache do
              @ds.read_multi @key1, @key2, @key3
              @ds.read @key1
              @ds.read @key2
              @ds.read @key3
            end
            @ds.with_local_cache do
              @ds.read @key1
              @ds.read @key2
              @ds.read @key3
              @ds.read_multi @key1, @key2, @key3
            end
          end
        end

        @m = Dalli::Client.new(@servers)
        x.report("set:plain:dalli") do
          n.times do
            @ds.with_local_cache do
              @ds.read @key1
              @ds.write @key2, @value
              @ds.fetch(@key3) { @value }
              @ds.fetch(@key2) { @value }
              @ds.fetch(@key1) { @value }
              @ds.write @key2, @value, :unless_exists => true
              @ds.delete @key2
              @ds.increment @counter, 1, :initial => 100
              @ds.increment @counter, 1, :expires_in => 12
              @ds.decrement @counter, 1
            end
          end
        end

        @ds.clear
        sizeable_data = "<marquee>some view partial data</marquee>" * 50
        [@key1, @key2, @key3, @key4, @key5, @key6].each do |key|
          @ds.write(key, sizeable_data)
        end

        x.report("read_multi_big:rails:dalli") do
          n.times do
            @ds.read_multi @key1, @key2, @key3, @key4
            @ds.read @key1
            @ds.read @key2
            @ds.read @key3
            @ds.read @key4
            @ds.read @key1
            @ds.read @key2
            @ds.read @key3
            @ds.read_multi @key1, @key2, @key3
          end
        end

        x.report("read_multi_big:rails-localcache:dalli") do
          n.times do
            @ds.with_local_cache do
              @ds.read_multi @key1, @key2, @key3, @key4
              @ds.read @key1
              @ds.read @key2
              @ds.read @key3
              @ds.read @key4
            end
            @ds.with_local_cache do
              @ds.read @key1
              @ds.read @key2
              @ds.read @key3
              @ds.read_multi @key1, @key2, @key3
            end
          end
        end

        @m = Dalli::Client.new(@servers)
        x.report("set:plain:dalli") do
          n.times do
            @m.set @key1, @marshalled, 0, :raw => true
            @m.set @key2, @marshalled, 0, :raw => true
            @m.set @key3, @marshalled, 0, :raw => true
            @m.set @key1, @marshalled, 0, :raw => true
            @m.set @key2, @marshalled, 0, :raw => true
            @m.set @key3, @marshalled, 0, :raw => true
          end
        end

        @m = Dalli::Client.new(@servers)
{'fraction_non_alphanumeric': 0.12728228030999605, 'fraction_numerical': 0.030868251674766846, 'mean_word_length': 1.5228628230616301, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7283519', 'n_tokens_mistral': 2649, 'n_tokens_neox': 2491, 'n_words': 609}