File: benchmark.rb

package info (click to toggle)
ruby-faye 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,792 kB
  • sloc: javascript: 14,833; ruby: 5,068; makefile: 30
file content (45 lines) | stat: -rw-r--r-- 962 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require 'rubygems'
require 'bundler/setup'
require 'faye'

port   = ARGV[0] || 9292
path   = ARGV[1] || 'bayeux'
scheme = ARGV[2] == 'tls' ? 'https' : 'http'

EM.run {
  A = Faye::Client.new("#{ scheme }://0.0.0.0:#{ port }/#{ path }")
  B = Faye::Client.new("#{ scheme }://0.0.0.0:#{ port }/#{ path }")

  A.connect do
    B.connect do

      time = Time.now.to_f * 1000
      MAX  = 1000

      stop = lambda do
        puts Time.now.to_f * 1000 - time
        EM.stop
      end

      handle = lambda do |client, channel|
        lambda do |n|
          if n == MAX
            stop.call
          else
            client.publish(channel, n + 1)
          end
        end
      end

      sub_a = A.subscribe('/chat/a', &handle.call(A, '/chat/b'))
      sub_b = B.subscribe('/chat/b', &handle.call(B, '/chat/a'))

      sub_a.callback do
        sub_b.callback do
          puts 'START'
          A.publish('/chat/b', 0)
        end
      end
    end
  end
}