forked from TechEmpower/FrameworkBenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmarks_controller.rb
More file actions
77 lines (58 loc) · 1.41 KB
/
benchmarks_controller.rb
File metadata and controls
77 lines (58 loc) · 1.41 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
# 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 db
render json: World.with_pk(random_id).values
end
def queries
ids = requested_ids
worlds = DB.synchronize do
ids.map do |id|
World.with_pk(id)
end
end
render json: worlds.map!(&:values)
end
def fortunes
records = Fortune.all
fortune = Fortune.new
fortune.id = 0
fortune.message = "Additional fortune added at request time."
records << fortune
records.sort_by!(&:message)
render plain: FORTUNES_TEMPLATE.result(binding)
headers["content-type"] = "text/html; charset=utf-8"
end
def updates
worlds = nil
ids = requested_ids
DB.synchronize do
worlds = ids.map do |id|
world = World.with_pk(id)
new_value = random_id
new_value = random_id while new_value == world.randomnumber
world.randomnumber = new_value
world
end
World.batch_update(worlds)
end
render json: worlds.map!(&:values)
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