Skip to content

Commit

Permalink
Add benchmarks and a few slight optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
brainopia committed Oct 16, 2010
1 parent 8c5ab26 commit 9ae139b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ group :development, :production do
gem 'twitter'
end

group :benchmark do
gem 'json'
end

group :websocket do
gem 'json'
gem 'em-websocket'
Expand Down
46 changes: 46 additions & 0 deletions benchmarks/worm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'bundler'
Bundler.require :benchmark

$:.unshift './../lib'
require 'cell'
require 'map'
require 'worm'
require 'human'
require 'benchmark'

def json(command, data)
JSON.generate [command, data]
end

Benchmark.bmbm do |x|
x.report('Worm.new') do
1000.times do
json('id', Object.new.object_id)
Worm.new Object.new
end
end

x.report('Worm.find+direction') do
1000.times do
socket = Object.new
Worm.new socket
Worm.find(socket).direction = '1'.to_i
end
end

x.report('Periodic Timer') do
10.times do
Worm.all.each &:move
json = JSON.generate ['render', Map.data]
Worm.sockets.map do
json
end
end
end

x.report('Worm.destroy') do
1000.times do
Worm.destroy Worm.storage.first.first
end
end
end
4 changes: 4 additions & 0 deletions lib/worm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def self.sockets
@@worms.keys
end

def self.storage
@@worms
end

#not realized methods yet

def check_availability(cell)
Expand Down
20 changes: 16 additions & 4 deletions public/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,28 @@ keyboard = {

switch(event.which){
case 37:
socket.send(4)
if (keyboard.direction != 4){
keyboard.direction = 4
socket.send(4)
}
break
case 38:
socket.send(1)
if (keyboard.direction != 1){
keyboard.direction = 1
socket.send(1)
}
break
case 39:
socket.send(2)
if (keyboard.direction != 2){
keyboard.direction = 2
socket.send(2)
}
break
case 40:
socket.send(3)
if (keyboard.direction != 3){
keyboard.direction = 3
socket.send(3)
}
break
}
})
Expand Down

0 comments on commit 9ae139b

Please sign in to comment.