Skip to content

Commit

Permalink
Replace Benchmark with Integration Tests
Browse files Browse the repository at this point in the history
* remove benchmark. resolves #40
* add webmock/vcr based integration tests
* test that api gets correct number fo request
* test that api gets correct data in each request
  • Loading branch information
cassidycodes committed Oct 31, 2024
1 parent b24a1bb commit 953c2ea
Show file tree
Hide file tree
Showing 16 changed files with 550 additions and 173 deletions.
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.3.5
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ gemspec

group :development do
gem "bundler", "~> 2"
gem "debug", ">= 1.0.0"
gem "rack-contrib", "~> 2"
gem "rack-test", "~> 2"
gem "rake", "~> 13"
gem "rspec", "~> 3"
gem "sinatra", "~> 2"
gem "sinatra-contrib"
gem "standardrb", "~> 1"
gem "timecop", "~> 0.9"
gem "vcr", "~> 6"
gem "webmock", "~> 3"
end
60 changes: 60 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,53 @@ PATH
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
crack (1.0.0)
bigdecimal
rexml
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.5.1)
graphql (2.3.7)
base64
hashdiff (1.1.1)
io-console (0.7.2)
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.7.2)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
multi_json (1.15.0)
mustermann (2.0.2)
ruby2_keywords (~> 0.0.1)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
racc
psych (5.1.2)
stringio
public_suffix (6.0.1)
racc (1.8.1)
rack (2.2.10)
rack-contrib (2.5.0)
rack (< 4)
rack-protection (2.2.4)
rack
rack-test (2.1.0)
rack (>= 1.3)
rainbow (3.1.1)
rake (13.2.1)
rdoc (6.7.0)
psych (>= 4.0.0)
regexp_parser (2.9.2)
reline (0.5.10)
io-console (~> 0.5)
rexml (3.3.8)
rspec (3.13.0)
rspec-core (~> 3.13.0)
Expand Down Expand Up @@ -54,6 +85,18 @@ GEM
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
sinatra (2.2.4)
mustermann (~> 2.0)
rack (~> 2.2)
rack-protection (= 2.2.4)
tilt (~> 2.0)
sinatra-contrib (2.2.4)
multi_json
mustermann (~> 2.0)
rack-protection (= 2.2.4)
sinatra (= 2.2.4)
tilt (~> 2.0)
standard (1.40.1)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
Expand All @@ -68,18 +111,35 @@ GEM
rubocop-performance (~> 1.21.0)
standardrb (1.0.1)
standard
stringio (3.1.1)
tilt (2.4.0)
timecop (0.9.10)
unicode-display_width (2.6.0)
vcr (6.3.1)
base64
webmock (3.24.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
bundler (~> 2)
debug (>= 1.0.0)
graphql-hive!
rack-contrib (~> 2)
rack-test (~> 2)
rake (~> 13)
rspec (~> 3)
sinatra (~> 2)
sinatra-contrib
standardrb (~> 1)
timecop (~> 0.9)
vcr (~> 6)
webmock (~> 3)

BUNDLED WITH
2.5.15
71 changes: 42 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# GraphQL Hive: `graphql-ruby` integration
# GraphQL Hive: `graphql-ruby` integration
[![CI Suite](https://github.com/charlypoly/graphql-ruby-hive/actions/workflows/ci.yml/badge.svg)](https://github.com/charlypoly/graphql-ruby-hive/actions)
[![Gem Version](https://badge.fury.io/rb/graphql-hive.svg)](https://rubygems.org/gems/graphql-hive)

Expand Down Expand Up @@ -146,38 +146,53 @@ class MySchema < GraphQL::Schema
use(
GraphQL::Hive,
{
# mandatory
token: 'YOUR-TOKEN',

# optional
enabled: true, # enable/disable Hive Client
debug: false, # verbose logs
# token is the only required configuration value
token: 'YOUR-REGISTRY-TOKEN',
#
# The following are optional configuration values
#
# enable/disable Hive Client
enabled: true,
# verbose logs
debug: false,
# A custom logger
logger: MyLogger.new,
# endpoint and port of the Hive API. Change this if you are using a self-hosted Hive instance
endpoint: 'app.graphql-hive.com',
port: 80,
buffer_size: 50, # how many operations can be sent to hive in a single batch (AFTER sampling)

collect_usage: true, # report usage to Hive
# number of operations sent to hive in a batch (AFTER sampling)
buffer_size: 50,
# size of the queue used to send operations to the buffer before sampling
queue_size: 1000,
# report usage to hive
collect_usage: true,
# Usage sampling configurations
collect_usage_sampling: {
# optional members of `collect_usage_sampling`
sample_rate: 0.5, # % of operations reported
sampler: proc { |context| context.operation_name.includes?('someQuery') 1 : 0.5 }, # assign custom sampling rates (overrides `sampling rate`)
at_least_once: true, # sample every distinct operation at least once
key_generator: proc { |context| context.operation_name } # assign custom keys to distinguish between distinct operations
# % of operations recorded
sample_rate: 0.5,
# custom sampler to assign custom sampling rates
sampler: proc { |context| context.operation_name.includes?('someQuery') 1 : 0.5 },
# sample every distinct operation at least once
at_least_once: true,
# assign custom keys to distinguish between distinct operations
key_generator: proc { |context| context.operation_name }
},
report_schema: true, # publish schema to Hive
# publish schema to Hive
report_schema: true,
# mandatory if `report_schema: true`
reporting: {
reporting: {
# mandatory members of `reporting`
author: 'Author of the latest change',
commit: 'git sha or any identifier',
# optional members of `reporting
# optional members of `reporting
service_name: '',
service_url: '',
},

# pass an optional proc to client_info to help identify the client (ex: Apollo web app) that performed the query
client_info: proc { |context| { name: context.client_name, version: context.client_version } }
client_info: proc { |context|
{ name: context.client_name, version: context.client_version }
}
}
)

Expand All @@ -186,16 +201,14 @@ class MySchema < GraphQL::Schema
end
```

See default options for the optional parameters [here](https://github.com/charlypoly/graphql-ruby-hive/blob/01407d8fed80912a7006fee503bf2967fa20a79c/lib/graphql-hive.rb#L53).
See default options for the optional parameters [here](https://github.com/rperryng/graphql-ruby-hive/blob/master/lib/graphql-hive.rb#L31-L41).

<br/>

**A note on `buffer_size` and performances**

The `graphql-hive` usage reporter, responsible for sending the operations data to Hive, is running in a separate `Thread` to avoid any significant impact on your GraphQL API performances.

The performance overhead (with the default `buffer_size` option) is around 1% and [is constantly evaluated for new PR](https://github.com/charlypoly/graphql-ruby-hive/actions/workflows/benchmark.yml).

If your GraphQL API has a high RPM, we encourage you to increase the `buffer_size` value.

However, please note that a higher `buffer_size` value will introduce some peak of increase in memory consumption.
> [!Important]
> `buffer_size` and `queue_size` will affect memory consumption.
>
> `buffer_size` is the number of operations sent to Hive in a batch after operations have been sampled.
> `queue_size` is the size of the queue used to send operations to the buffer before sampling.
> Adjust these values according to your application's memory constraints and throughput.
> High throughput applications will need a larger `queue_size`.
8 changes: 0 additions & 8 deletions examples/simple-api/Gemfile

This file was deleted.

48 changes: 0 additions & 48 deletions examples/simple-api/Gemfile.lock

This file was deleted.

31 changes: 0 additions & 31 deletions examples/simple-api/app.rb

This file was deleted.

2 changes: 0 additions & 2 deletions examples/simple-api/config.ru

This file was deleted.

52 changes: 0 additions & 52 deletions examples/simple-api/schema.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/graphql-hive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require "graphql-hive/sampler"
require "graphql-hive/sampling/basic_sampler"
require "graphql-hive/sampling/dynamic_sampler"
require "graphql"

module GraphQL
# GraphQL Hive usage collector and schema reporter
Expand All @@ -36,6 +37,7 @@ class Hive < GraphQL::Tracing::PlatformTracing
read_operations: true,
report_schema: true,
buffer_size: 50,
queue_size: 1000,
logger: nil,
collect_usage_sampling: 1.0
}.freeze
Expand Down
Loading

0 comments on commit 953c2ea

Please sign in to comment.