Sha256: 9a6daeb3141cb4d92ea7b37fa9a4f4f257fd2dfc481a207181a5710051f90b8d

Contents?: true

Size: 1.37 KB

Versions: 90

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require 'bundler/setup'
require 'polyphony/adapters/redis'
require 'json'

X_SESSIONS = 1000
X_NODES = 10_000
X_SUBSCRIPTIONS_PER_SESSION = 1000

$sessions = []
X_SESSIONS.times do
  $sessions << {
    subscriptions: X_SUBSCRIPTIONS_PER_SESSION.times.map do
      "node#{rand(X_NODES)}"
    end.uniq
  }
end

REDIS_HOST = ENV['REDIS_HOST'] || 'localhost'
p redis_host: REDIS_HOST

spin do
  redis = Redis.new(host: REDIS_HOST)
  redis.subscribe('events') do |on|
    on.message do |_, message|
      distribute_event(JSON.parse(message, symbolize_names: true))
    end
  end
end

$update_count = 0

def distribute_event(event)
  $update_count += 1
  t0 = Time.now
  count = 0
  $sessions.each do |s|
    count += 1 if s[:subscriptions].include?(event[:path])
  end
  elapsed = Time.now - t0
  rate = X_SESSIONS / elapsed
  puts "elapsed: #{elapsed} (#{rate}/s)" if $update_count % 100 == 0
end

spin do
  redis = Redis.new(host: REDIS_HOST)
  throttled_loop(1000) do
    redis.publish('events', { path: "node#{rand(X_NODES)}" }.to_json)
  end
end

spin do
  last_count = 0
  last_stamp = Time.now
  throttled_loop(1) do
    now = Time.now
    elapsed = now - last_stamp
    delta = $update_count - last_count
    puts "update rate: #{delta.to_f / elapsed}"
    last_stamp = now
    last_count = $update_count
  end
end

trap('SIGINT') do
  puts 'bye...'
  exit!
end

suspend

Version data entries

90 entries across 90 versions & 1 rubygems

Version Path
polyphony-1.6 examples/adapters/redis_pubsub_perf.rb
polyphony-1.5 examples/adapters/redis_pubsub_perf.rb
polyphony-1.4 examples/adapters/redis_pubsub_perf.rb
polyphony-1.3 examples/adapters/redis_pubsub_perf.rb
polyphony-1.2.1 examples/adapters/redis_pubsub_perf.rb
polyphony-1.2 examples/adapters/redis_pubsub_perf.rb
polyphony-1.1.1 examples/adapters/redis_pubsub_perf.rb
polyphony-1.1 examples/adapters/redis_pubsub_perf.rb
polyphony-1.0.2 examples/adapters/redis_pubsub_perf.rb
polyphony-1.0.1 examples/adapters/redis_pubsub_perf.rb
polyphony-1.0 examples/adapters/redis_pubsub_perf.rb
polyphony-0.99.6 examples/adapters/redis_pubsub_perf.rb
polyphony-0.99.5 examples/adapters/redis_pubsub_perf.rb
polyphony-0.99.4 examples/adapters/redis_pubsub_perf.rb
polyphony-0.99.3 examples/adapters/redis_pubsub_perf.rb
polyphony-0.99.2 examples/adapters/redis_pubsub_perf.rb
polyphony-0.99.1 examples/adapters/redis_pubsub_perf.rb
polyphony-0.99 examples/adapters/redis_pubsub_perf.rb
polyphony-0.98 examples/adapters/redis_pubsub_perf.rb
polyphony-0.97 examples/adapters/redis_pubsub_perf.rb