Skip to content

Instantly share code, notes, and snippets.

@i-tsvetkov
Last active May 22, 2019 11:34
Show Gist options
  • Select an option

  • Save i-tsvetkov/78e0af07be27d9791fd54be3e6fc6c45 to your computer and use it in GitHub Desktop.

Select an option

Save i-tsvetkov/78e0af07be27d9791fd54be3e6fc6c45 to your computer and use it in GitHub Desktop.
require "allocation_tracer"
require "graphql"
require "json"
schema_defn = <<-GRAPHQL
schema {
query: Query
}
type Query {
posts(limit: Int!): [Post]
}
type Post {
id: Int!
user: User!
comments: [Comment!]
stars: Int!
}
type Comment {
id: Int!
user: User!
length: Int!
stars: Int!
}
type User {
id: Int!
age: Int!
}
GRAPHQL
class Query
def self.posts(obj, args, ctx)
args.limit.times.map do
Post.new
end
end
end
class Post
def self.id(obj, args, ctx)
rand(1..2**31)
end
def self.user(obj, args, ctx)
User.new
end
def self.comments(obj, args, ctx)
rand(1..42).times.map do
Comment.new
end
end
def self.stars(obj, args, ctx)
rand(1..2**31)
end
end
class User
def self.id(obj, args, ctx)
rand(1..2**31)
end
def self.age(obj, args, ctx)
rand(1..2**31)
end
end
class Comment
def self.id(obj, args, ctx)
rand(1..2**31)
end
def self.user(obj, args, ctx)
User.new
end
def self.length(obj, args, ctx)
rand(1..2**31)
end
def self.stars(obj, args, ctx)
rand(1..2**31)
end
end
module ExecuteGraphQLByConvention
module_function
# Find a Ruby module corresponding to `type`,
# then call its method corresponding to `field`.
def call(type, field, obj, args, ctx)
type_module = Object.const_get(type.name)
type_module.public_send(field.name, obj, args, ctx)
end
end
schema = GraphQL::Schema.from_definition(schema_defn, default_resolve: ExecuteGraphQLByConvention)
query = <<-GRAPHQL
query {
posts(limit: 10000) {
id
user {
id
age
}
comments {
id
user {
id
age
}
length
stars
}
stars
}
}
GRAPHQL
ObjectSpace::AllocationTracer.setup(%i{ path line type })
trace = ObjectSpace::AllocationTracer.trace do
schema.execute(query, variables: { foo: 42 }, context: { bar: 42 })
end
trace = trace.sort_by{ |info, counts| counts.first }
trace.reverse!
puts JSON.pretty_generate(trace.first(10))
Command being timed: "ruby allocation_test.rb"
User time (seconds): 37.32
System time (seconds): 0.39
Percent of CPU this job got: 99%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:37.72
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 996616
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 285519
Voluntary context switches: 3
Involuntary context switches: 202
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment