Hacker News new | ask | show | jobs
by codegangsta 4581 days ago
Have you seen Martini?

https://github.com/codegangsta/martini

6 comments

> "Handlers are invoked via reflection"

That's where I say NO. go's reflect package is slow and a nightmare to use. Go's JSON parser, which uses reflect, is 2.5x slower than Node.js (in my own benchmarks [1]). And Go 1.1 was 3.5x worse that Node.js at parsing JSON. Again, those are my benchmarks, but they seem consistent with what other people are finding (from my Googling around).

I would not make this trade off, I would not use reflect on EVERY request, just so that my request handlers are compatible with http.HandleFunc. It almost seems silly to do that. That's why other frameworks (like Revel) are using custom Request objects.

However, I do understand why the author is emphasizing this feature. If your handlers are incompatible with http.HandleFunc (i.e. if they take a custom Request object), you need your own Router which invokes the handlers, and you need your own ServeMux, so you end up giving up on a lot of functionality from net/http. That's why Revel has a ton of custom objects that mirror what net/http has and it proxies method calls.

So you keep wondering: "why the hell am I writing so much seemingly unrelated code? I just want to add a 'user' property to each request, but now I've coded so many struct's and created so many files". It makes you feel unproductive and it bloats the code.

Again, I've made my choice, but you should be aware of such caveats and, of course, benchmark things yourself.

1) My benchmark results. For some charts, higher is better, for others, lower is better. Use your judgement. http://goo.gl/xfWFXo

Your benchmark results look very different from that of the web framework benchmark:

http://www.techempower.com/benchmarks/#section=data-r7&hw=i7...

In that benchmark Go's JSON performance is almost 3x higher than that of Node.

Any reasons why there's such a big difference?

I think it's because they're measuring the rate at which Node.js and Go receive requests, process json, and reply as a single metric.

I measured the speed with which Go and Node.js decode/encode JSON, i.e. there is no network and web server involved, just json.Marshal()/json.Unmarshal() vs JSON.parse()/JSON.stringify()

I benchmarked the web server speed separately.

Do you have the source code for your benchmarks? Thank you.
Sure, I updated the spreadsheet with links to the code for each benchmark. BSD license. Please keep in mind these were quick and dirty. If you run any of them, please reply with your findings.
It looks really slick, and I'm considering switching over a go-backed JSON API to an Ember app I'm building to use it.

How's the performance? I'm a little afraid of the "reflection" boogeyman, but I don't have any experience to say what kind of impact we're talking about here.

I'm coming from Rails day-to-day, and a very light, compiled Go web backend is a refreshing change of pace. I'm leery of giving up the simplicity of net/http, but if it stays small maybe it's the way to go. Right now I'm popping in gorilla toolkit, though, so if martini is as "light touch" as that, I'd be up for using it.

I have not tried this yet, but https://github.com/benbjohnson/megajson
I'm kind of amazed at the SLOC, ~600 lines for the whole thing. Flask is much bigger and depends on Werkzeug (~20K?), heck even Bottle doesn't come in under 1,000 lines. That is incredibly attractive.
Yup I plan to keep it that way too. I believe there is enough flexibility in Martini to cultivate some great functionality around the martini-contrib repo. There is already some pretty awesome things landing there https://github.com/codegangsta/martini-contrib
Checkout Camping (Ruby) & Squatting (Perl). Both are web microframeworks that aim (when minimised) to fit at or under 4K characters.

Here's the squashed versions of...

- Camping (currently 4.031 kb over 55 lines) - https://github.com/camping/camping/blob/master/lib/camping.r...

- Squatting (currently 3.929 kb over 68 lines) - https://github.com/beppu/squatting/blob/master/lib/squatting...

bottle.py might be a better Python comparison, since they're more concerned about being small & standalone.
Yeah just after posting my comment.

BTW, great work, that's exactly what I was looking for.

+1 for Martini. Spent some time looking for a decent framework like this, and was delighted when I discovered it.
Thanks. As a Flask user myself, and sort-of interested in Go, I'm going to take a look at this. Thanks.