Skip to content

Commit

Permalink
increase unicorn worker and make candidates on memory (but the change…
Browse files Browse the repository at this point in the history
…s broke app so rolled back them) 17144
  • Loading branch information
mickamy committed Jan 2, 2024
1 parent fb6b2d1 commit 85bb5e5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ common:
build: common
docker compose build --progress=plain

up: build
up:
docker compose up -d

down:
Expand Down
2 changes: 2 additions & 0 deletions admin/config/bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias grep='grep --color=auto'

export ISHOCON_APP_LANG=ruby
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
- ./webapp:/home/ishocon/webapp
ports:
- "443:443"
- "3306:3306"

bench:
image: showwin/ishocon2_bench:latest
Expand Down
31 changes: 31 additions & 0 deletions note.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,34 @@ docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443"
~/ghq/github.com/mickamy/ISHOCON2 imp1* 1m 2s
```

- unicorn のワーカ増やした
- candidates をメモリに載せようとしたら壊れたので戻した
- 元々30件だからたいしたことないか?
- TODO: 余裕できたら再チャレンジ

```
❯ make bench
docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443"
2024/01/02 01:19:04 Start GET /initialize
2024/01/02 01:19:05 期日前投票を開始します
2024/01/02 01:19:13 エラーメッセージに誤りがあります at POST /vote
make: *** [bench] Error 1
~/ghq/github.com/mickamy/ISHOCON2 imp1* 9s
```

- unicorn のプロセスのおかげでスコアだけ上がった

```
❯ make bench
docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443"
2024/01/02 01:33:23 Start GET /initialize
2024/01/02 01:33:23 期日前投票を開始します
2024/01/02 01:33:24 期日前投票が終了しました
2024/01/02 01:33:24 投票を開始します Workload: 3
2024/01/02 01:34:09 投票が終了しました
2024/01/02 01:34:09 投票者が結果を確認しています
2024/01/02 01:34:25 投票者の感心がなくなりました
2024/01/02 01:34:25 {"score": 17144, "success": 16072, "failure": 0}
```
33 changes: 24 additions & 9 deletions webapp/ruby/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ def db
client
end

# def candidates
# @candidates ||= db.query('SELECT * FROM candidates').map do |row|
# {
# id: row[:id],
# name: row[:name],
# political_party: row[:political_party],
# sex: row[:sex],
# }
# end
# end

def election_results
query = <<SQL
SELECT c.id, c.name, c.political_party, c.sex, v.count
Expand Down Expand Up @@ -75,12 +86,13 @@ def db_initialize
end

get '/' do
candidates = []
cs = []
election_results.each_with_index do |r, i|
# 上位10人と最下位のみ表示
candidates.push(r) if i < 10 || 28 < i
cs.push(r) if i < 10 || 28 < i
end

# parties_set = cs.map { |c| c[:political_party] }.uniq
parties_set = db.query('SELECT political_party FROM candidates GROUP BY political_party')
parties = {}
parties_set.each { |a| parties[a[:political_party]] = 0 }
Expand All @@ -93,13 +105,14 @@ def db_initialize
sex_ratio[r[:sex].to_sym] += r[:count] || 0
end

erb :index, locals: { candidates: candidates,
erb :index, locals: { candidates: cs,
parties: parties,
sex_ratio: sex_ratio }
end

get '/candidates/:id' do
candidate = db.xquery('SELECT * FROM candidates WHERE id = ?', params[:id]).first
# candidate = candidates.first { |c| c[:id] == param[:id] }
return redirect '/' if candidate.nil?
votes = db.xquery('SELECT COUNT(*) AS count FROM votes WHERE candidate_id = ?', params[:id]).first[:count]
keywords = voice_of_supporter([params[:id]])
Expand Down Expand Up @@ -133,20 +146,22 @@ def db_initialize
params[:address],
params[:mynumber]).first
candidate = db.xquery('SELECT * FROM candidates WHERE name = ?', params[:candidate]).first
# candidate = candidates.first { |c| c[:name] == params[:candidate] }
voted_count =
user.nil? ? 0 : db.xquery('SELECT COUNT(*) AS count FROM votes WHERE user_id = ?', user[:id]).first[:count]

candidates = db.query('SELECT * FROM candidates')
cs = candidates
if user.nil?
return erb :vote, locals: { candidates: candidates, message: '個人情報に誤りがあります' }
return erb :vote, locals: { candidates: cs, message: '個人情報に誤りがあります' }
elsif user[:votes] < (params[:vote_count].to_i + voted_count)
return erb :vote, locals: { candidates: candidates, message: '投票数が上限を超えています' }
return erb :vote, locals: { candidates: cs, message: '投票数が上限を超えています' }
elsif params[:candidate].nil? || params[:candidate] == ''
return erb :vote, locals: { candidates: candidates, message: '候補者を記入してください' }
return erb :vote, locals: { candidates: cs, message: '候補者を記入してください' }
elsif candidate.nil?
return erb :vote, locals: { candidates: candidates, message: '候補者を正しく記入してください' }
return erb :vote, locals: { candidates: cs, message: '候補者を正しく記入してください' }
elsif params[:keyword].nil? || params[:keyword] == ''
return erb :vote, locals: { candidates: candidates, message: '投票理由を記入してください' }
return erb :vote, locals: { candidates: cs, message: '投票理由を記入してください' }
end

params[:vote_count].to_i.times do
Expand All @@ -155,7 +170,7 @@ def db_initialize
candidate[:id],
params[:keyword])
end
return erb :vote, locals: { candidates: candidates, message: '投票に成功しました' }
return erb :vote, locals: { candidates: cs, message: '投票に成功しました' }
end

get '/initialize' do
Expand Down
4 changes: 2 additions & 2 deletions webapp/ruby/unicorn_config.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
worker_processes 2
worker_processes 8
preload_app true
pid './unicorn.pid'
pid '/tmp/unicorn.pid'
listen 8080

0 comments on commit 85bb5e5

Please sign in to comment.