Skip to content

Instantly share code, notes, and snippets.

@usergenic
Created October 13, 2011 23:21
Show Gist options
  • Select an option

  • Save usergenic/1285814 to your computer and use it in GitHub Desktop.

Select an option

Save usergenic/1285814 to your computer and use it in GitHub Desktop.
FQL vs Graph
#!/usr/bin/env ruby
require 'rubygems'
require 'koala'
require 'benchmark'
require 'optparse'
require 'yaml'
require 'json'
@session_token = nil
@count = 10
@notes = []
optparse = OptionParser.new do |options|
options.banner = "Usage: #{$0} [options]"
options.on "-c", "--count COUNT", "How many iterations to run for benchmark" do |@count| end
options.on "-t", "--token TOKEN", "Facebook Application Session Token" do |@session_token| end
options.on "-i", "--ids ID_LIST", "Comma-separated list of FB page_ids" do |@ids| end
options.on "-b", "--begin BEGINTIME", "Unix timestamp for beginning of report time" do |@begin_time| end
options.on "-e", "--end ENDTIME", "Unix timestamp for ending of report time" do |@end_time| end
options.on "-h", "--help"
end
optparse.parse!
raise "No --ids option provided." unless @ids
raise "No --token option provided." unless @session_token
raise "No --begin option provided." unless @begin_time
raise "No --end option provided." unless @end_time
@count = @count.to_i
@ids = @ids.to_s.split(',').map { |x| x.strip.to_i }
api = Koala::Facebook::API.new(@session_token)
posts = nil
insights = nil
fql = nil
class BatchBatch
MAXBATCHSIZE = 50
def initialize(api)
@api = api
@queries = []
end
def method_missing(method_id, *arguments)
@queries << [method_id, arguments]
end
def results
@results = []
until @queries.empty?
query_count = 1
@results.concat(@api.batch do |batch|
until query_count > MAXBATCHSIZE or @queries.empty?
query_method, query_args = @queries.shift
batch.send(query_method, *query_args)
query_count += 1
end
end.to_a)
end
@results
end
def self.for(api, &block)
bb = BatchBatch.new(api)
yield bb
bb.results
end
end
Benchmark.bm(60) do |bm|
bm.report "'posts' from fql for #{@ids.size} pages" do
@count.times do |n|
posts = BatchBatch.for(api) do |batch|
@ids.each do |id|
fql = <<-end
SELECT post_id, message
FROM stream
WHERE source_id = #{id}
end
batch.graph_call("method/fql.query", {:query => fql}, "post")
end
end
posts.inspect
end
end
@notes << "'posts' from fql batch for #{@ids.size} pages returned #{posts.flatten.size} posts."
bm.report "'posts' from graph batch for #{@ids.size} pages" do
@count.times do |n|
posts = BatchBatch.for(api) do |batch|
@ids.each do |id|
batch.get_connections(id, 'posts', 'fields' => %w[from message type created_time], 'since' => @begin_time, 'until' => @end_time)
end
end
posts.inspect
end
end
@notes << "'posts' from graph batch for #{@ids.size} pages returned #{posts.flatten.size} posts."
all_posts = posts.flatten
bm.report "'posts' from graph non-batched for #{@ids.size} pages" do
@ids.each do |id|
@count.times do |n|
posts = api.get_connections(id, 'posts', 'fields' => %w[from message type created_time], 'since' => @begin_time, 'until' => @end_time)
posts.inspect
end
@notes << "'posts' from graph non-batched for #{id} returned #{posts.flatten.size} posts."
end
end
post_ids = all_posts.flatten.map { |post| post['id'] }
bm.report "'insights' from fql single query" do
@count.times do |n|
fql = <<-end
SELECT object_id, value, period, end_time
FROM insights
WHERE object_id IN (#{ post_ids.map{ |id| id.to_s.inspect }.join(',') })
AND metric IN (
'post_impressions_organic_unique',
'post_impressions_unique',
'post_engaged_users',
'post_story_adds_unique'
)
AND period = 0
end
insights = api.fql_query(fql)
insights.inspect
end
end
@notes << "'insights' from fql returned #{insights.flatten.size} data points."
bm.report "'insights' from graph batch for #{post_ids.size} posts" do
@count.times do |n|
insights = BatchBatch.for(api) do |batch|
post_ids.each do |post_id|
batch.get_connections(post_id, 'insights/post_impressions_organic_unique/lifetime')
batch.get_connections(post_id, 'insights/post_impressions_unique/lifetime')
batch.get_connections(post_id, 'insights/post_engaged_users/lifetime')
batch.get_connections(post_id, 'insights/post_story_adds_unique/lifetime')
end
end
insights.inspect
end
end
@notes << "'insights' from graph batch for #{post_ids.size} posts returned #{insights.flatten.size} data points."
end
unless @notes.empty?
puts
puts "NOTES"
puts @notes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment