Skip to content

Commit cb0b857

Browse files
committed
Add a benchmark for analysis
1 parent 7f599f4 commit cb0b857

2 files changed

Lines changed: 83 additions & 13 deletions

File tree

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ namespace :bench do
134134
prepare_benchmark
135135
GraphQLBenchmark.profile_large_introspection
136136
end
137+
138+
desc "Run analysis on a big query"
139+
task :profile_large_analysis do
140+
prepare_benchmark
141+
GraphQLBenchmark.profile_large_analysis
142+
end
137143
end
138144

139145
namespace :test do

benchmark/run.rb

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,45 @@ def self.profile
7272
StackProf::Report.new(result).print_text
7373
end
7474

75-
def self.profile_large_introspection
76-
schema = Class.new(GraphQL::Schema) do
77-
query_t = Class.new(GraphQL::Schema::Object) do
78-
graphql_name("Query")
79-
100.times do |n|
80-
obj_t = Class.new(GraphQL::Schema::Object) do
81-
graphql_name("Object#{n}")
82-
20.times do |n2|
83-
field :"field#{n2}", String do
84-
argument :arg, String
85-
end
75+
SILLY_LARGE_SCHEMA = Class.new(GraphQL::Schema) do
76+
query_t = Class.new(GraphQL::Schema::Object) do
77+
graphql_name("Query")
78+
int_ts = 5.times.map do |i|
79+
int_t = Module.new do
80+
include GraphQL::Schema::Interface
81+
graphql_name "Interface#{i}"
82+
5.times do |n2|
83+
field :"field#{n2}", String do
84+
argument :arg, String
85+
end
86+
end
87+
end
88+
field :"int_field_#{i}", int_t
89+
int_t
90+
end
91+
92+
100.times do |n|
93+
obj_t = Class.new(GraphQL::Schema::Object) do
94+
graphql_name("Object#{n}")
95+
implements(*int_ts)
96+
20.times do |n2|
97+
field :"field#{n2}", String do
98+
argument :arg, String
8699
end
100+
87101
end
88-
field :"rootfield#{n}", obj_t
102+
field :self_field, self
103+
field :int_0_field, int_ts[0]
89104
end
105+
106+
field :"rootfield#{n}", obj_t
90107
end
91-
query(query_t)
92108
end
109+
query(query_t)
110+
end
93111

112+
def self.profile_large_introspection
113+
schema = SILLY_LARGE_SCHEMA
94114
Benchmark.ips do |x|
95115
x.report("Run large introspection") {
96116
schema.to_json
@@ -109,6 +129,50 @@ def self.profile_large_introspection
109129
report.pretty_print
110130
end
111131

132+
def self.profile_large_analysis
133+
query_str = "query {\n".dup
134+
fragments = []
135+
5.times do |n|
136+
query_str << " intField#{n} { "
137+
20.times do |o|
138+
query_str << "...Obj#{o}Fields "
139+
end
140+
query_str << "}\n"
141+
end
142+
query_str << "}"
143+
144+
20.times do |o|
145+
query_str << "fragment Obj#{o}Fields on Object#{o} { "
146+
20.times do |f|
147+
query_str << " field#{f}(arg: \"a\")\n"
148+
end
149+
query_str << " selfField { selfField { selfField { __typename } } }\n"
150+
query_str << " int0Field { ...Int0Fields }"
151+
query_str << "}\n"
152+
end
153+
query_str << "fragment Int0Fields on Interface0 { __typename }"
154+
query = GraphQL::Query.new(SILLY_LARGE_SCHEMA, query_str)
155+
analyzers = [GraphQL::Analysis::AST::FieldUsage]
156+
multiplex_analyzers = []
157+
Benchmark.ips do |x|
158+
x.report("Running introspection") {
159+
GraphQL::Analysis::AST.analyze_query(query, analyzers)
160+
}
161+
end
162+
163+
result = StackProf.run(mode: :wall, interval: 1) do
164+
GraphQL::Analysis::AST.analyze_query(query, analyzers)
165+
end
166+
167+
StackProf::Report.new(result).print_text
168+
169+
report = MemoryProfiler.report do
170+
GraphQL::Analysis::AST.analyze_query(query, analyzers)
171+
end
172+
puts "\n\n"
173+
report.pretty_print
174+
end
175+
112176
# Adapted from https://github.com/rmosolgo/graphql-ruby/issues/861
113177
def self.profile_large_result
114178
schema = ProfileLargeResult::Schema

0 commit comments

Comments
 (0)