Skip to content

Instantly share code, notes, and snippets.

@roxblnfk
Last active July 14, 2021 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roxblnfk/9b292ee0dd8396f52fc50f609aac97ff to your computer and use it in GitHub Desktop.
Save roxblnfk/9b292ee0dd8396f52fc50f609aac97ff to your computer and use it in GitHub Desktop.
ORM v1 vs v2

Связанная issue: cycle/orm#149 В основе проект yiisoft/demo

Выполняемая команда на добавление фикстур: yii fixture/add 200

  • данные рандомные и относительно рандомного объёма (между двумя запусками могут отличаться на 50%)
  • Сущности: User, Post, Comment, Tag
  • Схема:
    [comment] :: default.comment
       Entity     : App\Blog\Entity\Comment
       Mapper     : App\Blog\Comment\CommentMapper
       Constrain  : App\Blog\Comment\Scope\PublicScope
       Repository : App\Blog\Comment\CommentRepository
       Primary key: id
       Fields     :
         (property -> db.field -> typecast)
         id -> id -> int
         public -> public -> int
         content -> content
         created_at -> created_at -> datetime
         updated_at -> updated_at -> datetime
         published_at -> published_at -> datetime
         deleted_at -> deleted_at -> datetime
         user_id -> user_id -> int
         post_id -> post_id -> int
       Relations  :
         comment->user belongs to user eager load cascaded
           not null comment.user_id <==> user.id
         comment->post belongs to post promise load cascaded
           not null comment.post_id <==> post.id
    [post] :: default.post
       Entity     : App\Blog\Entity\Post
       Mapper     : App\Blog\Post\PostMapper
       Constrain  : App\Blog\Post\Scope\PublicScope
       Repository : App\Blog\Post\PostRepository
       Primary key: id
       Fields     :
         (property -> db.field -> typecast)
         id -> id -> int
         slug -> slug
         title -> title
         public -> public -> int
         content -> content
         created_at -> created_at -> datetime
         updated_at -> updated_at -> datetime
         published_at -> published_at -> datetime
         deleted_at -> deleted_at -> datetime
         user_id -> user_id -> int
       Relations  :
         post->user belongs to user promise load cascaded
           not null post.user_id <==> user.id
         post->tags many to many tag promise load cascaded
           not null post.id <= postTag.post_id|postTag.tag_id => tag.id
         post->comments has many comment promise load cascaded
           not null post.id <==> comment.post_id
    [postTag] :: default.post_tag
       Entity     : App\Blog\Entity\PostTag
       Mapper     : Cycle\ORM\Mapper\Mapper
       Constrain  : no constrain
       Repository : Cycle\ORM\Select\Repository
       Primary key: id
       Fields     :
         (property -> db.field -> typecast)
         id -> id -> int
         post_id -> post_id -> int
         tag_id -> tag_id -> int
       No relations
    [tag] :: default.tag
       Entity     : App\Blog\Entity\Tag
       Mapper     : Cycle\ORM\Mapper\Mapper
       Constrain  : no constrain
       Repository : App\Blog\Tag\TagRepository
       Primary key: id
       Fields     :
         (property -> db.field -> typecast)
         id -> id -> int
         label -> label
         created_at -> created_at -> datetime
       Relations  :
         tag->posts many to many post promise load cascaded
           not null tag.id <= postTag.tag_id|postTag.post_id => post.id
    [user] :: default.user
       Entity     : App\User\User
       Mapper     : Cycle\ORM\Mapper\Mapper
       Constrain  : no constrain
       Repository : App\User\UserRepository
       Primary key: id
       Fields     :
         (property -> db.field -> typecast)
         id -> id -> int
         login -> login
         passwordHash -> password_hash
         created_at -> created_at -> datetime
         updated_at -> updated_at -> datetime
       Relations  :
         user->posts has many post promise load cascaded
           not null user.id <==> post.user_id
         user->comments has many comment promise load cascaded
           not null user.id <==> comment.user_id
    

