Gem 有用 Spree Commerce 的吗?真心慢啊

tini8 · 2015年04月03日 · 最后由 ericguo 回复于 2015年10月05日 · 3873 次阅读

在 MAC 上搭建了一个,每打开一个页面差不多得 2 秒钟,都花费再 erb 模板渲染上了,production 环境应该也改善不了多少吧,erb 模板没法缓存。

下面是打开首页时 console 的信息:

Started GET "/" for ::1 at 2015-04-03 21:59:17 +0800 Processing by Spree::HomeController#index as HTML Spree::Preference Load (0.1ms) SELECT "spree_preferences".* FROM "spree_preferences" WHERE "spree_preferences"."key" = ? LIMIT 1 [["key", "spree/frontend_configuration/locale"]] Spree::User Load (0.1ms) SELECT "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = ? ORDER BY "spree_users"."id" ASC LIMIT 1 [["id", 1]] Spree::Store Load (0.1ms) SELECT "spree_stores".* FROM "spree_stores" WHERE (url like '%localhost%') ORDER BY "spree_stores"."id" ASC LIMIT 1 Spree::Store Load (0.1ms) SELECT "spree_stores".* FROM "spree_stores" WHERE "spree_stores"."default" = 't' ORDER BY "spree_stores"."id" ASC LIMIT 1 Spree::Order Load (0.0ms) SELECT "spree_orders".* FROM "spree_orders" WHERE "spree_orders"."completed_at" IS NULL AND "spree_orders"."currency" = ? AND "spree_orders"."guest_token" = ? AND "spree_orders"."store_id" = ? AND "spree_orders"."user_id" = ? LIMIT 1 [["currency", "USD"], ["guest_token", "Kw4AVot88W6Elx_pi9Tpkw"], ["store_id", 1], ["user_id", 1]] Spree::Order Load (0.1ms) SELECT "spree_orders".* FROM "spree_orders" WHERE "spree_orders"."user_id" = ? AND "spree_orders"."completed_at" IS NULL ORDER BY created_at DESC LIMIT 1 [["user_id", 1]] Spree::Taxonomy Load (0.1ms) SELECT "spree_taxonomies".* FROM "spree_taxonomies" ORDER BY spree_taxonomies.position, spree_taxonomies.created_at Spree::Taxon Load (0.2ms) SELECT "spree_taxons".* FROM "spree_taxons" WHERE "spree_taxons"."parent_id" IS NULL AND "spree_taxons"."taxonomy_id" IN (1, 2) Spree::Taxon Load (0.2ms) SELECT "spree_taxons".* FROM "spree_taxons" WHERE "spree_taxons"."parent_id" IN (1, 2) ORDER BY "lft" Rendered /Users/tini8/.rvm/gems/ruby-2.2.1@rails420/gems/spree_frontend-3.0.0/app/views/spree/shared/_taxonomies.html.erb (13.9ms) (0.3ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "spree_products" INNER JOIN "spree_variants" ON "spree_variants"."product_id" = "spree_products"."id" AND "spree_variants"."is_master" = 't' AND "spree_variants"."deleted_at" IS NULL INNER JOIN "spree_prices" ON "spree_prices"."variant_id" = "spree_variants"."id" AND "spree_prices"."deleted_at" IS NULL WHERE "spree_products"."deleted_at" IS NULL AND ("spree_products".deleted_at IS NULL or "spree_products".deleted_at >= '2015-04-03 13:59:17.648583') AND ("spree_products".available_on <= '2015-04-03 13:59:17.649115') AND (spree_prices.amount IS NOT NULL) AND "spree_prices"."currency" = 'USD' LIMIT 12 OFFSET 0) subquery_for_count (0.2ms) SELECT MAX("spree_products"."updated_at") FROM "spree_products" INNER JOIN "spree_variants" ON "spree_variants"."product_id" = "spree_products"."id" AND "spree_variants"."is_master" = 't' AND "spree_variants"."deleted_at" IS NULL INNER JOIN "spree_prices" ON "spree_prices"."variant_id" = "spree_variants"."id" AND "spree_prices"."deleted_at" IS NULL WHERE "spree_products"."deleted_at" IS NULL AND ("spree_products".deleted_at IS NULL or "spree_products".deleted_at >= '2015-04-03 13:59:17.648583') AND ("spree_products".available_on <= '2015-04-03 13:59:17.649115') AND (spree_prices.amount IS NOT NULL) AND "spree_prices"."currency" = 'USD' LIMIT 12 OFFSET 0 (0.3ms) SELECT COUNT() FROM "spree_products" INNER JOIN "spree_variants" ON "spree_variants"."product_id" = "spree_products"."id" AND "spree_variants"."is_master" = 't' AND "spree_variants"."deleted_at" IS NULL INNER JOIN "spree_prices" ON "spree_prices"."variant_id" = "spree_variants"."id" AND "spree_prices"."deleted_at" IS NULL WHERE "spree_products"."deleted_at" IS NULL AND ("spree_products".deleted_at IS NULL or "spree_products".deleted_at >= '2015-04-03 13:59:17.648583') AND ("spree_products".available_on <= '2015-04-03 13:59:17.649115') AND (spree_prices.amount IS NOT NULL) AND "spree_prices"."currency" = 'USD' CACHE (0.0ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "spree_products" INNER JOIN "spree_variants" ON "spree_variants"."product_id" = "spree_products"."id" AND "spree_variants"."is_master" = 't' AND "spree_variants"."deleted_at" IS NULL INNER JOIN "spree_prices" ON "spree_prices"."variant_id" = "spree_variants"."id" AND "spree_prices"."deleted_at" IS NULL WHERE "spree_products"."deleted_at" IS NULL AND ("spree_products".deleted_at IS NULL or "spree_products".deleted_at >= '2015-04-03 13:59:17.648583') AND ("spree_products".available_on <= '2015-04-03 13:59:17.649115') AND (spree_prices.amount IS NOT NULL) AND "spree_prices"."currency" = 'USD' LIMIT 12 OFFSET 0) subquery_for_count CACHE (0.0ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "spree_products" INNER JOIN "spree_variants" ON "spree_variants"."product_id" = "spree_products"."id" AND "spree_variants"."is_master" = 't' AND "spree_variants"."deleted_at" IS NULL INNER JOIN "spree_prices" ON "spree_prices"."variant_id" = "spree_variants"."id" AND "spree_prices"."deleted_at" IS NULL WHERE "spree_products"."deleted_at" IS NULL AND ("spree_products".deleted_at IS NULL or "spree_products".deleted_at >= '2015-04-03 13:59:17.648583') AND ("spree_products".available_on <= '2015-04-03 13:59:17.649115') AND (spree_prices.amount IS NOT NULL) AND "spree_prices"."currency" = 'USD' LIMIT 12 OFFSET 0) subquery_for_count Spree::Product Load (0.3ms) SELECT "spree_products". FROM "spree_products" INNER JOIN "spree_variants" ON "spree_variants"."product_id" = "spree_products"."id" AND "spree_variants"."is_master" = 't' AND "spree_variants"."deleted_at" IS NULL INNER JOIN "spree_prices" ON "spree_prices"."variant_id" = "spree_variants"."id" AND "spree_prices"."deleted_at" IS NULL WHERE "spree_products"."deleted_at" IS NULL AND ("spree_products".deleted_at IS NULL or "spree_products".deleted_at >= '2015-04-03 13:59:17.648583') AND ("spree_products".available_on <= '2015-04-03 13:59:17.649115') AND (spree_prices.amount IS NOT NULL) AND "spree_prices"."currency" = 'USD' LIMIT 12 OFFSET 0 Spree::Variant Load (0.2ms) SELECT "spree_variants".* FROM "spree_variants" WHERE "spree_variants"."deleted_at" IS NULL AND "spree_variants"."is_master" = 't' AND "spree_variants"."product_id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) Spree::Price Load (0.2ms) SELECT "spree_prices".* FROM "spree_prices" WHERE "spree_prices"."deleted_at" IS NULL AND "spree_prices"."variant_id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) Spree::Image Load (0.3ms) SELECT "spree_assets".* FROM "spree_assets" WHERE "spree_assets"."type" IN ('Spree::Image') AND "spree_assets"."viewable_type" = 'Spree::Variant' AND "spree_assets"."viewable_id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) ORDER BY "spree_assets"."position" ASC Rendered /Users/tini8/.rvm/gems/ruby-2.2.1@rails420/gems/spree_frontend-3.0.0/app/views/spree/shared/_products.html.erb (447.9ms) Rendered /Users/tini8/.rvm/gems/ruby-2.2.1@rails420/gems/spree_frontend-3.0.0/app/views/spree/home/index.html.erb within spree/layouts/spree_application (622.9ms) Rendered /Users/tini8/.rvm/gems/ruby-2.2.1@rails420/gems/spree_frontend-3.0.0/app/views/spree/shared/_head.html.erb (189.1ms) Spree::Tracker Load (0.2ms) SELECT "spree_trackers".* FROM "spree_trackers" WHERE "spree_trackers"."active" = 't' ORDER BY "spree_trackers"."id" ASC LIMIT 1 Rendered /Users/tini8/.rvm/gems/ruby-2.2.1@rails420/gems/spree_frontend-3.0.0/app/views/spree/shared/_google_analytics.js.erb (1.1ms) Deface: 1 overrides found for 'spree/shared/_nav_bar' Deface: 'auth_shared_login_bar' matched 1 times with 'li#search-bar' Deface: [ERROR] The original source for 'auth_shared_login_bar' has changed, this override should be reviewed to ensure it's still valid. (0.2ms) SELECT MAX("spree_taxons"."updated_at") FROM "spree_taxons" WHERE "spree_taxons"."parent_id" IS NULL Spree::Taxon Load (0.1ms) SELECT "spree_taxons".* FROM "spree_taxons" WHERE "spree_taxons"."parent_id" IS NULL ORDER BY "spree_taxons"."lft" Rendered /Users/tini8/.rvm/gems/ruby-2.2.1@rails420/gems/spree_frontend-3.0.0/app/views/spree/shared/_search.html.erb (4.5ms) Rendered /Users/tini8/.rvm/gems/ruby-2.2.1@rails420/gems/spree_frontend-3.0.0/app/views/spree/shared/_nav_bar.html.erb (91.6ms) Rendered /Users/tini8/.rvm/gems/ruby-2.2.1@rails420/gems/spree_frontend-3.0.0/app/views/spree/shared/_main_nav_bar.html.erb (2.8ms) Rendered /Users/tini8/.rvm/gems/ruby-2.2.1@rails420/gems/spree_frontend-3.0.0/app/views/spree/shared/_header.html.erb (292.5ms) Rendered /Users/tini8/.rvm/gems/ruby-2.2.1@rails420/gems/spree_frontend-3.0.0/app/views/spree/shared/_sidebar.html.erb (0.2ms) Completed 200 OK in 1593ms (Views: 1582.2ms | ActiveRecord: 3.4ms)

development 上 deface 很慢,production 好一些。

生产环境没这么慢,都有 cache 的。

### 开发模式
Completed 200 OK in 2323ms (Views: 2284.9ms | ActiveRecord: 19.6ms)

###生产模式
Completed 200 OK in 33ms (Views: 17.1ms | ActiveRecord: 7.2ms)

这是我机器上同一个页面的在生产模式和开发模式下的速度差异。基本不用担心。

另外把 development 的 config.assets.debug 设为 false,也能提升一点速度。

#3 楼 @beiersi 咦?生产模式的 views 是怎么提速的?我记得 erb 模板没缓存的,slim 模板有缓存

#5 楼 @tini8 生产模式下启用了 cache 的,fragment cache。

#5 楼 @tini8 #6 楼 @beiersi 生产模式可以跑下面命令预编译 deface 替换的模板,这个才是提速的关键。

rake deface:precompile
需要 登录 后方可回复, 如果你还没有账号请 注册新账号