issue 11269 性能问题,从12秒到6秒多,还有优化空间

This commit is contained in:
guange 2017-01-18 00:17:41 +08:00
parent fdf00eb9cf
commit 6d1503f3bd
8 changed files with 67 additions and 57 deletions

View File

@ -1,5 +1,5 @@
<% user_ids = student_work.student_work_projects.empty? ? "(-1)" : "(" + student_work.student_work_projects.map{|st| st.user_id}.join(",") + ")" %>
<% student_work_groups = @homework.student_works.where("user_id in #{user_ids}").order("created_at asc") %>
<% user_ids = student_work.student_work_projects.map(&:user_id) %>
<% student_work_groups = @homework.student_works.where(user_id: user_ids).includes(:user).order("created_at asc") %>
<% anon_count = student_work.student_works_scores.where("reviewer_role = 3 and appeal_status != 3").group_by(&:user_id).count %>
<% student_work_groups.each_with_index do |st, j| %>
<% score_open = @is_teacher || @homework.score_open == 1 || st.user == User.current %>

View File

@ -1,4 +1,4 @@
if Rails.env.development?
Rack::MiniProfiler.config.position = 'right'
Rack::MiniProfiler.config.start_hidden = true
Rack::MiniProfiler.config.start_hidden = false
end

View File

@ -11,7 +11,8 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20161223083022) do
ActiveRecord::Schema.define(:version => 20170117161330) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
t.string "act_type", :null => false
@ -337,14 +338,16 @@ ActiveRecord::Schema.define(:version => 20161223083022) do
add_index "changeset_parents", ["parent_id"], :name => "changeset_parents_parent_ids"
create_table "changesets", :force => true do |t|
t.integer "repository_id", :null => false
t.string "revision", :null => false
t.integer "repository_id", :null => false
t.string "revision", :null => false
t.string "committer"
t.datetime "committed_on", :null => false
t.datetime "committed_on", :null => false
t.text "comments"
t.date "commit_date"
t.string "scmid"
t.integer "user_id"
t.integer "project_id"
t.integer "type", :default => 0
end
add_index "changesets", ["committed_on"], :name => "index_changesets_on_committed_on"
@ -614,20 +617,6 @@ ActiveRecord::Schema.define(:version => 20161223083022) do
t.integer "container_id", :default => 0
end
create_table "course_class_post", :id => false, :force => true do |t|
t.integer "班级id", :default => 0, :null => false
t.string "班级名"
t.integer "帖子id", :default => 0, :null => false
t.integer "主贴id"
t.string "帖子标题", :default => "", :null => false
t.text "帖子内容"
t.integer "帖子用户id"
t.integer "帖子回复数", :default => 0, :null => false
t.integer "最后回帖id"
t.datetime "发帖时间", :null => false
t.datetime "帖子更新时间", :null => false
end
create_table "course_contributor_scores", :force => true do |t|
t.integer "course_id"
t.integer "user_id"
@ -927,13 +916,11 @@ ActiveRecord::Schema.define(:version => 20161223083022) do
t.integer "exercise_status"
t.integer "user_id"
t.integer "time"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "publish_time"
t.datetime "end_time"
t.integer "show_result"
t.integer "question_random", :default => 0
t.integer "choice_random", :default => 0
end
create_table "first_pages", :force => true do |t|
@ -1122,6 +1109,10 @@ ActiveRecord::Schema.define(:version => 20161223083022) do
t.datetime "updated_at", :null => false
end
create_table "innodb_monitor", :id => false, :force => true do |t|
t.integer "a"
end
create_table "invite_lists", :force => true do |t|
t.integer "project_id"
t.integer "user_id"
@ -2257,6 +2248,8 @@ ActiveRecord::Schema.define(:version => 20161223083022) do
t.string "description", :default => ""
end
add_index "user_extensions", ["user_id"], :name => "index_user_extensions_on_user_id"
create_table "user_feedback_messages", :force => true do |t|
t.integer "user_id"
t.integer "journals_for_message_id"
@ -2433,6 +2426,16 @@ ActiveRecord::Schema.define(:version => 20161223083022) do
t.datetime "updated_at", :null => false
end
create_table "wechat_logs", :force => true do |t|
t.string "openid", :null => false
t.text "request_raw"
t.text "response_raw"
t.text "session_raw"
t.datetime "created_at", :null => false
end
add_index "wechat_logs", ["openid"], :name => "index_wechat_logs_on_openid"
create_table "wiki_content_versions", :force => true do |t|
t.integer "wiki_content_id", :null => false
t.integer "page_id", :null => false

16
lib/tasks/test.rake Normal file
View File

@ -0,0 +1,16 @@
Rake::TaskManager.class_eval do
def delete_task(task_name)
@tasks.delete(task_name.to_s)
end
Rake.application.delete_task("db:test:purge")
Rake.application.delete_task("db:test:prepare")
end
namespace :db do
namespace :test do
task :purge do
end
task :prepare do
end
end
end

View File

@ -30,7 +30,6 @@ RSpec.configure do |config|
config.use_transactional_fixtures = true
config.before(:suite) do
require_relative "seeds"
end
# RSpec Rails can automatically mix in different behaviours to your tests

View File

@ -1,38 +1,16 @@
require 'rails_helper'
require 'shared_account_spec'
# "attachments"=>{"1"=>{"filename"=>"11.gif", "description"=>"", "is_public_checkbox"=>"1", "token"=>"33731.8a74e086d7716f89bc4fbac0606589c7"}}
RSpec.describe "作业打包下载", :type => :request do
let(:student){FactoryGirl.create(:student)}
RSpec.describe ZipdownController, :type => :controller do
describe "单独下载某学生作业" do
include_context "create user"
before {
FactoryGirl.create(:user)
shared_login
@homework = FactoryGirl.create(:homework, author_id: current_user.id)
@attch = HomeworkAttach.new
@attch.bid_id = @homework.id
@attch.user_id = student.id
@attachment = Attachment.new(:file => File.open(File.join(Rails.root, "spec/fixtures/test.jpg")))
@attachment.author = User.current
@attachment.container_type = 'HomeworkAttach'
@attachment.container_id = @attch.id
@attachment.filename = "test.jpg"
@attachment.save
params = {"1"=>{"filename" => "test.jpg", "description" =>"",
"is_public_checkbox"=>"1",
"token" => "#{@attachment.id}.#{@attachment.digest}" }
}
@attch.save_attachments(params)
@attch.name = "test.jpg"
@attch.save!
session[:user_id] = 3135
session[:atime] = Time.now.utc.to_i
session[:ctime] = Time.now.utc.to_i
}
it "正常下载" do
uu = current_user
allow(uu).to receive(:admin?).and_return(true)
allow(User).to receive(:current).and_return(uu)
get zipdown_download_user_homework_path, {homework:@attch.id}
get 'download_user_homework', {homework:24753}
expect(response).to have_http_status(:success)
expect(response.content_type).to eq(Mime::Type.new("applcation/zip",:zip))
end

View File

@ -1,7 +1,13 @@
require 'test_helper'
class ZipdownControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
def setup
login
end
test "能够正常下载学生作业" do
get 'download_user_homework', {homework:861}
assert response.response_code == 200
puts response
end
end

View File

@ -519,3 +519,11 @@ ActionView::TestCase::TestController.class_eval do
Rails.application.routes
end
end
class ActionController::TestCase
def login
session[:user_id] = 3135
session[:atime] = Time.now.utc.to_i
session[:ctime] = Time.now.utc.to_i
end
end