Skip to content

Instantly share code, notes, and snippets.

@mejibyte
Created August 29, 2011 19:12
Show Gist options
  • Save mejibyte/1179149 to your computer and use it in GitHub Desktop.
Save mejibyte/1179149 to your computer and use it in GitHub Desktop.
Benchmark to see if ActiveRecord::Base.include_root_in_json significantly affects performance
# encoding: utf-8
require 'benchmark'
require 'json'
json_normal = <<-EOS
[
{
"order": {
"checkin_id": 298486374,
"created_at": "2011-08-18T19:48:57Z",
"id": 298486374,
"read": null,
"status": "pending",
"store_id": 800752302,
"table_id": null,
"updated_at": "2011-08-18T19:48:57Z",
"user_id": null,
"selections": [
{
"id": 298486374,
"note": null,
"selected_options": null,
"product": {
"description": "Is it a copyright violation to use McDonalds on my test file? I hope they sue me.",
"id": 699613630,
"name": "Big Mac",
"price": "€3.40"
}
}
]
}
},
{
"order": {
"checkin_id": 980190962,
"created_at": "2011-08-18T19:48:57Z",
"id": 980190962,
"read": null,
"status": "pending",
"store_id": 800752302,
"table_id": null,
"updated_at": "2011-08-18T19:48:57Z",
"user_id": null,
"selections": [
{
"id": 980190962,
"note": null,
"selected_options": {
"Dessert": "Sundae",
"Sauce": "Extra hot"
},
"product": {
"description": "A delicious cheasy greasy pie",
"id": 266175090,
"name": "Pepperoni Pizza",
"price": "€10.99"
}
}
]
}
}
]
EOS
json_rootless = <<-EOS
[
{
"checkin_id": 298486374,
"created_at": "2011-08-18T19:48:57Z",
"id": 298486374,
"read": null,
"status": "pending",
"store_id": 800752302,
"table_id": null,
"updated_at": "2011-08-18T19:48:57Z",
"user_id": null,
"type": "order",
"selections": [
{
"id": 298486374,
"note": null,
"selected_options": null,
"product": {
"description": "Is it a copyright violation to use McDonalds on my test file? I hope they sue me.",
"id": 699613630,
"name": "Big Mac",
"price": "€3.40"
}
}
]
},
{
"checkin_id": 980190962,
"created_at": "2011-08-18T19:48:57Z",
"id": 980190962,
"read": null,
"status": "pending",
"store_id": 800752302,
"table_id": null,
"updated_at": "2011-08-18T19:48:57Z",
"user_id": null,
"type": "order",
"selections": [
{
"id": 980190962,
"note": null,
"selected_options": {
"Dessert": "Sundae",
"Sauce": "Extra hot"
},
"product": {
"description": "A delicious cheasy greasy pie",
"id": 266175090,
"name": "Pepperoni Pizza",
"price": "€10.99"
}
}
]
}
]
EOS
n = 100_000
Benchmark.bm(8) do |x|
x.report('normal') { n.times { JSON.parse(json_normal) } }
x.report('rootless') { n.times { JSON.parse(json_rootless) } }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment