diff --git a/frameworks/Ruby/rage/Gemfile b/frameworks/Ruby/rage/Gemfile new file mode 100644 index 00000000000..a7f090d01f9 --- /dev/null +++ b/frameworks/Ruby/rage/Gemfile @@ -0,0 +1,13 @@ +source "https://rubygems.org" + +gem "rage-rb", "~> 1.3" + +gem "pg", "~> 1.0" +gem "activerecord", "~> 7.0.0", require: "active_record" + +# Build JSON APIs with ease +# gem "alba" + +# Get 50% to 150% boost when parsing JSON. +# Rage will automatically use FastJsonparser if it is available. +# gem "fast_jsonparser" diff --git a/frameworks/Ruby/rage/README.md b/frameworks/Ruby/rage/README.md new file mode 100755 index 00000000000..05e005bd363 --- /dev/null +++ b/frameworks/Ruby/rage/README.md @@ -0,0 +1,47 @@ +# Rage Benchmarking Test + +Rage is a fast web framework compatible with Rails. It uses an event-driven architecture and implements a lightweight, cooperative concurrency model based on Ruby Fibers. + +https://github.com/rage-rb/rage + +### Test Type Implementation Source Code + +* [JSON](app/controllers/benchmarks_controller.rb) +* [PLAINTEXT](app/controllers/benchmarks_controller.rb) +* [DB](app/controllers/benchmarks_controller.rb) +* [QUERY](app/controllers/benchmarks_controller.rb) +* [UPDATE](app/controllers/benchmarks_controller.rb) +* [FORTUNES](app/controllers/benchmarks_controller.rb) + +## Important Libraries + +The tests were run with: + +* [ActiveRecord](https://rubygems.org/gems/activerecord) +* [PG](https://rubygems.org/gems/pg) + +## Test URLs + +### JSON + +http://localhost:8080/json + +### PLAINTEXT + +http://localhost:8080/plaintext + +### DB + +http://localhost:8080/db + +### QUERY + +http://localhost:8080/queries?queries= + +### UPDATE + +http://localhost:8080/updates?queries= + +### FORTUNES + +http://localhost:8080/fortunes diff --git a/frameworks/Ruby/rage/Rakefile b/frameworks/Ruby/rage/Rakefile new file mode 100644 index 00000000000..046f1fcbd8d --- /dev/null +++ b/frameworks/Ruby/rage/Rakefile @@ -0,0 +1 @@ +require_relative "config/application" diff --git a/frameworks/Ruby/rage/app/controllers/application_controller.rb b/frameworks/Ruby/rage/app/controllers/application_controller.rb new file mode 100644 index 00000000000..c3238c52392 --- /dev/null +++ b/frameworks/Ruby/rage/app/controllers/application_controller.rb @@ -0,0 +1,2 @@ +class ApplicationController < RageController::API +end diff --git a/frameworks/Ruby/rage/app/controllers/benchmarks_controller.rb b/frameworks/Ruby/rage/app/controllers/benchmarks_controller.rb new file mode 100644 index 00000000000..884356e58c1 --- /dev/null +++ b/frameworks/Ruby/rage/app/controllers/benchmarks_controller.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +class BenchmarksController < ApplicationController + ALL_DB_IDS = (1..10_000).to_a + FORTUNES_TEMPLATE = ERB.new(Rage.root.join("app/views/fortunes.html.erb").read) + + before_action do + headers["server"] = "rage" + end + + def json + render json: { message: "Hello, World!" } + end + + def plaintext + render plain: "Hello, World!" + end + + def db + render json: World.find(random_id) + end + + def queries + records = requested_ids.map do |id| + World.find(id) + end + + render json: records + end + + def fortunes + records = Fortune.pluck(:id, :message).map! { |id, message| { id:, message: } } + + records << Fortune.new(id: 0, message: "Additional fortune added at request time.") + records.sort_by! { |record| record[:message] } + + render plain: FORTUNES_TEMPLATE.result(binding) + headers["content-type"] = "text/html; charset=utf-8" + end + + def updates + records = requested_ids.map do |id| + World.find(id) + end + + updates = records.map do |record| + new_value = random_id + new_value = random_id until new_value != record.randomNumber + + record.randomNumber = new_value + + { id: record.id, randomnumber: new_value } + end + + World.upsert_all(updates.sort_by! { |u| u[:id] }) + + render json: records + end + + private + + def requested_ids + num = params[:queries].to_i + + if num > 500 + num = 500 + elsif num < 1 + num = 1 + end + + ALL_DB_IDS.sample(num) + end + + def random_id + Random.rand(9_999) + 1 + end +end diff --git a/frameworks/Ruby/rage/app/models/application_record.rb b/frameworks/Ruby/rage/app/models/application_record.rb new file mode 100644 index 00000000000..b63caeb8a5c --- /dev/null +++ b/frameworks/Ruby/rage/app/models/application_record.rb @@ -0,0 +1,3 @@ +class ApplicationRecord < ActiveRecord::Base + primary_abstract_class +end diff --git a/frameworks/Ruby/rage/app/models/fortune.rb b/frameworks/Ruby/rage/app/models/fortune.rb new file mode 100644 index 00000000000..6b7ad122f80 --- /dev/null +++ b/frameworks/Ruby/rage/app/models/fortune.rb @@ -0,0 +1,7 @@ +class Fortune < ApplicationRecord + self.table_name = "Fortune" + + def as_json(*) + attributes + end +end diff --git a/frameworks/Ruby/rage/app/models/world.rb b/frameworks/Ruby/rage/app/models/world.rb new file mode 100644 index 00000000000..951aab55b64 --- /dev/null +++ b/frameworks/Ruby/rage/app/models/world.rb @@ -0,0 +1,9 @@ +class World < ApplicationRecord + self.table_name = "World" + + def as_json(*) + attributes + end + + alias_attribute(:randomNumber, :randomnumber) +end diff --git a/frameworks/Ruby/rage/app/views/fortunes.html.erb b/frameworks/Ruby/rage/app/views/fortunes.html.erb new file mode 100644 index 00000000000..1aa63f3772a --- /dev/null +++ b/frameworks/Ruby/rage/app/views/fortunes.html.erb @@ -0,0 +1,12 @@ + + +
id | message |
---|---|
<%= record[:id] %> | <%= CGI.escape_html(record[:message]) %> |