forked from Lyrinox-Technologies/Source-License
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_api.rb
More file actions
592 lines (488 loc) · 17.4 KB
/
Copy pathbenchmark_api.rb
File metadata and controls
592 lines (488 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#!/usr/bin/env ruby
# frozen_string_literal: true
# Source License API Benchmark Script
# Tests fiber-enabled API endpoints for performance comparison
require 'net/http'
require 'json'
require 'uri'
require 'benchmark'
require 'concurrent-ruby'
require 'optparse'
class SourceLicenseAPIBenchmark
DEFAULT_HOST = 'localhost'
DEFAULT_PORT = 4567
DEFAULT_THREADS = 10
DEFAULT_REQUESTS = 100
def initialize(options = {})
@host = options[:host] || DEFAULT_HOST
@port = options[:port] || DEFAULT_PORT
@threads = options[:threads] || DEFAULT_THREADS
@requests = options[:requests] || DEFAULT_REQUESTS
@verbose = options[:verbose] || false
@base_url = "http://#{@host}:#{@port}"
@results = {}
@errors = []
end
def run_all_benchmarks
puts '🚀 Starting Source License API Benchmarks'
puts '=' * 60
puts "Host: #{@host}:#{@port}"
puts "Threads: #{@threads}"
puts "Requests per endpoint: #{@requests}"
puts '=' * 60
puts
# Check server availability first
unless server_available?
puts "❌ Server not available at #{@base_url}"
puts 'Please start the server first with: ruby app.rb'
exit 1
end
puts '✅ Server is available'
puts
# Run individual endpoint benchmarks
benchmark_products_endpoint
benchmark_license_validation
benchmark_concurrent_license_validation
benchmark_order_creation_simulation
benchmark_mixed_workload
# Print summary
print_summary
end
private
def server_available?
uri = URI("#{@base_url}/")
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 5
http.read_timeout = 5
http.get(uri.path)
true
rescue StandardError
false
end
def benchmark_products_endpoint
puts '📦 Benchmarking Products Endpoint'
puts '-' * 40
endpoint = '/api/products'
# Single request test
single_time = benchmark_single_request(endpoint)
# Concurrent requests test
concurrent_time = benchmark_concurrent_requests(endpoint, @requests)
@results[:products] = {
single_request_time: single_time,
concurrent_requests_time: concurrent_time,
requests_per_second: @requests / concurrent_time,
endpoint: endpoint,
}
puts "Single request: #{format('%.3f', single_time * 1000)}ms"
puts "#{@requests} concurrent requests: #{format('%.3f', concurrent_time)}s"
puts "Requests/second: #{format('%.2f', @results[:products][:requests_per_second])}"
puts
end
def benchmark_license_validation
puts '🔑 Benchmarking License Validation'
puts '-' * 40
# Create some test license keys (these will likely return 404, but we're testing throughput)
test_keys = generate_test_license_keys(10)
results = []
test_keys.each do |key|
endpoint = "/api/license/#{key}/validate"
time = benchmark_single_request(endpoint, expected_status: [200, 404])
results << time
end
avg_time = results.sum / results.length
@results[:license_validation] = {
average_time: avg_time,
endpoint: '/api/license/[KEY]/validate',
}
puts "Average validation time: #{format('%.3f', avg_time * 1000)}ms"
puts "Tested #{test_keys.length} different license keys"
puts
end
def benchmark_concurrent_license_validation
puts '🔄 Benchmarking Concurrent License Validation'
puts '-' * 40
test_keys = generate_test_license_keys(@requests)
start_time = Time.now
# Use concurrent-ruby for true parallelism
pool = Concurrent::ThreadPoolExecutor.new(
min_threads: @threads,
max_threads: @threads,
max_queue: @requests
)
futures = test_keys.map do |key|
Concurrent::Future.execute(executor: pool) do
endpoint = "/api/license/#{key}/validate"
make_request(endpoint, expected_status: [200, 404])
end
end
# Wait for all requests to complete
futures.each(&:value!)
total_time = Time.now - start_time
pool.shutdown
pool.wait_for_termination(10)
@results[:concurrent_license_validation] = {
total_time: total_time,
requests_per_second: @requests / total_time,
concurrent_requests: @requests,
}
puts "#{@requests} concurrent validations: #{format('%.3f', total_time)}s"
puts "Requests/second: #{format('%.2f', @results[:concurrent_license_validation][:requests_per_second])}"
puts
end
def benchmark_order_creation_simulation
puts '🛒 Benchmarking Order Creation (Simulation)'
puts '-' * 40
# Test a simple validation request first to see if order endpoint is responsive
test_single_time = nil
begin
# Use a very short timeout for the test request
uri = URI("#{@base_url}/api/orders")
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 2
http.read_timeout = 2
test_req = Net::HTTP::Post.new(uri)
test_req['Content-Type'] = 'application/json'
test_req.body = '{"test": "quick"}' # Invalid but should get quick response
start_time = Time.now
response = http.request(test_req)
test_single_time = Time.now - start_time
puts "Order endpoint test response: #{response.code} (#{format('%.3f', test_single_time * 1000)}ms)"
rescue Net::OpenTimeout, Net::ReadTimeout, StandardError => e
puts "⚠️ Order endpoint appears to be slow or hanging (#{e.class.name})"
puts ' Skipping order creation benchmark to avoid hanging'
puts ' This suggests the fiber implementation in order creation may need optimization'
puts
return
end
# If the test request took too long, skip the full benchmark
if test_single_time && test_single_time > 5.0
puts "⚠️ Order endpoint is too slow (#{format('%.3f', test_single_time)}s for test request)"
puts ' Skipping full benchmark to avoid hanging'
puts
return
end
# Simulate order creation requests with reduced count and shorter timeouts
order_data = {
customer: {
email: 'benchmark@example.com',
name: 'Benchmark User',
},
items: [
{
productId: 'test-product-1',
quantity: 1,
},
],
payment_method: 'stripe',
amount: 29.99,
currency: 'USD',
}
endpoint = '/api/orders'
reduced_requests = [@requests / 10, 5].max # Use fewer requests to avoid hanging
puts "Using #{reduced_requests} requests (reduced from #{@requests} due to potential slow endpoint)"
# Single request with timeout
single_time = benchmark_single_request_with_timeout(endpoint,
method: 'POST',
data: order_data,
expected_status: [400, 404, 500],
timeout: 10)
return unless single_time # Skip if single request failed
# Multiple requests with timeout protection
times = []
success_count = 0
reduced_requests.times do |i|
puts " Request #{i + 1}/#{reduced_requests}..." if @verbose
time = benchmark_single_request_with_timeout(endpoint,
method: 'POST',
data: order_data,
expected_status: [400, 404, 500],
timeout: 10)
if time
times << time
success_count += 1
else
puts " Request #{i + 1} timed out or failed"
break if success_count < i / 2 # Stop if too many failures
end
end
return if times.empty?
avg_time = times.sum / times.length
total_time = times.sum
@results[:order_creation] = {
single_request_time: single_time,
average_time: avg_time,
total_time: total_time,
requests_per_second: success_count / total_time,
success_count: success_count,
attempted_requests: reduced_requests,
}
puts "Single order request: #{format('%.3f', single_time * 1000)}ms"
puts "Successful requests: #{success_count}/#{reduced_requests}"
puts "Average time: #{format('%.3f', avg_time * 1000)}ms"
puts "Requests/second: #{format('%.2f', @results[:order_creation][:requests_per_second])}"
puts
end
def benchmark_mixed_workload
puts '🔀 Benchmarking Mixed API Workload'
puts '-' * 40
# Mix of different endpoints to simulate real usage
endpoints = [
{ path: '/api/products', method: 'GET', weight: 40 },
{ path: '/api/license/TEST-1234-5678-9012/validate', method: 'GET', weight: 30 },
{ path: '/api/orders/123', method: 'GET', weight: 20 },
{ path: '/api/license/TEST-9876-5432-1098/validate', method: 'GET', weight: 10 },
]
# Create weighted request list
requests = []
endpoints.each do |endpoint|
count = (@requests * endpoint[:weight] / 100.0).to_i
count.times { requests << endpoint }
end
# Randomize order
requests.shuffle!
start_time = Time.now
# Execute mixed workload concurrently
pool = Concurrent::ThreadPoolExecutor.new(
min_threads: @threads,
max_threads: @threads,
max_queue: requests.length
)
futures = requests.map do |req|
Concurrent::Future.execute(executor: pool) do
make_request(req[:path], method: req[:method], expected_status: [200, 404, 500])
end
end
futures.each(&:value!)
total_time = Time.now - start_time
pool.shutdown
pool.wait_for_termination(10)
@results[:mixed_workload] = {
total_requests: requests.length,
total_time: total_time,
requests_per_second: requests.length / total_time,
endpoint_mix: endpoints,
}
puts "Mixed workload (#{requests.length} requests): #{format('%.3f', total_time)}s"
puts "Requests/second: #{format('%.2f', @results[:mixed_workload][:requests_per_second])}"
puts 'Endpoint distribution:'
endpoints.each do |ep|
puts " - #{ep[:method]} #{ep[:path]}: #{ep[:weight]}%"
end
puts
end
def benchmark_single_request(endpoint, method: 'GET', data: nil, expected_status: [200])
start_time = Time.now
make_request(endpoint, method: method, data: data, expected_status: expected_status)
Time.now - start_time
end
def benchmark_single_request_with_timeout(endpoint, method: 'GET', data: nil, expected_status: [200], timeout: 10)
start_time = Time.now
response = make_request_with_timeout(endpoint, method: method, data: data, expected_status: expected_status,
timeout: timeout)
return nil unless response
Time.now - start_time
end
def make_request_with_timeout(endpoint, method: 'GET', data: nil, expected_status: [200], timeout: 10)
uri = URI("#{@base_url}#{endpoint}")
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = [timeout / 2, 5].min
http.read_timeout = timeout
request = case method.upcase
when 'GET'
Net::HTTP::Get.new(uri)
when 'POST'
req = Net::HTTP::Post.new(uri)
req['Content-Type'] = 'application/json'
req.body = data.to_json if data
req
else
raise "Unsupported method: #{method}"
end
begin
response = http.request(request)
unless expected_status.include?(response.code.to_i)
@errors << {
endpoint: endpoint,
method: method,
expected: expected_status,
actual: response.code.to_i,
message: response.body&.slice(0, 200),
}
end
puts "#{method} #{endpoint} -> #{response.code}" if @verbose
response
rescue Net::OpenTimeout, Net::ReadTimeout => e
puts "⚠️ Request timeout: #{method} #{endpoint}" if @verbose
@errors << {
endpoint: endpoint,
method: method,
error: e.class.name,
message: "Request timed out after #{timeout}s",
}
nil
rescue StandardError => e
@errors << {
endpoint: endpoint,
method: method,
error: e.class.name,
message: e.message,
}
nil
end
end
def benchmark_concurrent_requests(endpoint, count)
start_time = Time.now
pool = Concurrent::ThreadPoolExecutor.new(
min_threads: @threads,
max_threads: @threads,
max_queue: count
)
futures = Array.new(count) do
Concurrent::Future.execute(executor: pool) do
make_request(endpoint)
end
end
futures.each(&:value!)
total_time = Time.now - start_time
pool.shutdown
pool.wait_for_termination(10)
total_time
end
def make_request(endpoint, method: 'GET', data: nil, expected_status: [200])
uri = URI("#{@base_url}#{endpoint}")
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 10
http.read_timeout = 10
request = case method.upcase
when 'GET'
Net::HTTP::Get.new(uri)
when 'POST'
req = Net::HTTP::Post.new(uri)
req['Content-Type'] = 'application/json'
req.body = data.to_json if data
req
else
raise "Unsupported method: #{method}"
end
begin
response = http.request(request)
unless expected_status.include?(response.code.to_i)
@errors << {
endpoint: endpoint,
method: method,
expected: expected_status,
actual: response.code.to_i,
message: response.body,
}
end
puts "#{method} #{endpoint} -> #{response.code}" if @verbose
response
rescue StandardError => e
@errors << {
endpoint: endpoint,
method: method,
error: e.class.name,
message: e.message,
}
nil
end
end
def generate_test_license_keys(count)
Array.new(count) do |_i|
# Generate fake license keys for testing
parts = Array.new(4) { format('%04d', rand(10_000)) }
"TEST-#{parts.join('-')}"
end
end
def print_summary
puts '📊 BENCHMARK SUMMARY'
puts '=' * 60
if @results.any?
@results.each do |test_name, data|
puts "#{test_name.to_s.tr('_', ' ').upcase}:"
case test_name
when :products
puts " • Single request: #{format('%.3f', data[:single_request_time] * 1000)}ms"
puts " • Throughput: #{format('%.2f', data[:requests_per_second])} req/s"
when :license_validation
puts " • Average time: #{format('%.3f', data[:average_time] * 1000)}ms"
when :concurrent_license_validation
puts " • Concurrent throughput: #{format('%.2f', data[:requests_per_second])} req/s"
puts " • Total time: #{format('%.3f', data[:total_time])}s"
when :order_creation
puts " • Average time: #{format('%.3f', data[:average_time] * 1000)}ms"
puts " • Throughput: #{format('%.2f', data[:requests_per_second])} req/s"
when :mixed_workload
puts " • Mixed throughput: #{format('%.2f', data[:requests_per_second])} req/s"
puts " • Total requests: #{data[:total_requests]}"
end
puts
end
end
if @errors.any?
puts "⚠️ ERRORS ENCOUNTERED (#{@errors.length}):"
@errors.first(5).each do |error|
puts " • #{error[:method]} #{error[:endpoint]}: #{error[:error] || error[:actual]} - #{error[:message]&.slice(
0, 100
)}"
end
puts " ... and #{@errors.length - 5} more" if @errors.length > 5
puts
end
# Calculate overall performance score
if @results[:products] && @results[:concurrent_license_validation]
overall_rps = [
@results[:products][:requests_per_second],
@results[:concurrent_license_validation][:requests_per_second],
].compact.sum / 2
puts '🎯 OVERALL PERFORMANCE:'
puts " • Average throughput: #{format('%.2f', overall_rps)} req/s"
puts " • Fiber-enabled: #{fiber_enabled_score}"
puts
end
puts '✅ Benchmark completed!'
puts 'Run with --verbose for detailed request logs'
end
def fiber_enabled_score
# Simple heuristic: if we can handle more than 50 req/s, fibers are likely helping
overall_rps = @results.values.filter_map { |r| r[:requests_per_second] }.max || 0
case overall_rps
when 0..25
"LOW (#{format('%.1f', overall_rps)} req/s)"
when 25..100
"MEDIUM (#{format('%.1f', overall_rps)} req/s)"
when 100..500
"HIGH (#{format('%.1f', overall_rps)} req/s)"
else
"EXCELLENT (#{format('%.1f', overall_rps)} req/s)"
end
end
end
# Command line interface
if __FILE__ == $0
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: ruby benchmark_api.rb [options]'
opts.on('-h', '--host HOST', 'Server host (default: localhost)') do |h|
options[:host] = h
end
opts.on('-p', '--port PORT', Integer, 'Server port (default: 4567)') do |p|
options[:port] = p
end
opts.on('-t', '--threads THREADS', Integer, 'Number of threads (default: 10)') do |t|
options[:threads] = t
end
opts.on('-r', '--requests REQUESTS', Integer, 'Requests per test (default: 100)') do |r|
options[:requests] = r
end
opts.on('-v', '--verbose', 'Verbose output') do |v|
options[:verbose] = v
end
opts.on('--help', 'Show this help') do
puts opts
exit
end
end.parse!
benchmark = SourceLicenseAPIBenchmark.new(options)
benchmark.run_all_benchmarks
end