yii fixture/add 200 Связи заполнены с одной стороны

Запуск на пустой базе

Time :: Usage :: PeakBefore generation:
 0.00s 10.21MiB 10.21MiB
Before transaction:
 94.77s 54.43MiB 54.43MiB
After transaction:
 150.34s 306.18MiB 311.28MiB

Запуск на непустой базе

Time :: Usage :: PeakBefore generation:
 0.00s 10.21MiB 10.21MiB
Before transaction:
 96.22s 54.43MiB 54.43MiB     //на этапе подготовки данных пару секунд сожрал сторм с индексацией
After transaction:
 156.84s 323.19MiB 329.99MiB

Heap: 323.19 - 54.43 = 268,76 ??
Transaction: 329.99 - 268,76 = 61,23

yii fixture/add 200 Двусторонние связи заполнены (реверт коммита)

Запуск на пустой базе

Time :: Usage :: PeakBefore generation:
 0.00s 10.21MiB 10.21MiB
Before transaction:
 93.48s 54.43MiB 54.43MiB
After transaction:
 313.19s 418.45MiB 423.55MiB

Запуск на заполненной базе

Time :: Usage :: PeakBefore generation:
 0.00s 10.21MiB 10.21MiB
Before transaction:
 96.86s 79.95MiB 79.95MiB
PHP Fatal error:  Allowed memory size of 512MB exhausted 

Лимит 2G:
Time :: Usage :: PeakBefore generation:
 0.00s 10.21MiB 10.21MiB
Before transaction:
 97.86s 83.35MiB 83.35MiB
After transaction:
 372.80s 602.15MiB 612.36MiB

Переписанная версия v2-master:

  • иной способ обработки связей и сортировки: проход по сущностям производится без углубления в стек вызовов
  • уход от подписок на изменения полей
  • данные не хранятся в командах и т.д. Без оптимизации алгоритмов на быстродействие. 13 старых тестов фейлятся. Ветка Коммит

yii fixture/add 200 Связи заполнены с одной стороны

Запуск на пустой базе

Time :: Usage :: PeakBefore generation:
 0.00s 10.21MiB 10.21MiB
Before transaction:
 94.69s 52.73MiB 52.73MiB
After transaction:
 298.10s 205.82MiB 216.03MiB

Запуск на непустой базе

Time :: Usage :: PeakBefore generation:
 0.00s 10.21MiB 10.21MiB
Before transaction:
 93.97s 52.73MiB 52.73MiB
After transaction:
 268.40s 185.41MiB 192.21MiB

Heap: 185.41 - 52.73 = 132.68
Transaction: 192.21 - 132.68 = 59.53

yii fixture/add 200 Двусторонние связи заполнены (реверт коммита)

Запуск на пустой базе

Time :: Usage :: PeakBefore generation:
 0.00s 10.21MiB 10.21MiB
Before transaction:
 95.10s 56.13MiB 56.13MiB
After transaction:
 214.21s 190.51MiB 195.61MiB

Запуск на заполненной базе

Time :: Usage :: PeakBefore generation:
 0.00s 10.21MiB 10.21MiB
Before transaction:
 96.59s 78.25MiB 78.25MiB
After transaction:
 454.97s 236.44MiB 241.54MiB

v2 с поддержкой композитных ключей и переписанной кучей https://github.com/cycle/orm/commit/569c332ece00ce5e3284dddbc330adc85b48421c

yii fixture/add 200 Двусторонние связи заполнены (реверт коммита)

Запуск на пустой базе

Time :: Usage :: PeakBefore generation:
 0.00s 10.21MiB 10.21MiB
Before transaction:
 94.52s 56.13MiB 56.13MiB
After transaction:
 324.61s 435.46MiB 440.56MiB

Запуск на заполненной базе

Time :: Usage :: PeakBefore generation:
 0.00s 10.21MiB 10.21MiB
Before transaction:
 99.68s 96.96MiB 96.96MiB
After transaction:
 389.43s 656.58MiB 668.49MiB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment