Created
July 10, 2012 16:26
-
-
Save mrpunkin/3084461 to your computer and use it in GitHub Desktop.
Benchmark looping through response hashes for _content keys.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'json' | |
require 'benchmark' | |
user = JSON.parse(<<EOS) | |
{ "person": { "id": "19198543@N06", "nsid": "19198543@N06", "ispro": 1, "iconserver": "3280", "iconfarm": 4, "path_alias": "hopskipandjump", "gender": "M", "ignored": 0, "contact": 0, "friend": 0, "family": 0, "revcontact": 0, "revfriend": 0, "revfamily": 0, | |
"username": { "_content": "hopskipandjump" }, | |
"location": { "_content": "" }, | |
"timezone": { "label": "Central Time (US & Canada)", "offset": "-06:00" }, | |
"description": { "_content": "Family" }, | |
"photosurl": { "_content": "http:\/\/www.flickr.com\/photos\/hopskipandjump\/" }, | |
"profileurl": { "_content": "http:\/\/www.flickr.com\/people\/hopskipandjump\/" }, | |
"mobileurl": { "_content": "http:\/\/m.flickr.com\/photostream.gne?id=19153221" }, | |
"photos": { | |
"firstdatetaken": { "_content": "2007-12-21 18:39:20" }, | |
"firstdate": { "_content": "1221326610" }, | |
"count": { "_content": "12544" } } }, "stat": "ok" } | |
EOS | |
photo = JSON.parse(<<EOS) | |
{ "photo": { "id": "7524818098", "secret": "2715e61b6a", "server": "8008", "farm": 9, "dateuploaded": "1341719882", "isfavorite": 0, "license": 0, "safety_level": 0, "rotation": 0, "originalsecret": "d2e3d43c0b", "originalformat": "jpg", | |
"owner": { "nsid": "52946269@N00", "username": "mrpunkin", "realname": "Bryan", "location": "Vancouver, WA, USA", "iconserver": 38, "iconfarm": 1 }, | |
"title": { "_content": "St. Helens" }, | |
"description": { "_content": "" }, | |
"visibility": { "ispublic": 0, "isfriend": 1, "isfamily": 1 }, | |
"dates": { "posted": "1341719882", "taken": "2012-07-07 20:58:02", "takengranularity": 0, "lastupdate": "1341719886" }, | |
"permissions": { "permcomment": 1, "permaddmeta": 1 }, "views": 0, | |
"editability": { "cancomment": 1, "canaddmeta": 1 }, | |
"publiceditability": { "cancomment": 0, "canaddmeta": 0 }, | |
"usage": { "candownload": 1, "canblog": 1, "canprint": 1, "canshare": 1 }, | |
"comments": { "_content": 0 }, | |
"notes": { | |
"note": [ | |
] }, | |
"people": { "haspeople": 0 }, | |
"tags": { | |
"tag": [ | |
{ "id": "1027074-7524818098-60504812", "author": "52946269@N00", "raw": "instagram app", "_content": "instagramapp", "machine_tag": 0 }, | |
{ "id": "1027074-7524818098-1628", "author": "52946269@N00", "raw": "square", "_content": "square", "machine_tag": 0 }, | |
{ "id": "1027074-7524818098-14976", "author": "52946269@N00", "raw": "square format", "_content": "squareformat", "machine_tag": 0 }, | |
{ "id": "1027074-7524818098-34115330", "author": "52946269@N00", "raw": "iphoneography", "_content": "iphoneography", "machine_tag": 0 }, | |
{ "id": "1027074-7524818098-60643605", "author": "52946269@N00", "raw": "uploaded:by=instagram", "_content": "uploaded:by=instagram", "machine_tag": 1 }, | |
{ "id": "1027074-7524818098-348109", "author": "52946269@N00", "raw": "Amaro", "_content": "amaro", "machine_tag": 0 } | |
] }, | |
"urls": { | |
"url": [ | |
{ "type": "photopage", "_content": "http:\/\/www.flickr.com\/photos\/mrpunkin\/7524818098\/" } | |
] }, "media": "photo" }, "stat": "ok" } | |
EOS | |
## Current flickr.people.getInfo | |
def get_user_info(data) | |
%w[username realname location description profileurl mobileurl photosurl].each do |attribute| | |
data['person'][attribute] = data['person'][attribute]['_content'] rescue next | |
end | |
end | |
## Current flickr.photos.getInfo | |
def get_photo_info(data) | |
data['photo']['title'] = data['photo']['title']['_content'] | |
data['photo']['description'] = data['photo']['description']['_content'] | |
data['photo']['comments_count'] = data['photo'].delete('comments')['_content'] | |
data['photo']['dates']['uploaded'] = data['photo'].delete('dateuploaded') | |
data['photo']['tags'] = data['photo']['tags']['tag'] | |
end | |
## New cleanup_content method | |
def cleanup_content(data) | |
data.inject({}) do |h,(k,v)| | |
h[k] = v.is_a?(Hash) ? (v['_content'] || cleanup_content(v)) : v; h | |
end | |
end | |
Benchmark.bm(20) do |x| | |
x.report("Current user:"){ get_user_info(user) } | |
x.report("New user:"){ cleanup_content(user) } | |
x.report("Current photo:"){ get_photo_info(photo) } | |
x.report("New photo:"){ cleanup_content(photo) } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user system total real | |
Current user: 0.000000 0.000000 0.000000 ( 0.000063) | |
New user: 0.000000 0.000000 0.000000 ( 0.000037) | |
Current photo: 0.000000 0.000000 0.000000 ( 0.000015) | |
New photo: 0.000000 0.000000 0.000000 ( 0.000063) | |
user system total real | |
Current user: 0.000000 0.000000 0.000000 ( 0.000059) | |
New user: 0.000000 0.000000 0.000000 ( 0.000048) | |
Current photo: 0.000000 0.000000 0.000000 ( 0.000014) | |
New photo: 0.000000 0.000000 0.000000 ( 0.000063) | |
user system total real | |
Current user: 0.000000 0.000000 0.000000 ( 0.000059) | |
New user: 0.000000 0.000000 0.000000 ( 0.000038) | |
Current photo: 0.000000 0.000000 0.000000 ( 0.000015) | |
New photo: 0.000000 0.000000 0.000000 ( 0.000057) | |
user system total real | |
Current user: 0.000000 0.000000 0.000000 ( 0.000066) | |
New user: 0.000000 0.000000 0.000000 ( 0.000038) | |
Current photo: 0.000000 0.000000 0.000000 ( 0.000022) | |
New photo: 0.000000 0.000000 0.000000 ( 0.000063) | |
user system total real | |
Current user: 0.000000 0.000000 0.000000 ( 0.000070) | |
New user: 0.000000 0.000000 0.000000 ( 0.000044) | |
Current photo: 0.000000 0.000000 0.000000 ( 0.000014) | |
New photo: 0.000000 0.000000 0.000000 ( 0.000063) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment