Crystal is a Rubyists friend (quick anecdote)

Post on 21-Jan-2018

378 views 0 download

Transcript of Crystal is a Rubyists friend (quick anecdote)

CRYSTAL IS A RUBYISTS FRIEND(QUICK ANECDOTE)

FORREST CHANG (OCRUBY)FKCHANG2000@YAHOO.COM

BEFORE WE STARTWhich of the following caode samples is Ruby?

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

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

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

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)

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

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

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

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

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/

KEMAL SPEED44X faster that Sinatra

Framework Request Per Second Avg. Response Time

Kemal (Production) 100238 0.395ms

Sinatra (Thin) 2274 43.82ms

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/

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

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

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