Skip to content

Commit 2ff33a7

Browse files
authored
Merge pull request #3968 from rmosolgo/stack-trace-benchmark
Add a benchmark for stack trace depth
2 parents 11fe919 + 389779e commit 2ff33a7

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ namespace :bench do
122122
prepare_benchmark
123123
GraphQLBenchmark.profile_schema_memory_footprint
124124
end
125+
126+
desc "Check the depth of the stacktrace during execution"
127+
task :profile_stack_depth do
128+
prepare_benchmark
129+
GraphQLBenchmark.profile_stack_depth
130+
end
125131
end
126132

127133
namespace :test do

benchmark/run.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,79 @@ def self.profile_schema_memory_footprint
262262

263263
report.pretty_print
264264
end
265+
266+
class StackDepthSchema < GraphQL::Schema
267+
class Thing < GraphQL::Schema::Object
268+
field :thing, self do
269+
argument :lazy, Boolean, default_value: false
270+
end
271+
272+
def thing(lazy:)
273+
if lazy
274+
-> { :something }
275+
else
276+
:something
277+
end
278+
end
279+
280+
field :stack_trace_depth, Integer do
281+
argument :lazy, Boolean, default_value: false
282+
end
283+
284+
def stack_trace_depth(lazy:)
285+
get_depth = -> {
286+
graphql_caller = caller.select { |c| c.include?("graphql") }
287+
graphql_caller.size
288+
}
289+
290+
if lazy
291+
get_depth
292+
else
293+
get_depth.call
294+
end
295+
end
296+
end
297+
298+
class Query < GraphQL::Schema::Object
299+
field :thing, Thing
300+
301+
def thing
302+
:something
303+
end
304+
end
305+
306+
query(Query)
307+
lazy_resolve(Proc, :call)
308+
end
309+
310+
def self.profile_stack_depth
311+
query_str = <<-GRAPHQL
312+
query($lazyThing: Boolean!, $lazyStackTrace: Boolean!) {
313+
thing {
314+
thing(lazy: $lazyThing) {
315+
thing(lazy: $lazyThing) {
316+
thing(lazy: $lazyThing) {
317+
thing(lazy: $lazyThing) {
318+
stackTraceDepth(lazy: $lazyStackTrace)
319+
}
320+
}
321+
}
322+
}
323+
}
324+
}
325+
GRAPHQL
326+
327+
very_lazy_res = StackDepthSchema.execute(query_str, variables: { lazyThing: true, lazyStackTrace: true })
328+
lazy_res = StackDepthSchema.execute(query_str, variables: { lazyThing: true, lazyStackTrace: false })
329+
eager_res = StackDepthSchema.execute(query_str, variables: { lazyThing: false, lazyStackTrace: false })
330+
get_depth = ->(result) { result["data"]["thing"]["thing"]["thing"]["thing"]["thing"]["stackTraceDepth"] }
331+
332+
puts <<~RESULT
333+
Result Depth
334+
---------------------
335+
Eager #{get_depth.call(eager_res)}
336+
Lazy #{get_depth.call(lazy_res)}
337+
Very Lazy #{get_depth.call(very_lazy_res)}
338+
RESULT
339+
end
265340
end

0 commit comments

Comments
 (0)