瞎扯淡 什么是 “大型” Rails 项目

a0nqm · 2017年03月06日 · 最后由 griffinqiu 回复于 2017年09月21日 · 4347 次阅读

有听到前辈讲过一些”“大型”Rails 项目的管理方法,比如抽 Service、拆 Gem 等。 我认为项目“大小”的定义很模糊。所以多少行的项目算得上是“大型”呢?

比如我公司里的这个项目,代码统计如下:

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |  4874 |  3808 |      68 |     336 |   4 |     9 |
| Helpers              |   331 |   168 |       0 |      20 |   0 |     6 |
| Jobs                 |    23 |    19 |       3 |       3 |   1 |     4 |
| Models               |  4355 |  2341 |      50 |     241 |   4 |     7 |
| Mailers              |     0 |     0 |       0 |       0 |   0 |     0 |
| Javascripts          |  3917 |  2717 |       0 |     343 |   0 |     5 |
| Libraries            |   714 |   515 |       7 |      55 |   7 |     7 |
| Controller tests     |   340 |   270 |       7 |      29 |   4 |     7 |
| Helper tests         |   211 |   180 |       8 |       6 |   0 |    28 |
| Model tests          |   318 |   117 |      13 |      26 |   2 |     2 |
| Mailer tests         |     0 |     0 |       0 |       0 |   0 |     0 |
| Integration tests    |     0 |     0 |       0 |       0 |   0 |     0 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                | 15083 | 10135 |     156 |    1059 |   6 |     7 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 9568     Test LOC: 567     Code to Test Ratio: 1:0.1

这种规模算是小型还是大型呢?

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |  2976 |  2367 |      49 |     380 |   7 |     4 |
| Helpers              |   390 |   317 |       2 |      35 |  17 |     7 |
| Models               |  6214 |  4902 |      90 |     467 |   5 |     8 |
| Mailers              |     0 |     0 |       0 |       0 |   0 |     0 |
| Libraries            |  1873 |  1555 |      50 |     147 |   2 |     8 |
| Controller tests     |  1315 |  1031 |      24 |       3 |   0 |   341 |
| Helper tests         |     0 |     0 |       0 |       0 |   0 |     0 |
| Model tests          |  4140 |  3462 |      38 |       8 |   0 |   430 |
| Mailer tests         |     0 |     0 |       0 |       0 |   0 |     0 |
| Integration tests    |     0 |     0 |       0 |       0 |   0 |     0 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                | 16908 | 13634 |     253 |    1040 |   4 |    11 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 9141     Test LOC: 4493     Code to Test Ratio: 1:0.5

我们项目拆的七零八落的,这是其中一个

User.count >= 10_000_000

吧?

+----------------------+--------+--------+---------+---------+-----+-------+
| Name                 |  Lines |    LOC | Classes | Methods | M/C | LOC/M |
+----------------------+--------+--------+---------+---------+-----+-------+
| Controllers          |  14970 |  12206 |     321 |    2080 |   6 |     3 |
| Helpers              |   3017 |   2631 |       0 |     270 |   0 |     7 |
| Jobs                 |   1534 |   1256 |      63 |     122 |   1 |     8 |
| Models               |  42755 |  30393 |     495 |    3063 |   6 |     7 |
| Mailers              |    199 |    166 |      10 |      16 |   1 |     8 |
| Javascripts          |   7686 |   5926 |     182 |    1165 |   6 |     3 |
| Libraries            |  16079 |  13358 |     145 |    1151 |   7 |     9 |
| Tasks                |   3899 |   3405 |       4 |      73 |  18 |    44 |
| Controller tests     |   8885 |   7331 |     207 |     948 |   4 |     5 |
| Helper tests         |    329 |    255 |      15 |      27 |   1 |     7 |
| Model tests          |  25440 |  20199 |     317 |    1751 |   5 |     9 |
| Mailer tests         |    136 |    112 |       9 |      18 |   2 |     4 |
| Job tests            |    798 |    645 |      25 |      78 |   3 |     6 |
| Integration tests    |    205 |    155 |       2 |      17 |   8 |     7 |
+----------------------+--------+--------+---------+---------+-----+-------+
| Total                | 125932 |  98038 |    1795 |   10779 |   6 |     7 |
+----------------------+--------+--------+---------+---------+-----+-------+
  Code LOC: 69341     Test LOC: 28697     Code to Test Ratio: 1:0.4

知人的,其实 KnewOne 项目全部代码量比这个还多... 没存代码就是

jasl 回复

495 个 Model ...

我表示从没做过超过 50 个 Model 的项目

看了一下,有超过 50 个 Model 的,但应该在 100 以内

这个数据使用什么工具统计的?

huacnlee 回复

KO 也肯定超过 50 了... 知人这边是因为企业系统,模型围绕业务做的,业务多了模型数量就上去了... 不过可能那统计有水分,不少都是虚拟模型应该

接近 600 个 Model,看来我做的是大型 Rails 项目😂

helapu 回复
rake stats
jasl 回复

感觉各类业务多了,应该拆成小项目来搞,这样单独看各类业务的时候不会觉得东西好多。

想想那么多 Model,那么多业务函数,跑个 CI 得要多久啊

a0nqm 回复

😂 再看了下 rake 下的其他功能

huacnlee 回复

我的 mac mini 要半个小时多吧

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          | 23878 | 13391 |     370 |    1760 |   4 |     5 |
| Helpers              |  2381 |  1989 |       1 |     300 | 300 |     4 |
| Models               | 21610 | 14640 |     335 |    1894 |   5 |     5 |
| Mailers              |   745 |   625 |      27 |      91 |   3 |     4 |
| Javascripts          | 18787 | 14934 |      35 |    2317 |  66 |     4 |
| Libraries            |   702 |   552 |      14 |      71 |   5 |     5 |
| Controller specs     | 11811 | 10239 |       2 |      17 |   8 |   600 |
| Decorator specs      |   476 |   409 |       0 |       0 |   0 |     0 |
| Feature specs        |  1082 |   910 |       0 |       1 |   0 |   908 |
| Form specs           |  1505 |  1363 |       0 |       0 |   0 |     0 |
| Helper specs         |   156 |    63 |       0 |       0 |   0 |     0 |
| Lib specs            |    48 |    41 |       0 |       0 |   0 |     0 |
| Mailer specs         |   393 |   329 |       0 |       0 |   0 |     0 |
| Model specs          | 18993 | 13438 |       0 |      10 |   0 |  1341 |
| Policy specs         |    36 |    31 |       0 |       0 |   0 |     0 |
| Presenter specs      |   239 |   214 |       0 |       0 |   0 |     0 |
| Query specs          |   174 |   156 |       0 |       0 |   0 |     0 |
| Request specs        |   713 |   600 |       0 |       0 |   0 |     0 |
| Serializer specs     |   784 |   419 |       0 |       0 |   0 |     0 |
| Service specs        |  5376 |  4762 |       1 |      15 |  15 |   315 |
| Uploader specs       |    51 |    41 |       0 |       0 |   0 |     0 |
| Validator specs      |    75 |    64 |       1 |       1 |   1 |    62 |
| Value specs          |   268 |   225 |       0 |       0 |   0 |     0 |
| View specs           |  7107 |  6745 |       0 |       4 |   0 |  1684 |
| Worker specs         |   114 |   101 |       0 |       0 |   0 |     0 |
| Wrapper specs        |    32 |    26 |       0 |       0 |   0 |     0 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                | 117536 | 86307 |     786 |    6481 |   8 |    11 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 46131     Test LOC: 40176     Code to Test Ratio: 1:0.9
jasl 回复

说起 CI 时间的话,想起 4 年前的一个 rails 项目,CI 上跑一次完整回归大概 5 个多小时

rainchen 回复

凶残了...

rainchen 回复

好荣幸 4 年前跟大神一起做个这个大项目。

jasl 回复

GitLab 算不算大项目?

Model 136,许多还应该是抽象实现,表数量:63 张

不过跑 CI 也要两三个小时以上,这东西搞了 UI 测试哪套,改动起来特别费神。


