-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse json body with 'Oj' gem to improve parsing time
'Oj'(https://github.com/ohler55/oj) is faster than the built in Ruby json gem. Oj achieves this by implementing the encoding and decoding of JSON in C, as a C extension to Ruby. I added some benchmarking tests and example json files to compare how both parsing implementations behave when parsing contents of different complexities and sizes.
- Loading branch information
Showing
9 changed files
with
20,539 additions
and
11 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require_relative '../lib/grac' | ||
require 'benchmark/ips' | ||
require 'json' | ||
require 'oj' | ||
|
||
TYPHOEUS_RESPONSE = Struct.new(:body) | ||
|
||
files = Dir.glob("./benchmarking/test_files/*.json").each_with_object({}) do |file, hash_object| | ||
hash_object[File.basename(file)] = File.read(file) | ||
end | ||
|
||
Oj.default_options = { mode: :compat, use_to_json: true } | ||
|
||
# Configure the number of seconds used during | ||
# the warmup phase (default 2) and calculation phase (default 5) | ||
benchmark_config = { time: 30 } | ||
|
||
files.each do |filename, file_contents| | ||
typhoeus_response = TYPHOEUS_RESPONSE.new(file_contents) | ||
grac_response = Grac::Response.new(typhoeus_response) | ||
result = JSON.parse(file_contents) == grac_response.parsed_json | ||
|
||
puts result | ||
raise "#{filename} Not equal" unless result | ||
|
||
Benchmark.ips do |x| | ||
x.config(benchmark_config) | ||
|
||
x.report("JSON.parse: #{filename}") do | ||
JSON.parse(file_contents) | ||
end | ||
|
||
x.report("OJ.load: #{filename}") do | ||
grac_response.parsed_json | ||
end | ||
|
||
x.compare! | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
{ | ||
"url": "https://api.github.com/repos/octocat/Hello-World/compare/master...topic", | ||
"html_url": "https://github.com/octocat/Hello-World/compare/master...topic", | ||
"permalink_url": "https://github.com/octocat/Hello-World/compare/octocat:bbcd538c8e72b8c175046e27cc8f907076331401...octocat:0328041d1152db8ae77652d1618a02e57f745f17", | ||
"diff_url": "https://github.com/octocat/Hello-World/compare/master...topic.diff", | ||
"patch_url": "https://github.com/octocat/Hello-World/compare/master...topic.patch", | ||
"base_commit": { | ||
"url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"node_id": "MDY6Q29tbWl0NmRjYjA5YjViNTc4NzVmMzM0ZjYxYWViZWQ2OTVlMmU0MTkzZGI1ZQ==", | ||
"html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments", | ||
"commit": { | ||
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"author": { | ||
"name": "Monalisa Octocat", | ||
"email": "[email protected]", | ||
"date": "2011-04-14T16:00:49Z" | ||
}, | ||
"committer": { | ||
"name": "Monalisa Octocat", | ||
"email": "[email protected]", | ||
"date": "2011-04-14T16:00:49Z" | ||
}, | ||
"message": "Fix all the bugs", | ||
"tree": { | ||
"url": "https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" | ||
}, | ||
"comment_count": 0, | ||
"verification": { | ||
"verified": false, | ||
"reason": "unsigned", | ||
"signature": null, | ||
"payload": null | ||
} | ||
}, | ||
"author": { | ||
"login": "octocat", | ||
"id": 1, | ||
"node_id": "MDQ6VXNlcjE=", | ||
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/octocat", | ||
"html_url": "https://github.com/octocat", | ||
"followers_url": "https://api.github.com/users/octocat/followers", | ||
"following_url": "https://api.github.com/users/octocat/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions", | ||
"organizations_url": "https://api.github.com/users/octocat/orgs", | ||
"repos_url": "https://api.github.com/users/octocat/repos", | ||
"events_url": "https://api.github.com/users/octocat/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/octocat/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"committer": { | ||
"login": "octocat", | ||
"id": 1, | ||
"node_id": "MDQ6VXNlcjE=", | ||
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/octocat", | ||
"html_url": "https://github.com/octocat", | ||
"followers_url": "https://api.github.com/users/octocat/followers", | ||
"following_url": "https://api.github.com/users/octocat/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions", | ||
"organizations_url": "https://api.github.com/users/octocat/orgs", | ||
"repos_url": "https://api.github.com/users/octocat/repos", | ||
"events_url": "https://api.github.com/users/octocat/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/octocat/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"parents": [ | ||
{ | ||
"url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" | ||
} | ||
] | ||
}, | ||
"merge_base_commit": { | ||
"url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"node_id": "MDY6Q29tbWl0NmRjYjA5YjViNTc4NzVmMzM0ZjYxYWViZWQ2OTVlMmU0MTkzZGI1ZQ==", | ||
"html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments", | ||
"commit": { | ||
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"author": { | ||
"name": "Monalisa Octocat", | ||
"email": "[email protected]", | ||
"date": "2011-04-14T16:00:49Z" | ||
}, | ||
"committer": { | ||
"name": "Monalisa Octocat", | ||
"email": "[email protected]", | ||
"date": "2011-04-14T16:00:49Z" | ||
}, | ||
"message": "Fix all the bugs", | ||
"tree": { | ||
"url": "https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" | ||
}, | ||
"comment_count": 0, | ||
"verification": { | ||
"verified": false, | ||
"reason": "unsigned", | ||
"signature": null, | ||
"payload": null | ||
} | ||
}, | ||
"author": { | ||
"login": "octocat", | ||
"id": 1, | ||
"node_id": "MDQ6VXNlcjE=", | ||
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/octocat", | ||
"html_url": "https://github.com/octocat", | ||
"followers_url": "https://api.github.com/users/octocat/followers", | ||
"following_url": "https://api.github.com/users/octocat/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions", | ||
"organizations_url": "https://api.github.com/users/octocat/orgs", | ||
"repos_url": "https://api.github.com/users/octocat/repos", | ||
"events_url": "https://api.github.com/users/octocat/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/octocat/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"committer": { | ||
"login": "octocat", | ||
"id": 1, | ||
"node_id": "MDQ6VXNlcjE=", | ||
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/octocat", | ||
"html_url": "https://github.com/octocat", | ||
"followers_url": "https://api.github.com/users/octocat/followers", | ||
"following_url": "https://api.github.com/users/octocat/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions", | ||
"organizations_url": "https://api.github.com/users/octocat/orgs", | ||
"repos_url": "https://api.github.com/users/octocat/repos", | ||
"events_url": "https://api.github.com/users/octocat/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/octocat/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"parents": [ | ||
{ | ||
"url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" | ||
} | ||
] | ||
}, | ||
"status": "behind", | ||
"ahead_by": 1, | ||
"behind_by": 2, | ||
"total_commits": 1, | ||
"commits": [ | ||
{ | ||
"url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"node_id": "MDY6Q29tbWl0NmRjYjA5YjViNTc4NzVmMzM0ZjYxYWViZWQ2OTVlMmU0MTkzZGI1ZQ==", | ||
"html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments", | ||
"commit": { | ||
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"author": { | ||
"name": "Monalisa Octocat", | ||
"email": "[email protected]", | ||
"date": "2011-04-14T16:00:49Z" | ||
}, | ||
"committer": { | ||
"name": "Monalisa Octocat", | ||
"email": "[email protected]", | ||
"date": "2011-04-14T16:00:49Z" | ||
}, | ||
"message": "Fix all the bugs", | ||
"tree": { | ||
"url": "https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" | ||
}, | ||
"comment_count": 0, | ||
"verification": { | ||
"verified": false, | ||
"reason": "unsigned", | ||
"signature": null, | ||
"payload": null | ||
} | ||
}, | ||
"author": { | ||
"login": "octocat", | ||
"id": 1, | ||
"node_id": "MDQ6VXNlcjE=", | ||
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/octocat", | ||
"html_url": "https://github.com/octocat", | ||
"followers_url": "https://api.github.com/users/octocat/followers", | ||
"following_url": "https://api.github.com/users/octocat/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions", | ||
"organizations_url": "https://api.github.com/users/octocat/orgs", | ||
"repos_url": "https://api.github.com/users/octocat/repos", | ||
"events_url": "https://api.github.com/users/octocat/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/octocat/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"committer": { | ||
"login": "octocat", | ||
"id": 1, | ||
"node_id": "MDQ6VXNlcjE=", | ||
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/octocat", | ||
"html_url": "https://github.com/octocat", | ||
"followers_url": "https://api.github.com/users/octocat/followers", | ||
"following_url": "https://api.github.com/users/octocat/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions", | ||
"organizations_url": "https://api.github.com/users/octocat/orgs", | ||
"repos_url": "https://api.github.com/users/octocat/repos", | ||
"events_url": "https://api.github.com/users/octocat/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/octocat/received_events", | ||
"type": "User", | ||
"site_admin": false | ||
}, | ||
"parents": [ | ||
{ | ||
"url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" | ||
} | ||
] | ||
} | ||
], | ||
"files": [ | ||
{ | ||
"sha": "bbcd538c8e72b8c175046e27cc8f907076331401", | ||
"filename": "file1.txt", | ||
"status": "added", | ||
"additions": 103, | ||
"deletions": 21, | ||
"changes": 124, | ||
"blob_url": "https://github.com/octocat/Hello-World/blob/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt", | ||
"raw_url": "https://github.com/octocat/Hello-World/raw/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt", | ||
"contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/file1.txt?ref=6dcb09b5b57875f334f61aebed695e2e4193db5e", | ||
"patch": "@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test" | ||
} | ||
] | ||
} |
Oops, something went wrong.