|
1 | | -MAX_PK = 10_000 |
2 | | -QUERIES_MIN = 1 |
3 | | -QUERIES_MAX = 500 |
| 1 | +QUERY_RANGE = (1..10_000).freeze |
| 2 | +ALL_IDS = QUERY_RANGE.to_a |
4 | 3 |
|
5 | 4 | HelloWorld::App.controllers do |
| 5 | + |
| 6 | + after do |
| 7 | + response['Server'] = 'padrino' |
| 8 | + end |
| 9 | + |
| 10 | + after do |
| 11 | + response['Date'] = Time.now.httpdate |
| 12 | + end if defined?(Puma) |
| 13 | + |
6 | 14 | get '/json', :provides => [:json] do |
7 | | - response.headers['Server'] = 'padrino' |
8 | | - response.headers['Date'] = Time.now.httpdate |
9 | 15 | {message: "Hello, World!"}.to_json |
10 | 16 | end |
11 | 17 |
|
12 | 18 | get '/db', :provides => [:json] do |
13 | | - response.headers['Server'] = 'padrino' |
14 | | - response.headers['Date'] = Time.now.httpdate |
15 | | - id = Random.rand(MAX_PK) + 1 |
16 | | - World.get(id).attributes.to_json |
| 19 | + world = ActiveRecord::Base.with_connection do |
| 20 | + World.find(rand1).attributes |
| 21 | + end |
| 22 | + world.to_json |
17 | 23 | end |
18 | 24 |
|
19 | 25 | get '/queries', :provides => [:json] do |
20 | | - response.headers['Server'] = 'padrino' |
21 | | - response.headers['Date'] = Time.now.httpdate |
22 | | - queries = params['queries'].to_i.clamp(QUERIES_MIN, QUERIES_MAX) |
23 | | - |
24 | | - results = (1..queries).map do |
25 | | - World.get(Random.rand(MAX_PK) + 1).attributes |
26 | | - end.to_json |
| 26 | + worlds = ActiveRecord::Base.with_connection do |
| 27 | + ALL_IDS.sample(bounded_queries).map do |id| |
| 28 | + World.find(id).attributes |
| 29 | + end |
| 30 | + end |
| 31 | + worlds.to_json |
27 | 32 | end |
28 | 33 |
|
29 | 34 | get '/fortunes' do |
30 | | - response.headers['Server'] = 'padrino' |
31 | | - response.headers['Date'] = Time.now.httpdate |
32 | | - @fortunes = Fortune.all |
33 | | - @fortunes << Fortune.new(:id => 0, :message => "Additional fortune added at request time.") |
34 | | - @fortunes = @fortunes.sort_by { |x| x.message } |
| 35 | + @fortunes = Fortune.all.to_a |
| 36 | + @fortunes << Fortune.new( |
| 37 | + id: 0, |
| 38 | + message: "Additional fortune added at request time." |
| 39 | + ) |
| 40 | + @fortunes = @fortunes.sort_by(&:message) |
35 | 41 |
|
36 | 42 | render 'fortunes', layout: "layout" |
37 | 43 | end |
38 | 44 |
|
39 | 45 | get '/updates', :provides => [:json] do |
40 | | - response.headers['Server'] = 'padrino' |
41 | | - response.headers['Date'] = Time.now.httpdate |
42 | | - queries = params['queries'].to_i.clamp(QUERIES_MIN, QUERIES_MAX) |
43 | | - |
44 | | - worlds = (1..queries).map do |
45 | | - # get a random row from the database, which we know has 10000 |
46 | | - # rows with ids 1 - 10000 |
47 | | - world = World.get(Random.rand(MAX_PK) + 1) |
48 | | - world.update(randomNumber: Random.rand(MAX_PK) + 1) |
49 | | - world.attributes |
| 46 | + worlds = [] |
| 47 | + ActiveRecord::Base.with_connection do |
| 48 | + worlds = ALL_IDS.sample(bounded_queries).map do |id| |
| 49 | + world = World.find(id) |
| 50 | + new_value = rand1 |
| 51 | + new_value = rand1 while new_value == world.randomNumber |
| 52 | + world.randomNumber = new_value |
| 53 | + world |
| 54 | + end |
| 55 | + World.upsert_all(worlds) |
50 | 56 | end |
51 | 57 |
|
52 | 58 | worlds.to_json |
53 | 59 | end |
54 | 60 |
|
55 | 61 | get '/plaintext' do |
56 | | - response.headers['Server'] = 'padrino' |
57 | | - response.headers['Date'] = Time.now.httpdate |
58 | 62 | content_type 'text/plain' |
59 | 63 | "Hello, World!" |
60 | 64 | end |
61 | | - |
62 | 65 | end |
0 commit comments