Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #23

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Test #23

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion files/app/isubata.ruby.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ WorkingDirectory=/home/isucon/isubata/webapp/ruby
EnvironmentFile=/home/isucon/env.sh
Environment=RACK_ENV=production

ExecStart = /home/isucon/local/ruby/bin/bundle exec puma -p 5000 -t 10
ExecStart = /home/isucon/local/ruby/bin/bundle exec puma -p 5000 -t 10 -c /home/isucon/isubata/webapp/ruby/puma.conf.rb

Restart = always
Type = simple
Expand Down
1 change: 1 addition & 0 deletions result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"job_id":"","ip_addrs":"160.251.14.109","pass":false,"score":0,"message":"負荷走行前のバリデーションに失敗しました。2020-06-21 20:03:17.925766461 +0900 JST m=+0.535032389 サーバエラーが発生しました。500 Internal Server Error (POST /login )","error":["2020-06-21 20:03:17.925766461 +0900 JST m=+0.535032389 サーバエラーが発生しました。500 Internal Server Error (POST /login )"],"log":null,"load_level":0,"start_time":"2020-06-21T20:03:17.888062673+09:00","end_time":"2020-06-21T20:03:17.925815071+09:00"}
1 change: 1 addition & 0 deletions test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hoge
Binary file added webapp/public/css/bootstrap.min.css.gz
Binary file not shown.
Binary file added webapp/public/css/main.css.gz
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webapp/public/icons/default.png
Binary file added webapp/public/js/bootstrap.min.js.gz
Binary file not shown.
Binary file added webapp/public/js/chat.js.gz
Binary file not shown.
Binary file added webapp/public/js/jquery.min.js.gz
Binary file not shown.
Binary file added webapp/public/js/tether.min.js.gz
Binary file not shown.
167 changes: 140 additions & 27 deletions webapp/ruby/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'sinatra/base'

class App < Sinatra::Base

configure do
set :session_secret, 'tonymoris'
set :public_folder, File.expand_path('../../public', __FILE__)
Expand Down Expand Up @@ -31,6 +32,49 @@ def user

@_user
end

def redis
@redis ||= Redis.current
end

def all_channel_ids_key
"all_channele_ids_key"
end

def get_all_channel_ids
ids_json = redis.get(all_channel_ids_key)
if ids_json
JSON.parse(ids_json)
else
set_all_channel_ids
end
end

def set_all_channel_ids
ids = db.query('SELECT id FROM channel').to_a.map{|row| row['id']}
redis.set(all_channel_ids_key, ids.to_json)
ids
end

def all_channels_order_by_id_key
"all_channels_order_by_id"
end

def get_all_channels_order_by_id
rows = redis.zrange(all_channels_order_by_id_key, 0, -1)
if rows.length == 0
set_all_channels_order_by_id
else
rows.map {|row| JSON.parse(row) }
end
end

def set_all_channels_order_by_id
channels = db.query('SELECT * FROM channel ORDER BY id').to_a
redis.zadd(all_channels_order_by_id_key, channels.map{ |c| [c['id'], c.to_json] })
channels
end

end

get '/initialize' do
Expand All @@ -39,6 +83,12 @@ def user
db.query("DELETE FROM channel WHERE id > 10")
db.query("DELETE FROM message WHERE id > 10000")
db.query("DELETE FROM haveread")

redis.flushall

channel_count = db.prepare('SELECT channel_id, COUNT(*) AS cnt FROM message GROUP BY channel_id').execute
redis.mset channel_count.map{|h| ["channle_message_count:#{h[:channel_id]}", h[:cnt]]}.flatten

204
end

