Skip to content

Commit 3628b8d

Browse files
committed
save benchmark data as csv
1 parent a91a0c8 commit 3628b8d

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ group :test, :development do
1717
gem 'rspec', '~>3.11'
1818
# code coverage to monitor rspec tests
1919
gem 'simplecov'
20+
# save CSV files
21+
gem 'csv'
2022

2123
# Thread pool example file: thread_pool_spec.rb
2224
# https://github.com/mperham/connection_pool/tree/main

serpapi.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ information.'
2929
s.add_development_dependency 'rspec', '~>3.11'
3030
s.add_development_dependency 'yard', '~>0.9.28'
3131
s.add_development_dependency 'rubocop', '~>1.75.7'
32+
s.add_development_dependency 'csv'
3233

3334
end

spec/serpapi/client/benchmark/benchmark_spec.rb

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
require 'csv'
23

34
# This benchmark demonstrates that persistent connections are 2x quicker than regular HTTP connection.
45
#
@@ -14,47 +15,84 @@
1415
# ````
1516
# For an example of thread pool where the socket connection can be shared see: thread_pool_spec.rb
1617

18+
# Create unique filename with client_type, connection_type, and RUBY_VERSION
19+
timestamp = Time.now.strftime('%Y%m%d_%H%M%S_%L')
20+
CSV_FN = "serpapi_ruby_#{RUBY_VERSION.gsub('.', '_')}_#{timestamp}.csv"
21+
# Create the CSV file with headers if it doesn't exist
22+
CSV.open(CSV_FN, 'w') do |csv|
23+
csv << ['timestamp', 'client_type', 'connection_type', 'requests_count', 'total_time_seconds', 'avg_time_per_request', 'requests_per_second', 'ruby_version']
24+
end
25+
26+
def save(client_type, connection_type, requests_count, runtime)
27+
# Create CSV with headers and data
28+
CSV.open(CSV_FN, 'a') do |csv|
29+
# Add the data row
30+
avg_time = runtime / requests_count
31+
rps = requests_count / runtime
32+
33+
csv << [
34+
Time.now.iso8601,
35+
client_type,
36+
connection_type,
37+
requests_count,
38+
runtime.round(5),
39+
avg_time.round(5),
40+
rps.round(2),
41+
RUBY_VERSION
42+
]
43+
end
44+
45+
puts "update benchmark result to: #{File.absolute_path(CSV_FN)}"
46+
end
47+
1748
describe 'benchmark SerpApi client with/without persistent connection' do
1849

1950
# number of sequential requests
20-
n = 10
51+
n = 25
2152

2253
it 'regular get' do
54+
puts "start SerpApi regular get benchmark with #{n} requests"
2355
runtime = Benchmark.measure do
2456
client = SerpApi::Client.new(persistent: false, engine: 'google', api_key: ENV['SERPAPI_KEY'])
2557
results = n.times.map { |x| client.search(q: "coffee #{x}") }
2658
client.close
2759
end.total
28-
puts "serpapi client took #{runtime}s to completed #{n} requests without HTTP persistent connection"
60+
puts "regular get took #{runtime}s to completed #{n} requests without HTTP persistent connection"
61+
save('SerpApi', 'non-persistent', n, runtime)
2962
end
3063

3164
it 'keep alive' do
65+
puts "start SerpApi keep alive benchmark with #{n} requests"
3266
runtime = Benchmark.measure do
3367
client = SerpApi::Client.new(persistent: true, engine: 'google', api_key: ENV['SERPAPI_KEY'],)
3468
results = n.times.map { |x| client.search(q: "coffee #{n+x}") }
3569
client.close
3670
end.total
37-
puts "serpapi client took #{runtime}s to completed #{n} requests with HTTP persistent connection"
71+
puts "keep alive took #{runtime}s to completed #{n} requests with HTTP persistent connection"
72+
save('SerpApi', 'persistent', n, runtime)
3873
end
3974

4075
end
4176

4277
describe 'benchmark client using http.rb as a baseline' do
4378

44-
n = 10
79+
n = 25
4580
host = "http://api.icndb.com"
4681
endpoint = "/jokes/random"
4782

4883
it 'regular get' do
84+
puts "start HTTP.rb regular get benchmark with #{n} requests"
4985
runtime = Benchmark.measure do
5086
n.times do
5187
HTTP.get(host + endpoint)
5288
end
5389
end.total
5490
puts "http.rb took #{runtime}s to completed #{n} requests without HTTP persistent connection"
91+
save('HTTP.rb', 'non-persistent', n, runtime)
5592
end
5693

5794
it 'keep alive' do
95+
puts "start HTTP.rb keep alive benchmark with #{n} requests"
5896
runtime = Benchmark.measure do
5997
begin
6098
# create HTTP client with persistent connection to api.icndb.com:
@@ -67,7 +105,7 @@
67105
http.close if http
68106
end
69107
end.total
70-
puts "http.rb took #{runtime}s to completed #{n} requests with HTTP persistent connection"
71-
#expect(runtime).to be_within(0.1).of(0.02) # Adjust the tolerance as needed
108+
puts "keep alive took #{runtime}s to completed #{n} requests with HTTP persistent connection"
109+
save('HTTP.rb', 'persistent', n, runtime)
72110
end
73111
end

0 commit comments

Comments
 (0)