Discourse - 175 Models

huacnlee 回复

那俩实际比模型数复杂的多吧... 最近刚好在看

jasl 回复

看过 Discourse,比预想的要复杂得多。

a0nqm 回复

Gitlab 更工整一些,Discourse 那个太乱了。。

huacnlee 回复

肯定算大型项目了 Gitlab 还拆了好多到子项目里去,包括 Redmine 模型的代码都不多的,模型只能算核心数据结构吧,这几个项目的 lib 目录都是大头...

rainchen 回复

你们 CI 用的是什么工具?

我也觉得 gitlab 叫大型项目有点反直觉,毕竟没有人管 git 或者 gerrit 叫大型软件…

nouse 回复

但其实 Gitlab 不算小。 不过真要说大型项目,Github 绝对算一个。

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |  8871 |  7469 |     102 |     823 |   8 |     7 |
| Helpers              |  2309 |  1960 |       2 |     227 | 113 |     6 |
| Models               | 29324 | 23043 |     415 |    2629 |   6 |     6 |
| Libraries            | 10119 |  7971 |     175 |     928 |   5 |     6 |
| Functional tests     |   326 |   274 |       4 |      24 |   6 |     9 |
| Unit tests           |     0 |     0 |       0 |       0 |   0 |     0 |
| Controller specs     | 12005 | 10352 |       0 |      37 |   0 |   277 |
| Helper specs         |   575 |   487 |       0 |       4 |   0 |   119 |
| Integration specs    |   441 |   360 |       0 |       8 |   0 |    43 |
| Lib specs            |  5997 |  4961 |       1 |      26 |  26 |   188 |
| Mailer specs         |   685 |   621 |       0 |       0 |   0 |     0 |
| Model specs          | 45021 | 37825 |       3 |     181 |  60 |   206 |
| Presenter specs      |   290 |   240 |       0 |       0 |   0 |     0 |
| Request specs        | 20972 | 18067 |       0 |     105 |   0 |   170 |
| Routing specs        |    23 |    19 |       0 |       0 |   0 |     0 |
| Service specs        |  4590 |  3812 |      20 |      82 |   4 |    44 |
| Task specs           |   109 |    88 |       0 |       0 |   0 |     0 |
| Util specs           |    42 |    36 |       0 |       2 |   0 |    16 |
| Worker specs         |   321 |   284 |       0 |       0 |   0 |     0 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                | 142020 | 117869 |     722 |    5076 |   7 |    21 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 40443     Test LOC: 77426     Code to Test Ratio: 1:1.9
wpzero 回复

够大

a0nqm 回复

哈哈,刚刚没有算 rspec 的代码

huacnlee 回复

而且 UI 经常大改 😂

$ find . -name "*.cpp" -type f | xargs cat | wc -l
365582

😂

plan9 回复

.......

zchar [远程] 彩程知人招募工程师 (15 - 30k) 提及了此话题。 05月31日 15:24
+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          | 12577 | 10270 |     287 |    1363 |   4 |     5 |
| Helpers              |  3926 |  3323 |       0 |     499 |   0 |     4 |
| Models               | 21963 | 17862 |     263 |    2520 |   9 |     5 |
| Mailers              |   355 |   289 |       5 |      23 |   4 |    10 |
| Javascripts          | 27469 | 21880 |     158 |    4164 |  26 |     3 |
| Libraries            |  2330 |  1946 |      28 |     204 |   7 |     7 |
| Controller tests     | 24791 | 20449 |     252 |      91 |   0 |   222 |
| Helper tests         |  3404 |  2774 |      23 |       5 |   0 |   552 |
| Model tests          | 25046 | 20691 |     213 |      43 |   0 |   479 |
| Mailer tests         |   679 |   562 |       4 |       4 |   1 |   138 |
| Integration tests    |   646 |   506 |       8 |       5 |   0 |    99 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                | 123186 | 100552 |    1241 |    8921 |   7 |     9 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 55570     Test LOC: 44982     Code to Test Ratio: 1:0.8

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