Crystal is a Rubyists friend (quick anecdote)

16

Click here to load reader

Transcript of Crystal is a Rubyists friend (quick anecdote)

Page 1: Crystal is a Rubyists friend (quick anecdote)

CRYSTAL IS A RUBYISTS FRIEND(QUICK ANECDOTE)

FORREST CHANG (OCRUBY)[email protected]

Page 2: Crystal is a Rubyists friend (quick anecdote)

BEFORE WE STARTWhich of the following caode samples is Ruby?

Page 3: Crystal is a Rubyists friend (quick anecdote)

APPLICATION 1class App < Roda plugin :streaming plugin :basic_auth, authenticator: proc {|user, pass| [user, pass] == %w[client password]} route do |r| r.basic_auth { stream do |out| puts "streaming" IO.popen("tail -f #{SYSTEM_LOG}") do |io| io.each { |s| out << s } end end } end end

run App.freeze.app

Page 4: Crystal is a Rubyists friend (quick anecdote)

APPLICATION 2ws "/ws" do |socket|

socket.on_message { |message| puts "Streaming #{SYSTEM_LOG}" if message == "TopSecret1234!" Process.run("tail", {"-f", SYSTEM_LOG} ) { |proc | proc.output.each_line { |line| socket.send line } } end }

end

Kemal.run

Page 5: Crystal is a Rubyists friend (quick anecdote)

HARD TO TELL WHICH IS RUBY?That's the pointThe 2nd one is CrystalEasy/Comfortable for RubyistsA subset compiles for both language

Page 6: Crystal is a Rubyists friend (quick anecdote)

BACKGROUNDWanted to stream syslog entries real time for visualization in browserbased appHost machine, old, small - want a corresponding lightweight solutionWanted to try RodaCombined need (bullet 1) and desire (bullet 3)

Page 7: Crystal is a Rubyists friend (quick anecdote)

RODA

Small and lightUses a routing tree for routes (key distinguisher)Faster than Sinatra 15042.26 rps vs 4441.61 rps

Reasons Why

http://roda.jeremyevans.net/index.html

https://github.com/luislavena/bench-micro#requestssechttp://roda.jeremyevans.net/why.html

Page 8: Crystal is a Rubyists friend (quick anecdote)

RODA EXPERIENCEFairly easy to use (though routing tree is different)Supported streaming output pretty easilyMaybe an hour plus to get running

Page 9: Crystal is a Rubyists friend (quick anecdote)

PROBLEMBrowser supports streaming outputJavascript does not - D'oh!Need Server Side Events or Websockets for same effectDarn! Back to the drawing board

Page 10: Crystal is a Rubyists friend (quick anecdote)

RODA WEBSOCKET SUPPORT

Built in plugin (Roda designed for and heavily uses plugins) - Yay!Requires faye, Event Machine, mutexes – Hmm.Quick and small solution getting more complex and larger

http://roda.jeremyevans.net/rdoc/classes/Roda/RodaPlugins/Websockets.htmln

Page 11: Crystal is a Rubyists friend (quick anecdote)

SO HOW ABOUT CRYSTAL?Want a framework vs writing raw callsEnter Kemal - "sinatra for Crystal"

Crystal about as fast and as small as GolangBut is a "statically typed, type inferenced Ruby that compiles to nativecode"So all the bene�ts of Golang, with the Joy of Ruby

http://kemalcr.com/

Page 12: Crystal is a Rubyists friend (quick anecdote)

KEMAL SPEED44X faster that Sinatra

Framework Request Per Second Avg. Response Time

Kemal (Production) 100238 0.395ms

Sinatra (Thin) 2274 43.82ms

Page 13: Crystal is a Rubyists friend (quick anecdote)

KEMAL WEBSOCKETS SUPPORTBuilt inFast

Thor is used to run the benchmark.

thor -A 10000

Crystal (Kemal) CPU 1.85 Memory 11.2 MB

Node.js (ws) CPU 38.95 Memory 906.3 MB

This benchmark was performed on a 2013 Late MacbookPro with 2Ghz i7 and 8G ram.

P.S: Less is better

http://localhost:3000/

Page 14: Crystal is a Rubyists friend (quick anecdote)

One of the prime reasons people left Ruby for node was real time push (i.e.websockets), or performance/resourcesThis shows Crystal exceeds node in all 3 of those, w/the joy of Ruby

Page 15: Crystal is a Rubyists friend (quick anecdote)

KEMAL EXPERIENCEtook about 15 min to get it workingKemal author @sdogruyol super responsive/helpful on gitter

Since added functionality to that initial codeSuccess! Got the job done, on to the app

https://gitter.im/crystal-lang/crystal

Page 16: Crystal is a Rubyists friend (quick anecdote)

SUMMARYCrystal easy/comfortable for RubyistsFast and smallKemal instead of sinatra, roda, cuba, etc.For Rubyists, Crystal can be better �t than Golang esp. if starting fromscratchCommunity/Gitter active and helpfulBe Happy and Productive