Expand Down Expand Up @@ -87,6 +137,7 @@ def user
name = params[:name]
statement = db.prepare('SELECT * FROM user WHERE name = ?')
row = statement.execute(name).first
statement.close
if row.nil? || row['password'] != Digest::SHA1.hexdigest(row['salt'] + params[:password])
return 403
end
Expand Down Expand Up @@ -120,16 +171,19 @@ def user
last_message_id = params[:last_message_id].to_i
statement = db.prepare('SELECT * FROM message WHERE id > ? AND channel_id = ? ORDER BY id DESC LIMIT 100')
rows = statement.execute(last_message_id, channel_id).to_a
statement.close
users = get_users(rows.map { |r| r['user_id'] }.uniq)
response = []
rows.each do |row|
r = {}
r['id'] = row['id']
statement = db.prepare('SELECT name, display_name, avatar_icon FROM user WHERE id = ?')
r['user'] = statement.execute(row['user_id']).first
#statement = db.prepare('SELECT name, display_name, avatar_icon FROM user WHERE id = ?')
#r['user'] = statement.execute(row['user_id']).first
r['user'] = users[row['user_id']]
r['date'] = row['created_at'].strftime("%Y/%m/%d %H:%M:%S")
r['content'] = row['content']
response << r
statement.close
#statement.close
end
response.reverse!

Expand All @@ -140,6 +194,7 @@ def user
'ON DUPLICATE KEY UPDATE message_id = ?, updated_at = NOW()',
].join)
statement.execute(user_id, channel_id, max_message_id, max_message_id)
statement.close

content_type :json
response.to_json
Expand All @@ -155,6 +210,9 @@ def user

rows = db.query('SELECT id FROM channel').to_a
channel_ids = rows.map { |row| row['id'] }
#channel_ids = get_all_channel_ids



res = []
channel_ids.each do |channel_id|
Expand All @@ -164,9 +222,11 @@ def user
r = {}
r['channel_id'] = channel_id
r['unread'] = if row.nil?
# N+1
statement = db.prepare('SELECT COUNT(*) as cnt FROM message WHERE channel_id = ?')
statement.execute(channel_id).first['cnt']
else
# N+1
statement = db.prepare('SELECT COUNT(*) as cnt FROM message WHERE channel_id = ? AND ? < id')
statement.execute(channel_id, row['message_id']).first['cnt']
end
Expand Down Expand Up @@ -208,6 +268,7 @@ def user
r['content'] = row['content']
@messages << r
statement.close
#statement.close
end
@messages.reverse!

Expand Down Expand Up @@ -265,6 +326,23 @@ def user
statement.execute(name, description)
channel_id = db.last_id
statement.close

#_channel, id = redis.zrange(all_channels_order_by_id_key, -1, -1, with_scores: true).last
#new_id = id.to_i + 1
#now = Time.now
#attributes = {
# 'id': new_id,
# 'name': name,
# 'description': description,
# 'updated_at': now.to_s,
# 'created_at': now.to_s,
#}

#redis.zadd(all_channels_order_by_id_key, [new_id, attributes.to_json])
#ids = get_all_channel_ids << new_id
#redis.set(all_channel_ids_key, ids)


redirect "/channel/#{channel_id}", 303
end

Expand Down Expand Up @@ -299,13 +377,18 @@ def user

avatar_name = digest + ext
avatar_data = data

# 画像保存をファイルシステムに変更
path = File.join("/home/isucon/isubata/webapp/public/icons/", avatar_name)
File.write(path, avatar_data) unless File.exists?(path)
end
end


if !avatar_name.nil? && !avatar_data.nil?
statement = db.prepare('INSERT INTO image (name, data) VALUES (?, ?)')
statement.execute(avatar_name, avatar_data)
statement.close
#statement = db.prepare('INSERT INTO image (name, data) VALUES (?, ?)')
#statement.execute(avatar_name, avatar_data)
#statement.close
statement = db.prepare('UPDATE user SET avatar_icon = ? WHERE id = ?')
statement.execute(avatar_name, user['id'])
statement.close
Expand All @@ -320,35 +403,53 @@ def user
redirect '/', 303
end

post'/dump/icons' do
rows = db.query('SELECT `name`, `data` FROM `image`')

rows.each do |row|
File.write("/home/isucon/isubata/webapp/public/icons/#{row['name']}", row['data'])
end

redirect '/'
end

get '/icons/:file_name' do
file_name = params[:file_name]
statement = db.prepare('SELECT * FROM image WHERE name = ?')
row = statement.execute(file_name).first
statement.close
ext = file_name.include?('.') ? File.extname(file_name) : ''
mime = ext2mime(ext)
if !row.nil? && !mime.empty?
content_type mime
return row['data']
#statement = db.prepare('SELECT * FROM image WHERE name = ?')
#row = statement.execute(file_name).first
#statement.close
#ext = file_name.include?('.') ? File.extname(file_name) : ''
#mime = ext2mime(ext)
#if !row.nil? && !mime.empty?
# content_type mime
# return row['data']
#end

path = "/home/isucon/isubata/webapp/public/icons/#{file_name}"
if path
return File.read(path)
end
404
end

private

def db
return @db_client if defined?(@db_client)
#return @db_client if defined?(@db_client)

@db_client = Mysql2::Client.new(
#@db_client = Mysql2::Client.new(
Thread.current[:isubata_db] ||= Mysql2::Client.new(
host: ENV.fetch('ISUBATA_DB_HOST') { 'localhost' },
port: ENV.fetch('ISUBATA_DB_PORT') { '3306' },
username: ENV.fetch('ISUBATA_DB_USER') { 'root' },
password: ENV.fetch('ISUBATA_DB_PASSWORD') { '' },
database: 'isubata',
encoding: 'utf8mb4'
encoding: 'utf8mb4',
reconnect: true,
init_command: %q!SET SESSION sql_mode='TRADITIONAL,NO_AUTO_VALUE_ON_ZERO,ONLY_FULL_GROUP_BY'!
)
@db_client.query('SET SESSION sql_mode=\'TRADITIONAL,NO_AUTO_VALUE_ON_ZERO,ONLY_FULL_GROUP_BY\'')
@db_client
#@db_client.query('SET SESSION sql_mode=\'TRADITIONAL,NO_AUTO_VALUE_ON_ZERO,ONLY_FULL_GROUP_BY\'')
#@db_client
end

def db_get_user(user_id)
Expand Down Expand Up @@ -380,15 +481,16 @@ def register(user, password)
end

def get_channel_list_info(focus_channel_id = nil)
channels = db.query('SELECT * FROM channel ORDER BY id').to_a
description = ''
channels.each do |channel|
if channel['id'] == focus_channel_id
description = channel['description']
break
end
#channels = db.query('SELECT * FROM channel ORDER BY id').to_a
@channel_list ||= db.query('SELECT * FROM channel ORDER BY id').to_a
#channels = get_all_channels_order_by_id

if focus_channel_id
statement = db.prepare('SELECT description FROM channel WHERE id = ? LIMIT 1')
description = statement.execute(focus_channel_id.to_i).first['description']
statement.close
end
[channels, description]
[@channel_list, description]
end

def ext2mime(ext)
Expand All @@ -403,4 +505,15 @@ def ext2mime(ext)
end
''
end

def get_users(ids)
rows = db.query("SELECT id, name, display_name, avatar_icon FROM user WHERE id IN (#{ids.join(',')})")

dict = {}
rows.each do |row|
dict[row['id']] = row.select { |k, v| %w[name display_name avatar_icon].include?(k) }
end

dict
end
end
1 change: 1 addition & 0 deletions webapp/ruby/nginx
9 changes: 9 additions & 0 deletions webapp/ruby/puma.conf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
workers 2
threads 32,32

bind 'tcp://0.0.0.0:5000'
bind 'unix:///run/isubata/puma.sock'

stdout_redirect '/home/isucon/puma.out', '/home/isucon/puma.err' true

preload_app!
1 change: 1 addition & 0 deletions webapp/ruby/result.json

Large diffs are not rendered by default.

16 changes: 1 addition & 15 deletions webapp/ruby/views/add_channel.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
<form action="/add_channel" method="post">
<div class="form-group row">
<label for="inputname" class="col-sm-2 col-form-label">チャンネル名</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" id="inputname">
</div>
</div>
<div class="form-group row">
<label for="inputdescription" class="col-sm-2 col-form-label">詳細</label>
<div class="col-sm-10">
<textarea class="form-control input-sm" rows="3" name="description" id="inputdescription"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary">登録</button>
</form>
<form action="/add_channel" method="post"><div class="form-group row"><label for="inputname" class="col-sm-2 col-form-label">チャンネル名</label><div class="col-sm-10"><input type="text" class="form-control" name="name" id="inputname"></div></div><div class="form-group row"><label for="inputdescription" class="col-sm-2 col-form-label">詳細</label><div class="col-sm-10"><textarea class="form-control input-sm" rows="3" name="description" id="inputdescription"></textarea></div></div><button type="submit" class="btn btn-primary">登録</button></form>
16 changes: 1 addition & 15 deletions webapp/ruby/views/channel.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
<div class="well"><%= @description %></div>
<div id="timeline"></div>

<% if user %>
<div class="row">
<div class="col-sm-9 col-md-9" id="chatbox-frame">
<div class="input-group chatbox">
<textarea class="form-control" rows="3" id="chatbox-textarea"></textarea>
<span class="input-group-btn"> <button class="btn btn-primary" onclick="on_send_button()">送信</button> </span>
</div>
</div>
</div>
<% end %>

<script type="text/javascript" src="/js/chat.js"></script>
<div class="well"><%= @description %></div><div id="timeline"></div><% if user %><div class="row"><div class="col-sm-9 col-md-9" id="chatbox-frame"><div class="input-group chatbox"><textarea class="form-control" rows="3" id="chatbox-textarea"></textarea><span class="input-group-btn"> <button class="btn btn-primary" onclick="on_send_button()">送信</button> </span></div></div></div><% end %><script type="text/javascript" src="/js/chat.js"></script>
28 changes: 1 addition & 27 deletions webapp/ruby/views/history.erb
Original file line number Diff line number Diff line change
@@ -1,27 +1 @@
<div id="history">
<% @messages.each do |msg| %>
<div class="media message">
<img class="avatar d-flex align-self-start mr-3" src="/icons/<%= msg['user']['avatar_icon'] %>" alt="no avatar">
<div class="media-body">
<h5 class="mt-0"><a href="/profile/<%= msg['user']['name'] %>"><%= msg['user']['display_name'] %>@<%= msg['user']['name'] %></a></h5>
<p class="content"><%= msg['content'] %></p>
<p class="message-date"><%= msg['date'] %></p>
</div>
</div>
<% end %>
</div>

<nav>
<ul class="pagination">
<% if 1 < @page %>
<li><a href="/history/<%= @channel_id %>?page=<%= @page - 1 %>"><span>«</span></a></li>
<% end %>
<% 1.upto(@max_page) do |p| %>
<% if @page == p %> <li class="active"> <% else %> <li> <% end %>
<a href="/history/<%= @channel_id %>?page=<%= p %>"><%= p %></a></li>
<% end %>
<% if @page < @max_page %>
<li><a href="/history/<%= @channel_id %>?page=<%= @page + 1 %>"><span>»</span></a></li>
<% end %>
</ul>
</nav>
<div id="history"><% @messages.each do |msg| %><div class="media message"><img class="avatar d-flex align-self-start mr-3" src="/icons/<%= msg['user']['avatar_icon'] %>" alt="no avatar"><div class="media-body"><h5 class="mt-0"><a href="/profile/<%= msg['user']['name'] %>"><%= msg['user']['display_name'] %>@<%= msg['user']['name'] %></a></h5><p class="content"><%= msg['content'] %></p><p class="message-date"><%= msg['date'] %></p></div></div><% end %></div><nav><ul class="pagination"><% if 1 < @page %><li><a href="/history/<%= @channel_id %>?page=<%= @page - 1 %>"><span>«</span></a></li><% end %><% 1.upto(@max_page) do |p| %><% if @page == p %> <li class="active"> <% else %> <li> <% end %><a href="/history/<%= @channel_id %>?page=<%= p %>"><%= p %></a></li><% end %><% if @page < @max_page %><li><a href="/history/<%= @channel_id %>?page=<%= @page + 1 %>"><span>»</span></a></li><% end %></ul></nav>
5 changes: 1 addition & 4 deletions webapp/ruby/views/index.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
<h1>ようこそ Isubata へ。</h1>
<p>Isubata は世界最速のチャットアプリケーションです。</p>
<p>利用を開始するには<a href="/login">ログイン</a>してください。</p>
<p>アカウントをお持ちでない場合は<a href="/register">新規登録</a>してください。</p>
<h1>ようこそ Isubata へ。</h1><p>Isubata は世界最速のチャットアプリケーションです。</p><p>利用を開始するには<a href="/login">ログイン</a>してください。</p><p>アカウントをお持ちでない場合は<a href="/register">新規登録</a>してください。</p>
Loading