Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
derekcollison committed Oct 30, 2010
0 parents commit d00c111
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*~
\#*\#
.\#*
*.rbc
.bundle
.gem
.DS_Store

19 changes: 19 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright 2009, 2010 Derek Collison <derek.collison@gmail.com>. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# NATS

A simple publish-subscribe messaging system.

## Supported Platforms

This gem currently works on the following Ruby platforms :

- MRI 1.8 and 1.9
- Rubinius

## Usage

require "nats/client"

NATS.start do |nc|
# Simple Subscriber
nc.subscribe('foo') { |sub, msg| puts "Msg received on [#{sub}] : '#{msg}' }

# Simple Publisher
nc.publish('foo', 'Hello World!')
end
24 changes: 24 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

desc "Run rspec"
task :spec do
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new do |t|
t.rspec_opts = %w(-fs -c)
end
end

desc "Build the gem"
task :gem do
sh 'gem build *.gemspec'
end

desc "Synonym for spec"
task :test => :spec
desc "Synonym for spec"
task :tests => :spec

desc "Synonym for gem"
task :pkg => :gem
desc "Synonym for gem"
task :package => :gem
94 changes: 94 additions & 0 deletions benchmark/perf-sublist.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require File.dirname(__FILE__) + '/../lib/server/sublist'

class PerfSublist
@@levels = 5
@@targets = ['derek', 'ruth', 'sam', 'meg', 'brett', 'ben', 'miles', 'bella', 'rex', 'diamond']
@@sublist = Sublist.new()

def PerfSublist.subsInit(pre=nil)
@@targets.each {|t|
sub = pre ? (pre + "." + t) : t
@@sublist.insert(sub, sub)
subsInit(sub) if sub.split(".").size < @@levels
}
end

def PerfSublist.disableSublistCache
@@sublist.disable_cache
end

def PerfSublist.addWildcards
@@sublist.insert("ruth.>", "honey")
@@sublist.insert("ruth.sam.meg.>", "honey")
@@sublist.insert("ruth.*.meg.*", "honey")
end

def PerfSublist.matchTest(subject, loop)
start = Time.now
loop.times {@@sublist.match(subject)}
stop = Time.now
puts "Matched #{subject} #{loop} times in #{ms_time(stop-start)} ms @ #{(loop/(stop-start)).to_i}/sec"
end

def PerfSublist.reset
@@sublist = Sublist.new()
end

def PerfSublist.subscriptionCount
@@sublist.count
end

end

def ms_time(t)
"%0.2f" % (t*1000)
end

# setup the subscription list.
start = Time.now
PerfSublist.subsInit
PerfSublist.addWildcards
stop = Time.now
puts
puts "Sublist holding #{PerfSublist.subscriptionCount} subscriptions"
puts "Insert rate of #{(PerfSublist.subscriptionCount/(stop-start)).to_i}/sec"

#require 'profiler'

puts
puts "cache test"

#Profiler__::start_profile
PerfSublist.matchTest("derek.sam.meg.ruth", 100000)
PerfSublist.matchTest("ruth.sam.meg.derek", 100000) # multiple returns w/ wc
PerfSublist.matchTest("derek.sam.meg.billybob", 100000) # worst case miss
#Profiler__::stop_profile
#Profiler__::print_profile($stdout)

puts
puts "Hit any key to continue w/ cache disabled"
STDIN.getc

#Profiler__::start_profile
PerfSublist.disableSublistCache
PerfSublist.matchTest("derek.sam.meg.ruth", 50000)
PerfSublist.matchTest("ruth.sam.meg.derek", 50000) # multiple returns w/ wc
PerfSublist.matchTest("derek.sam.meg.billybob", 50000) # worst case miss
#Profiler__::stop_profile
#Profiler__::print_profile($stdout)

#Run multiple times to see Jruby speedup
0.times do
puts "\n\n"
#PerfSublist.reset
#PerfSublist.subsInit
#PerfSublist.disableSublistCache
PerfSublist.matchTest("derek.sam.meg.ruth", 50000)
PerfSublist.matchTest("ruth.sam.meg.derek", 50000) # multiple returns w/ wc
PerfSublist.matchTest("derek.sam.meg.billybob", 50000) # worst case miss
end

# Allows you to see memory usage, etc
puts
puts "Hit any key to quit"
STDIN.getc
34 changes: 34 additions & 0 deletions benchmark/pub_speed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

require File.dirname(__FILE__) + '/../lib/client'

data = '-ok-'.freeze

NATS.on_error { |err| puts "Error: #{err}" }

NATS.start do |c|

r, loop = 0, 500000
start = Time.now

do_subs = true if ARGV.shift =~ /-listen/i

c.subscribe('test') { |sub, msg| r += 1 } if do_subs

c.subscribe(:done) do |sub, msg|
stop = Time.now
puts "Stop msg received on [#{sub}] : '#{msg}'"
puts "Test completed."
puts "Received #{r} of #{loop} expected msgs" if do_subs
puts "Nats processed #{(loop/(stop-start)).ceil} msgs/sec.\n"
NATS.stop
end

# Send data loop times
loop.times { c.publish(:test, data) }

# end condition trigger
c.publish(:done, 'ok')

puts "All #{loop} messages sent from client at a perceived rate of #{(loop/(Time.now-start)).ceil} msgs/sec.\n"

end
5 changes: 5 additions & 0 deletions bin/nats-server
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
# NATS command line interface script.
# Run <tt>nats-server -h</tt> to get more usage.

require 'nats/server'
22 changes: 22 additions & 0 deletions examples/auth_sub.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env ruby

require 'nats/client'

trap("TERM") { NATS.stop }
trap("INT") { NATS.stop }

def usage
puts "Usage: auth_sub <user> <pass> <subject>"; exit
end

user = ARGV.shift
pass = ARGV.shift
subject = ARGV.shift
usage unless user and pass and subject

uri = "nats://#{user}:#{pass}@localhost:8222"

NATS.start(:uri => uri) do |n|
puts "Listening on '#{subject}'"
n.subscribe(subject) { |sub, msg| puts "Msg Received on [#{sub}] : '#{msg}'" }
end
18 changes: 18 additions & 0 deletions examples/simple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby

require 'nats/client'

trap("TERM") { EM.stop }
trap("INT") { EM.stop }

NATS.start { |c|

test_sub = c.subscribe('test') do |sub, msg|
puts "Test Sub is #{test_sub}"
puts "received data on sub:#{sub}- #{msg}"
c.unsubscribe(test_sub)
end

c.publish('test', 'Hello World!')
c.publish('test', 'Hello World 2!')
}
35 changes: 35 additions & 0 deletions nats.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

spec = Gem::Specification.new do |s|
s.name = 'nats'
s.version = '0.1'
s.date = '2010-10-30'
s.summary = 'Simple Publish-Subscribe Messaging System'
s.homepage = "http://github.com/derekcollison/nats"
s.description = "A lightweight, fast, publish-subscribe messaging system."
s.has_rdoc = false

s.authors = ["Derek Collison"]
s.email = ["derek.collison@gmail.com"]

s.add_dependency('eventmachine', '>= 0.12.4')
s.add_dependency('yajl-ruby', '>= 0.7.8')

s.require_paths = ['lib']
s.bindir = 'bin'
s.executables = ['nats-server']

s.files = [
"COPYING",
"nats.gemspec",
"Rakefile",
"bin/nats-server",
"lib/nats/client.rb",
"lib/nats/ext/bytesize.rb",
"lib/nats/ext/em.rb",
"lib/nats/ext/json.rb",
"lib/nats/server.rb",
"lib/nats/server/parser.rb",
"lib/nats/server/sublist.rb"
]

end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'nats'

0 comments on commit d00c111

Please sign in to comment.