Rewriting the scope_all benchmark in Sequel and Raw

@sam I wrote scope_all benchmark in raw sql to compare with sequel and AR. If the test is correctly written these are results I got:


  • its/s : 116
  • its/s deviation : 7.71
  • total allocated objects per iteration : 14128


  • its/s : 414
  • its/s deviation : 9.65
  • total allocated objects per iteration : 5032


  • its/s : 307
  • its/s deviation : 5.53
  • total allocated objects per iteration : 5018
1 Like

How can raw be slower than sequel, this is quite interesting, can you link to the sequel bench.

Also I think we should probably maybe do something with the results, perhaps if we just formatted a giant string with the results each time?

str = ""
User.all.each do |u|
   str << "name #{} email #{u.emal}\n"

This kind of makes the bench bit more honest cause it is actually trying to achieve something.

@sam Here is the same sequel bench.

How can raw be slower than sequel

I wasn’t expecting it neither. Maybe it’s slower because raw query is made through ActiveRecord::Base.connection. Perhaps raw sql queries in sequel and AR perform differently. I’ll write bench for raw sql through sequel to compare.

Also I think we should probably maybe do something with the results

Right know we convert to array after fetching. But doing something more specific is better idea, will fix it :thumbsup:

I added one more test to measure raw sql through sequel. Note that I added some operations on fetched records in scope_all benches, which caused baseline to go little lower, but differences remained the same.

Again, these are results I got:


its/s : 77
deviation : 3.85
allocated objects : 20136

raw sql through activerecord

its/s : 285
deviation : 5
allocated objects : 6020


its/s : 345
deviation : 6
allocated objects : 6035

raw sql through sequel

its/s : 350
deviation : 8
allocated objects : 7040

Interesting thing to note here is performance in both sequel tests is similar.

@sam now I figured out that you weren’t probably thinking of executing raw sql commands through orm’s like AR and sequal, but directly via pg and mysql gems. :confused:

Oh yeah you got to use raw connections for raw tests:

So you would use:

=> #<PG::Connection:0x00563053b11f78>

Don’t use the AR abstraction on top of connection as it is wasteful.

Also, keep in mind Sequel has tricks:

We got to test with this as well.

Note for Jeremy

Our long term plan is to make this graph/page here:

Show Sequel numbers and raw numbers as well.

But we got to get all the benches into good shape first.

Here is one raw test for postgres_scope_all, I did it this time directly via pg gem.

Results I got locally are following:

its/s : 942
allocated objects: 4004

1 Like

Its interesting to see 77 vs 942, it really drives home how much overhead AR brings to the table.

Can you update that post with all your results to date on this specific test?

Also, can you run it on Rails 2/3/4 for a quick and dirty comparison.

(I know it will be eventually backfilled, but I am curious) :wink:

1 Like

sure @sam, I’m on it :thumbsup:

1 Like
// Activerecord




// Sequel


// Raw pg


^ Here it is @sam :slight_smile:

Btw, I didn’t managed to run benchmark for rails 2, I couldn’t require rails gem in benchmark script. Not sure where’s the problem :confused:

1 Like

Awesome, now that you have this data. I want the graph amended so it can display a comparison of all these 5 results! Start hacking away at the web UI and the backfill.

Only focus on this single test, this is the only test you care about till we can render the 5 lines here and get it backfilled :wink:

If you have to drop the whole “prepared” vs “non prepared” stuff and always do “prepared” for now so be it.

cc @system

Don’t worry about Rails 2 for now.

1 Like

@sem Just to make myself sure. Since this is graph with commit data, you want me to include here commits for sequel and pg. Therefore it would have 3 lines (AR, sequel, pg) and graph would look something like this:

Maybe better place to display results for older Rails releases would be here?

Yes, absolutely, this is what I want you to work towards!

Possibly, let’s not worry about that for now, just for now worry about getting the graph with the 3 lines going and backfilling all the info. Work with @system on gaining access to prd.

1 Like

How are you going with this? Are you blocked on anything from our side?

@sam, I am not blocked on anything. Right now I am working on web to display graphs to compare. My first goal is to display sequel along with AR because sequel has been already added. Here’s wip PR on web.

Fantastic, focus on getting stuff merged and on the site as soon as you can. I prefer to have a slightly janky UI than wait weeks till we have the UI in perfect state.

1 Like

Little peek. This is with data from production imported in local. UI is not perfect and has some issues but before I get into fixing it I’ll make sure sequel benchmark graphs are being displayed.

Though I am not sure why sequel is not being displayed since it looks like been added :confused:

@system pls take a look at this one :arrow_down: :pray:

Cool!!! how far do you feel we are from having something on

Hey @sam,

PR is some time in reviewing process. According to me it’s ready to be merged. When it’s merged we could compare benchmarks on rubybench.

Jeremy configured sequel repo with webhook to send push events on rubybench, so next time when he pushes something we should have sequel repo with it’s benchmarks on rubybench :tada: