-
Notifications
You must be signed in to change notification settings - Fork 136
Efremov HW 2 #86
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
Open
EfremovEvgeniy
wants to merge
15
commits into
hardcode-dev:master
Choose a base branch
from
EfremovEvgeniy:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Efremov HW 2 #86
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
65a344f
add runner
bf19263
add ruby_prof_printers.rb
b6cc120
memory_profiler_runner.rb
6f3b53b
stackprof_report.rb
c43d0c1
WIP streaming calculation report
f93e3a4
use Oj::StreamWriter
7b6b456
fix string split
24ffd9e
fix sort by
0a6f89d
add rspec
728492b
case study
88c9d6a
add test file
394613e
some refactoring
4329445
add screenshot from massif
bc6879a
fix readme
d22db9f
fix performance spec
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # Case-study оптимизации | ||
|
|
||
| ## Актуальная проблема | ||
| В нашем проекте возникла серьёзная проблема. | ||
|
|
||
| Необходимо было обработать файл с данными, чуть больше ста мегабайт. | ||
|
|
||
| У нас уже была программа на `ruby`, которая умела делать нужную обработку. | ||
|
|
||
| Она успешно работала на файлах размером пару мегабайт, но для большого файла она работала слишком долго, и не было понятно, закончит ли она вообще работу за какое-то разумное время. | ||
|
|
||
| Я решил исправить эту проблему, оптимизировав эту программу. | ||
|
|
||
| ## Формирование метрики | ||
| Для того, чтобы понимать, дают ли мои изменения положительный эффект на быстродействие программы я придумал использовать такую метрику: количество память показанное memory_profiler на 30_000 строк | ||
|
|
||
| ## Гарантия корректности работы оптимизированной программы | ||
| Программа поставлялась с тестом. Выполнение этого теста в фидбек-лупе позволяет не допустить изменения логики программы при оптимизации. | ||
|
|
||
| ## Feedback-Loop | ||
| Для того, чтобы иметь возможность быстро проверять гипотезы я выстроил эффективный `feedback-loop`, который позволил мне получать обратную связь по эффективности сделанных изменений за 30-40 сек | ||
|
|
||
| Вот как я построил: | ||
| - прогон программы, фиксирование потребляемой оперативной памяти в конце программы | ||
| - прогон профайлерами, фиксирование точки роста | ||
| - изменения в коде | ||
| - тесты | ||
|
|
||
|
|
||
| ## Вникаем в детали системы, чтобы найти главные точки роста | ||
| Для того, чтобы найти "точки роста" для оптимизации я воспользовался memory_profiler, stackprof, ruby-prof | ||
|
|
||
| Вот какие проблемы удалось найти и решить | ||
|
|
||
| ### Ваша находка №1 | ||
| - memory_profiler на 30_000 строк показывал 3.78 гб аллоцированной памяти. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не актуальное число |
||
| - переписать программу иным способом, а именно, не созранять всех юзеров и сессии в памяти, а на лету за один проход все посчитать, собрать report и записать в файл | ||
| В первой итерации я так и сделал, так как было понятно, что программа в первоначальном виде не имеет шансов уложится в бюджет по памяти. | ||
| Переписал программу "в лоб", не особо задумываясь об потреблении памяти, просто чтобы работало как и было и за один проход по исходному файлу составляла report. | ||
| - memory_profiler на 30_000 строк показал результат 43.12 mb (при этом время выполнения программы без профилирования на data_large.txt стало 25 секунд, что меньше, чем лучший результат после оптимизации по CPU) | ||
| - главная точка роста стала в String#split | ||
|
|
||
| ### Ваша находка №2 | ||
| - ruby prof c RubyProf.measure_mode = RubyProf::MEMORY показал главную точку роста в String#split (52 процента) | ||
| - попробовал сделать с блоком в split, чуть уменьшив каждый массив для сессий и юзеров | ||
| - memory_profiler на 30_000 строк показал результат 41 mb | ||
| - String#split все еще на первом месте, но рядом уже sort_by по строкам дат | ||
|
|
||
| ### Ваша находка №3 | ||
| - ruby prof c RubyProf.measure_mode = RubyProf::MEMORY показал главную точку роста в Enumerable#sort_by (31 процент) | ||
| - использовал SortedSet | ||
| - memory_profiler на 30_000 строк показал результат 41 mb | ||
| - Enumerable#sort_by пропал из топа | ||
|
|
||
| Еще добавил magic comment. | ||
| Поменял где надо массивы на Set. | ||
|
|
||
| ## Результаты | ||
| В результате проделанной оптимизации наконец удалось обработать файл с данными. | ||
|
|
||
| ## Защита от регрессии производительности | ||
| Для защиты от потери достигнутого прогресса при дальнейших изменениях программы написал performance rspec тест | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| require 'memory_profiler' | ||
| require_relative 'task-2' | ||
|
|
||
| report = MemoryProfiler.report do | ||
| work(file_name: ENV['FILE_NAME'], gc_disabled: false) | ||
| end | ||
| report.pretty_print(scale_bytes: true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| require_relative 'task-2.rb' | ||
| require 'minitest/autorun' | ||
|
|
||
| class TestMe < Minitest::Test | ||
| def setup | ||
| File.write('result.json', '') | ||
| File.write('data.txt', | ||
| 'user,0,Leida,Cira,0 | ||
| session,0,0,Safari 29,87,2016-10-23 | ||
| session,0,1,Firefox 12,118,2017-02-27 | ||
| session,0,2,Internet Explorer 28,31,2017-03-28 | ||
| session,0,3,Internet Explorer 28,109,2016-09-15 | ||
| session,0,4,Safari 39,104,2017-09-27 | ||
| session,0,5,Internet Explorer 35,6,2016-09-01 | ||
| user,1,Palmer,Katrina,65 | ||
| session,1,0,Safari 17,12,2016-10-21 | ||
| session,1,1,Firefox 32,3,2016-12-20 | ||
| session,1,2,Chrome 6,59,2016-11-11 | ||
| session,1,3,Internet Explorer 10,28,2017-04-29 | ||
| session,1,4,Chrome 13,116,2016-12-28 | ||
| user,2,Gregory,Santos,86 | ||
| session,2,0,Chrome 35,6,2018-09-21 | ||
| session,2,1,Safari 49,85,2017-05-22 | ||
| session,2,2,Firefox 47,17,2018-02-02 | ||
| session,2,3,Chrome 20,84,2016-11-25 | ||
| ') | ||
| end | ||
|
|
||
| def test_result | ||
| work | ||
| expected_result = JSON.parse('{"totalUsers":3,"uniqueBrowsersCount":14,"totalSessions":15,"allBrowsers":"CHROME 13,CHROME 20,CHROME 35,CHROME 6,FIREFOX 12,FIREFOX 32,FIREFOX 47,INTERNET EXPLORER 10,INTERNET EXPLORER 28,INTERNET EXPLORER 35,SAFARI 17,SAFARI 29,SAFARI 39,SAFARI 49","usersStats":{"Leida Cira":{"sessionsCount":6,"totalTime":"455 min.","longestSession":"118 min.","browsers":"FIREFOX 12, INTERNET EXPLORER 28, INTERNET EXPLORER 28, INTERNET EXPLORER 35, SAFARI 29, SAFARI 39","usedIE":true,"alwaysUsedChrome":false,"dates":["2017-09-27","2017-03-28","2017-02-27","2016-10-23","2016-09-15","2016-09-01"]},"Palmer Katrina":{"sessionsCount":5,"totalTime":"218 min.","longestSession":"116 min.","browsers":"CHROME 13, CHROME 6, FIREFOX 32, INTERNET EXPLORER 10, SAFARI 17","usedIE":true,"alwaysUsedChrome":false,"dates":["2017-04-29","2016-12-28","2016-12-20","2016-11-11","2016-10-21"]},"Gregory Santos":{"sessionsCount":4,"totalTime":"192 min.","longestSession":"85 min.","browsers":"CHROME 20, CHROME 35, FIREFOX 47, SAFARI 49","usedIE":false,"alwaysUsedChrome":false,"dates":["2018-09-21","2018-02-02","2017-05-22","2016-11-25"]}}}') | ||
| assert_equal expected_result, JSON.parse(File.read('result.json')) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| require 'rspec-benchmark' | ||
| require_relative 'task-2.rb' | ||
|
|
||
| RSpec.configure do |config| | ||
| config.include RSpec::Benchmark::Matchers | ||
| end | ||
|
|
||
| describe 'Performance' do | ||
| let(:file_name) { '10_000.txt' } | ||
|
|
||
| it 'allocates less then 12mb' do | ||
| expect do | ||
| work(file_name: file_name) | ||
| end.to perform_allocation(12 * 1024 * 1024).memory | ||
| end | ||
| end |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| require 'ruby-prof' | ||
| require_relative 'task-2' | ||
|
|
||
| RubyProf.measure_mode = RubyProf::MEMORY | ||
|
|
||
| result = RubyProf.profile do | ||
| work(file_name: ENV['FILE_NAME'], gc_disabled: true) | ||
| end | ||
|
|
||
| printer = RubyProf::FlatPrinter.new(result) | ||
| printer.print(File.open('ruby_prof_memory_reports/flat.txt', 'w+')) | ||
|
|
||
| printer = RubyProf::DotPrinter.new(result) | ||
| printer.print(File.open('ruby_prof_memory_reports/graphiz.dot', 'w+')) | ||
|
|
||
| printer = RubyProf::GraphHtmlPrinter.new(result) | ||
| printer.print(File.open('ruby_prof_memory_reports/graph.html', 'w+')) | ||
|
|
||
| printer = RubyProf::CallStackPrinter.new(result) | ||
| printer.print(File.open('ruby_prof_memory_reports/callstack.html', 'w+')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| require 'ruby-prof' | ||
| require_relative 'task-2' | ||
|
|
||
| RubyProf.measure_mode = RubyProf::ALLOCATIONS | ||
|
|
||
| result = RubyProf.profile do | ||
| work(file_name: ENV['FILE_NAME'], gc_disabled: true) | ||
| end | ||
|
|
||
| printer = RubyProf::FlatPrinter.new(result) | ||
| printer.print(File.open('ruby_prof_reports/flat.txt', 'w+')) | ||
|
|
||
| printer = RubyProf::DotPrinter.new(result) | ||
| printer.print(File.open('ruby_prof_reports/graphiz.dot', 'w+')) | ||
|
|
||
| printer = RubyProf::GraphHtmlPrinter.new(result) | ||
| printer.print(File.open('ruby_prof_reports/graph.html', 'w+')) | ||
|
|
||
| printer = RubyProf::CallStackPrinter.new(result) | ||
| printer.print(File.open('ruby_prof_reports/callstack.html', 'w+')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| require_relative 'task-2' | ||
|
|
||
| work(file_name: ENV['FILE_NAME']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| require 'stackprof' | ||
| require_relative 'task-2' | ||
|
|
||
| StackProf.run(mode: :object, out: 'stackprof_reports/stackprof.dump', raw: true) do | ||
| work(file_name: ENV['FILE_NAME'], gc_disabled: true) | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
не совсем правильно
memory_profiler - это профилировщик, его нельзя использовать для замера памяти
так как само его использование увеличивает использование памяти
то есть надо всегда разделять два процесса - бенчмаркинг и профайлинг