From 2ca31485ccff739523297cf1c5670ddc44eec458 Mon Sep 17 00:00:00 2001 From: lightalloy Date: Sun, 26 Jan 2025 00:12:28 +0300 Subject: [PATCH 01/10] Optimized to 58s --- Gemfile | 4 + Gemfile.lock | 17 + case.md | 11 + result.json | 1 + ruby_prof_reports/callstack.html | 1046 ++++++++++++++++ ruby_prof_reports/callstack_fast_sample.html | 1078 ++++++++++++++++ ruby_prof_reports/callstack_old.html | 1002 +++++++++++++++ .../callstack_optimized_each.html | 1086 +++++++++++++++++ ruby_prof_reports/callstack_sample_large.html | 1078 ++++++++++++++++ task-1.rb | 133 +- 10 files changed, 5394 insertions(+), 62 deletions(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 case.md create mode 100644 result.json create mode 100644 ruby_prof_reports/callstack.html create mode 100644 ruby_prof_reports/callstack_fast_sample.html create mode 100644 ruby_prof_reports/callstack_old.html create mode 100644 ruby_prof_reports/callstack_optimized_each.html create mode 100644 ruby_prof_reports/callstack_sample_large.html diff --git a/Gemfile b/Gemfile new file mode 100644 index 00000000..2068aa3c --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +ruby '3.2.0' +source "https://rubygems.org" + +gem 'ruby-prof' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 00000000..4bfff8b1 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,17 @@ +GEM + remote: https://rubygems.org/ + specs: + ruby-prof (1.7.0) + +PLATFORMS + ruby + x86_64-linux + +DEPENDENCIES + ruby-prof + +RUBY VERSION + ruby 3.2.0p0 + +BUNDLED WITH + 2.5.16 diff --git a/case.md b/case.md new file mode 100644 index 00000000..59d40352 --- /dev/null +++ b/case.md @@ -0,0 +1,11 @@ +- профилировала ruby-prof +- медленно `Array#each` => `Array#select` +- перебирает сессии по юзерам, когда можно этого избежать + +- потом был медленный collect_stats_from_users + - вижу, что много раз проходится по юзерам + - развернула методы - 92с + +- закэшировала переменные - 82 + + diff --git a/result.json b/result.json new file mode 100644 index 00000000..ad485631 --- /dev/null +++ b/result.json @@ -0,0 +1 @@ +{"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"]}}} diff --git a/ruby_prof_reports/callstack.html b/ruby_prof_reports/callstack.html new file mode 100644 index 00000000..05986ec5 --- /dev/null +++ b/ruby_prof_reports/callstack.html @@ -0,0 +1,1046 @@ + + + + + ruby-prof call tree + + + + + + + +
+
+ Call tree for application task-1.rb
Generated on 2025-01-26 00:09:59 +0300 + with options {}
+
+
+ Threshold: + + + + + +
+ + + + +
+ Thread: 260, Fiber: 240 (100.00% ~ 58.37264639999921) +
    + +
  • + +100.00% (100.00%) [global]# [1 calls, 1 total] +
      +
    • + +100.00% (100.00%) Object#work [1 calls, 1 total] +
        +
      • + +95.90% (95.90%) Array#each [2 calls, 2 total] +
          +
        • + +39.05% (40.72%) Array#map [3000000 calls, 3000000 total] +
            +
          • + +26.47% (67.80%) <Class::Date>#parse [2750940 calls, 2750940 total] +
              +
            • + +4.82% (18.21%) Regexp#match [5501880 calls, 5501880 total] +
            • +
            • + +3.23% (12.20%) String#gsub! [2750940 calls, 2750940 total] +
            • +
            • + +2.07% (7.83%) MatchData#begin [2750940 calls, 2750940 total] +
            • +
            • + +1.25% (4.71%) String#[]= [2750940 calls, 2750940 total] +
            • + + +
            +
          • +
          • + +2.10% (5.38%) Date#iso8601 [2750940 calls, 2750940 total] +
          • +
          • + +1.10% (2.82%) String#upcase [2750940 calls, 2751140 total] +
          • + +
          +
        • +
        • + +12.94% (13.49%) Object#parse_session [2750940 calls, 2750940 total] +
            +
          • + +3.91% (30.23%) String#split [2750940 calls, 6501881 total] +
          • +
          • + +2.62% (20.22%) Array#[] [13754700 calls, 21756580 total] +
          • +
          +
        • +
        • + +4.90% (5.11%) String#split [3250940 calls, 6501881 total] +
        • +
        • + +3.80% (3.97%) Hash#[] [2750940 calls, 2750940 total] +
            +
          • + +1.52% (40.05%) Hash#hash [2750940 calls, 3250949 total] +
          • +
          +
        • +
        • + +3.25% (3.38%) Array#sort [1000000 calls, 1000001 total] +
            +
          • + +1.31% (40.33%) Date#<=> [4784430 calls, 4784430 total] +
          • +
          +
        • +
        • + +3.01% (3.14%) Set#add [2750940 calls, 2750940 total] +
            +
          • + +1.05% (34.85%) Hash#[]= [2750940 calls, 3750941 total] +
          • +
          +
        • +
        • + +2.08% (2.16%) Object#parse_user [500000 calls, 500000 total] +
            + + +
          +
        • +
        • + +1.77% (1.85%) Array#any? [500000 calls, 500000 total] +
            + +
          +
        • +
        • + +1.75% (1.82%) Hash#[]= [1000000 calls, 3750941 total] +
            + + +
          +
        • +
        • + +1.25% (1.30%) String#== [6001880 calls, 6001880 total] +
        • +
        • + +1.23% (1.28%) Array#[] [6001880 calls, 21756580 total] +
        • + + + + + + + + + + + +
        +
      • +
      • + +1.47% (1.47%) Hash#each [1 calls, 2 total] +
          + + +
        +
      • +
      • + +1.21% (1.21%) JSON::Ext::Generator::GeneratorMethods::Hash#to_json [1 calls, 1 total] + +
      • + + + + + + + + + +
      +
    • +
    +
  • + +
+
+ +
+
+ + diff --git a/ruby_prof_reports/callstack_fast_sample.html b/ruby_prof_reports/callstack_fast_sample.html new file mode 100644 index 00000000..a19093eb --- /dev/null +++ b/ruby_prof_reports/callstack_fast_sample.html @@ -0,0 +1,1078 @@ + + + + + ruby-prof call tree + + + + + + + +
+
+ Call tree for application task-1.rb
Generated on 2025-01-25 23:10:53 +0300 + with options {}
+
+
+ Threshold: + + + + + +
+ + + + +
+ Thread: 260, Fiber: 240 (100.00% ~ 0.23113944600027025) +
    + +
  • + +100.00% (100.00%) [global]# [1 calls, 1 total] +
      +
    • + +99.99% (99.99%) Object#work [1 calls, 1 total] +
        +
      • + +58.39% (58.40%) Object#collect_stats_from_users [7 calls, 7 total] +
          +
        • + +58.39% (99.99%) Array#each [7 calls, 8 total] +
            +
          • + +32.10% (54.98%) Array#map [16896 calls, 16896 total] +
              +
            • + +17.84% (55.56%) <Class::Date>#parse [8464 calls, 8464 total] +
                +
              • + +3.63% (20.34%) Regexp#match [16928 calls, 16928 total] +
              • +
              • + +2.22% (12.44%) String#gsub! [8464 calls, 8464 total] +
              • +
              • + +1.51% (8.47%) MatchData#begin [8464 calls, 8464 total] +
              • + + + +
              +
            • +
            • + +1.89% (5.88%) Date#iso8601 [8464 calls, 8464 total] +
            • +
            • + +1.00% (3.12%) String#to_i [16928 calls, 16928 total] +
            • + +
            +
          • +
          • + +2.18% (3.73%) Array#sort [3072 calls, 3073 total] +
              + +
            +
          • +
          • + +1.88% (3.22%) String#+ [24576 calls, 24576 total] +
          • +
          • + +1.79% (3.07%) Array#any? [1536 calls, 1536 total] +
              + + +
            +
          • +
          • + +1.62% (2.78%) Hash#[] [21504 calls, 29968 total] +
          • +
          • + +1.56% (2.68%) Hash#merge [10752 calls, 10752 total] +
          • +
          • + +1.29% (2.21%) Hash#[]= [12288 calls, 22289 total] +
          • +
          • + +1.09% (1.87%) User#attributes [21504 calls, 21504 total] +
          • + + + + + + + + +
          +
        • +
        +
      • +
      • + +36.76% (36.76%) Array#each [1 calls, 8 total] +
          +
        • + +10.95% (29.78%) Object#parse_session [8464 calls, 8464 total] +
            +
          • + +4.56% (41.63%) String#split [8464 calls, 20001 total] +
          • +
          • + +1.94% (17.72%) Array#[] [42320 calls, 66928 total] +
          • +
          +
        • +
        • + +5.13% (13.94%) String#split [10000 calls, 20001 total] +
        • +
        • + +2.79% (7.60%) Hash#[] [8464 calls, 29968 total] +
            +
          • + +1.12% (40.20%) Hash#hash [8464 calls, 10009 total] +
          • +
          +
        • +
        • + +2.31% (6.29%) Set#add [8464 calls, 8464 total] +
            + +
          +
        • +
        • + +1.38% (3.74%) Object#parse_user [1536 calls, 1536 total] +
            + + +
          +
        • + + + + + +
        +
      • +
      • + +1.08% (1.08%) Enumerable#map [1 calls, 1 total] +
          +
        • + +1.08% (99.90%) Set#each [1 calls, 2 total] +
            +
          • + +1.08% (99.94%) Hash#each_key [1 calls, 2 total] +
              +
            • + +1.06% (98.46%) String#upcase [200 calls, 15135 total] +
            • +
            +
          • + +
          +
        • +
        +
      • + + + + + + + + + + +
      +
    • +
    +
  • + +
+
+ +
+
+ + diff --git a/ruby_prof_reports/callstack_old.html b/ruby_prof_reports/callstack_old.html new file mode 100644 index 00000000..f26e422b --- /dev/null +++ b/ruby_prof_reports/callstack_old.html @@ -0,0 +1,1002 @@ + + + + + ruby-prof call tree + + + + + + + +
+
+ Call tree for application task-1.rb
Generated on 2025-01-25 22:38:50 +0300 + with options {}
+
+
+ Threshold: + + + + + +
+ + + + +
+ Thread: 260, Fiber: 240 (100.00% ~ 6.001906517999942) +
    + +
  • + +100.00% (100.00%) [global]# [1 calls, 1 total] +
      +
    • + +100.00% (100.00%) Object#work [1 calls, 1 total] +
        +
      • + +97.26% (97.26%) Array#each [3 calls, 10 total] +
          +
        • + +87.16% (89.61%) Array#select [1536 calls, 1536 total] +
            +
          • + +24.63% (28.26%) String#== [13000704 calls, 13861980 total] +
          • +
          +
        • +
        • + +7.59% (7.80%) Array#all? [8464 calls, 10000 total] +
            +
          • + +4.50% (59.35%) BasicObject#!= [841276 calls, 841276 total] +
              +
            • + +1.52% (33.85%) String#== [841276 calls, 13861980 total] +
            • +
            +
          • +
          +
        • +
        • + +1.07% (1.10%) Array#+ [11736 calls, 11736 total] +
        • + + + + + + +
        +
      • +
      • + +2.37% (2.37%) Object#collect_stats_from_users [7 calls, 7 total] +
          +
        • + +2.37% (99.99%) Array#each [7 calls, 10 total] +
            +
          • + +1.23% (51.93%) Array#map [16896 calls, 16898 total] +
              + + + + +
            +
          • + + + + + + + + + + + + + + + +
          +
        • +
        +
      • + + + + + + + + + + +
      +
    • +
    +
  • + +
+
+ +
+
+ + diff --git a/ruby_prof_reports/callstack_optimized_each.html b/ruby_prof_reports/callstack_optimized_each.html new file mode 100644 index 00000000..99e5e40d --- /dev/null +++ b/ruby_prof_reports/callstack_optimized_each.html @@ -0,0 +1,1086 @@ + + + + + ruby-prof call tree + + + + + + + +
+
+ Call tree for application task-1.rb
Generated on 2025-01-25 23:16:37 +0300 + with options {}
+
+
+ Threshold: + + + + + +
+ + + + +
+ Thread: 260, Fiber: 240 (100.00% ~ 101.3259957979999) +
    + +
  • + +100.00% (100.00%) [global]# [1 calls, 1 total] +
      +
    • + +100.00% (100.00%) Object#work [1 calls, 1 total] +
        +
      • + +65.33% (65.33%) Object#collect_stats_from_users [7 calls, 7 total] +
          +
        • + +65.33% (100.00%) Array#each [7 calls, 8 total] +
            +
          • + +34.18% (52.32%) Array#map [5500000 calls, 5500000 total] +
              +
            • + +17.06% (49.91%) <Class::Date>#parse [2750940 calls, 2750940 total] +
                +
              • + +3.30% (19.37%) Regexp#match [5501880 calls, 5501880 total] +
              • +
              • + +1.77% (10.40%) String#gsub! [2750940 calls, 2750940 total] +
              • +
              • + +1.35% (7.93%) MatchData#begin [2750940 calls, 2750940 total] +
              • + + + +
              +
            • +
            • + +1.25% (3.65%) String#upcase [2750940 calls, 4826877 total] +
            • +
            • + +1.22% (3.58%) Date#iso8601 [2750940 calls, 2750940 total] +
            • + +
            +
          • +
          • + +4.48% (6.85%) Array#sort [1000000 calls, 1000001 total] +
              + +
            +
          • +
          • + +2.25% (3.44%) Array#join [500000 calls, 500001 total] +
          • +
          • + +2.14% (3.28%) String#+ [8000000 calls, 8000000 total] +
          • +
          • + +1.96% (3.00%) Hash#[] [7000000 calls, 9750940 total] +
          • +
          • + +1.87% (2.87%) Hash#merge [3500000 calls, 3500000 total] +
          • +
          • + +1.81% (2.77%) Hash#[]= [3992465 calls, 7243406 total] +
          • +
          • + +1.60% (2.44%) Array#any? [500000 calls, 500000 total] +
              + + +
            +
          • + + + + + + + + +
          +
        • +
        +
      • +
      • + +31.73% (31.73%) Array#each [1 calls, 8 total] +
          +
        • + +10.36% (32.65%) Object#parse_session [2750940 calls, 2750940 total] +
            +
          • + +4.65% (44.92%) String#split [2750940 calls, 6501881 total] +
          • +
          • + +1.39% (13.45%) Array#[] [13754700 calls, 21756580 total] +
          • +
          +
        • +
        • + +6.56% (20.68%) String#split [3250940 calls, 6501881 total] +
        • +
        • + +1.98% (6.24%) Hash#[] [2750940 calls, 9750940 total] +
            + + +
          +
        • +
        • + +1.60% (5.05%) Object#parse_user [500000 calls, 500000 total] +
            + + +
          +
        • +
        • + +1.60% (5.05%) Set#add [2750940 calls, 2750940 total] +
            + +
          +
        • + + + + + +
        +
      • +
      • + +1.01% (1.01%) JSON::Ext::Generator::GeneratorMethods::Hash#to_json [1 calls, 1 total] + +
      • + + + + + + + + + + +
      +
    • +
    +
  • + +
+
+ +
+
+ + diff --git a/ruby_prof_reports/callstack_sample_large.html b/ruby_prof_reports/callstack_sample_large.html new file mode 100644 index 00000000..b2c516cd --- /dev/null +++ b/ruby_prof_reports/callstack_sample_large.html @@ -0,0 +1,1078 @@ + + + + + ruby-prof call tree + + + + + + + +
+
+ Call tree for application task-1.rb
Generated on 2025-01-25 23:12:14 +0300 + with options {}
+
+
+ Threshold: + + + + + +
+ + + + +
+ Thread: 260, Fiber: 240 (100.00% ~ 1.0946423619998313) +
    + +
  • + +100.00% (100.00%) [global]# [1 calls, 1 total] +
      +
    • + +100.00% (100.00%) Object#work [1 calls, 1 total] +
        +
      • + +62.18% (62.18%) Object#collect_stats_from_users [7 calls, 7 total] +
          +
        • + +62.17% (100.00%) Array#each [7 calls, 8 total] +
            +
          • + +33.77% (54.31%) Array#map [84645 calls, 84645 total] +
              +
            • + +18.06% (53.49%) <Class::Date>#parse [42305 calls, 42305 total] +
                +
              • + +3.44% (19.06%) Regexp#match [84610 calls, 84610 total] +
              • +
              • + +2.24% (12.41%) String#gsub! [42305 calls, 42305 total] +
              • +
              • + +1.47% (8.15%) MatchData#begin [42305 calls, 42305 total] +
              • + + + +
              +
            • +
            • + +1.33% (3.93%) Date#iso8601 [42305 calls, 42305 total] +
            • +
            • + +1.09% (3.24%) String#to_i [84610 calls, 84610 total] +
            • + +
            +
          • +
          • + +2.77% (4.46%) Array#sort [15390 calls, 15391 total] +
              + +
            +
          • +
          • + +2.10% (3.38%) String#+ [123120 calls, 123120 total] +
          • +
          • + +1.96% (3.16%) Array#any? [7695 calls, 7695 total] +
              + + +
            +
          • +
          • + +1.92% (3.09%) Hash#[] [107730 calls, 150035 total] +
          • +
          • + +1.55% (2.49%) Hash#merge [53865 calls, 53865 total] +
          • +
          • + +1.29% (2.07%) Hash#[]= [61560 calls, 111561 total] +
          • +
          • + +1.16% (1.87%) User#attributes [107730 calls, 107730 total] +
          • + + + + + + + + +
          +
        • +
        +
      • +
      • + +34.48% (34.49%) Array#each [1 calls, 8 total] +
          +
        • + +9.96% (28.88%) Object#parse_session [42305 calls, 42305 total] +
            +
          • + +3.40% (34.18%) String#split [42305 calls, 100001 total] +
          • +
          • + +1.93% (19.40%) Array#[] [211525 calls, 334610 total] +
          • +
          +
        • +
        • + +4.92% (14.26%) String#split [50000 calls, 100001 total] +
        • +
        • + +2.76% (8.00%) Hash#[] [42305 calls, 150035 total] +
            +
          • + +1.12% (40.51%) Hash#hash [42305 calls, 50009 total] +
          • +
          +
        • +
        • + +2.24% (6.49%) Set#add [42305 calls, 42305 total] +
            + +
          +
        • +
        • + +1.64% (4.75%) Object#parse_user [7695 calls, 7695 total] +
            + + +
          +
        • + + + + + +
        +
      • + + + + + + + + + + + +
      +
    • +
    +
  • + +
+
+ +
+
+ + diff --git a/task-1.rb b/task-1.rb index 778672df..ff75d961 100644 --- a/task-1.rb +++ b/task-1.rb @@ -35,24 +35,31 @@ def parse_session(session) } end -def collect_stats_from_users(report, users_objects, &block) - users_objects.each do |user| - user_key = "#{user.attributes['first_name']}" + ' ' + "#{user.attributes['last_name']}" - report['usersStats'][user_key] ||= {} - report['usersStats'][user_key] = report['usersStats'][user_key].merge(block.call(user)) - end -end -def work - file_lines = File.read('data.txt').split("\n") +def work(filename = 'data.txt') + report = {} + file_lines = File.read(filename).split("\n") + + users_sessions = {} + current_user = nil + uniqueBrowsers = Set.new + totalSessions = 0 + allBrowsers = Set.new - users = [] - sessions = [] + users_objects = [] file_lines.each do |line| cols = line.split(',') - users = users + [parse_user(line)] if cols[0] == 'user' - sessions = sessions + [parse_session(line)] if cols[0] == 'session' + if cols[0] == 'user' + current_user = parse_user(line) + users_sessions[current_user] = [] + elsif cols[0] == 'session' + session = parse_session(line) + users_sessions[current_user].push session + + totalSessions += 1 + uniqueBrowsers.add(session['browser']) + end end # Отчёт в json @@ -70,78 +77,80 @@ def work # - Всегда использовал только Хром? + # - даты сессий в порядке убывания через запятую + - report = {} - report[:totalUsers] = users.count + report[:totalUsers] = users_sessions.count # Подсчёт количества уникальных браузеров - uniqueBrowsers = [] - sessions.each do |session| - browser = session['browser'] - uniqueBrowsers += [browser] if uniqueBrowsers.all? { |b| b != browser } - end + # uniqueBrowsers = [] + # sessions.each do |session| + # browser = session['browser'] + # uniqueBrowsers += [browser] if uniqueBrowsers.all? { |b| b != browser } + # end report['uniqueBrowsersCount'] = uniqueBrowsers.count - report['totalSessions'] = sessions.count + # report['totalSessions'] = sessions.count + + report['totalSessions'] = totalSessions + report['allBrowsers'] = uniqueBrowsers.map(&:upcase).sort.join(',') - report['allBrowsers'] = - sessions - .map { |s| s['browser'] } - .map { |b| b.upcase } - .sort - .uniq - .join(',') + # report['allBrowsers'] = + # sessions + # .map { |s| s['browser'] } + # .map { |b| b.upcase } + # .sort + # .uniq + # .join(',') # Статистика по пользователям - users_objects = [] - users.each do |user| + users_sessions.each do |user, sessions| attributes = user - user_sessions = sessions.select { |session| session['user_id'] == user['id'] } - user_object = User.new(attributes: attributes, sessions: user_sessions) - users_objects = users_objects + [user_object] + user_object = User.new(attributes: attributes, sessions: sessions) + users_objects.push user_object end + # users.each do |user| + # attributes = user + # user_sessions = sessions.select { |session| session['user_id'] == user['id'] } + # user_object = User.new(attributes: attributes, sessions: user_sessions) + # users_objects = users_objects + [user_object] + # end + report['usersStats'] = {} - # Собираем количество сессий по пользователям - collect_stats_from_users(report, users_objects) do |user| - { 'sessionsCount' => user.sessions.count } + users_objects.each do |user| + user_key = "#{user.attributes['first_name']} #{user.attributes['last_name']}" + times = user.sessions.map {|s| s['time']}.map(&:to_i) + browsers = user.sessions.map {|s| s['browser']}.map(&:upcase) + + report['usersStats'][user_key] = { + 'sessionsCount' => user.sessions.count, + 'totalTime' => "#{times.sum.to_s} min.", + 'longestSession' => "#{times.max.to_s} min.", + 'browsers' => browsers.sort.join(', '), + 'usedIE' => browsers.any? { |b| b =~ /INTERNET EXPLORER/ }, + 'alwaysUsedChrome' => browsers.all? { |b| b =~ /CHROME/ }, + 'dates' => user.sessions.map{ |s| Date.parse(s['date']) }.sort.reverse.map { |d| d.iso8601 } + } end - # Собираем количество времени по пользователям - collect_stats_from_users(report, users_objects) do |user| - { 'totalTime' => user.sessions.map {|s| s['time']}.map {|t| t.to_i}.sum.to_s + ' min.' } - end + File.write('result.json', "#{report.to_json}\n") +end - # Выбираем самую длинную сессию пользователя - collect_stats_from_users(report, users_objects) do |user| - { 'longestSession' => user.sessions.map {|s| s['time']}.map {|t| t.to_i}.max.to_s + ' min.' } - end - # Браузеры пользователя через запятую - collect_stats_from_users(report, users_objects) do |user| - { 'browsers' => user.sessions.map {|s| s['browser']}.map {|b| b.upcase}.sort.join(', ') } - end +require 'ruby-prof' - # Хоть раз использовал IE? - collect_stats_from_users(report, users_objects) do |user| - { 'usedIE' => user.sessions.map{|s| s['browser']}.any? { |b| b.upcase =~ /INTERNET EXPLORER/ } } - end +GC.disable +RubyProf.measure_mode = RubyProf::WALL_TIME - # Всегда использовал только Chrome? - collect_stats_from_users(report, users_objects) do |user| - { 'alwaysUsedChrome' => user.sessions.map{|s| s['browser']}.all? { |b| b.upcase =~ /CHROME/ } } - end +result = RubyProf::Profile.profile do + work('data_large.txt') +end - # Даты сессий через запятую в обратном порядке в формате iso8601 - collect_stats_from_users(report, users_objects) do |user| - { 'dates' => user.sessions.map{|s| s['date']}.map {|d| Date.parse(d)}.sort.reverse.map { |d| d.iso8601 } } - end +printer = RubyProf::CallStackPrinter.new(result) +printer.print(File.open('ruby_prof_reports/callstack.html', 'w+')) - File.write('result.json', "#{report.to_json}\n") -end class TestMe < Minitest::Test def setup From cdf1502aa6a74972b8097ac0a1b6dd2a34522a17 Mon Sep 17 00:00:00 2001 From: anna Date: Sun, 26 Jan 2025 16:03:14 +0300 Subject: [PATCH 02/10] Cached dates --- .ruby-version | 1 + Gemfile | 2 +- Gemfile.lock | 2 +- ruby_prof_reports/callstack.html | 186 ++++++++++++++++--------------- task-1.rb | 35 ++---- 5 files changed, 106 insertions(+), 120 deletions(-) create mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..9c25013d --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +3.3.6 diff --git a/Gemfile b/Gemfile index 2068aa3c..e902d603 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -ruby '3.2.0' +ruby '3.3.6' source "https://rubygems.org" gem 'ruby-prof' diff --git a/Gemfile.lock b/Gemfile.lock index 4bfff8b1..b808cd2b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,7 +11,7 @@ DEPENDENCIES ruby-prof RUBY VERSION - ruby 3.2.0p0 + ruby 3.3.6p108 BUNDLED WITH 2.5.16 diff --git a/ruby_prof_reports/callstack.html b/ruby_prof_reports/callstack.html index 05986ec5..13bbdbcf 100644 --- a/ruby_prof_reports/callstack.html +++ b/ruby_prof_reports/callstack.html @@ -663,7 +663,7 @@
- Call tree for application task-1.rb
Generated on 2025-01-26 00:09:59 +0300 + Call tree for application task-1.rb
Generated on 2025-01-26 15:47:01 +0300 with options {}
@@ -693,276 +693,280 @@
- Thread: 260, Fiber: 240 (100.00% ~ 58.37264639999921) + Thread: 260, Fiber: 240 (100.00% ~ 40.52076910100004)
  • -100.00% (100.00%) [global]# [1 calls, 1 total] +100.00% (100.00%) [global]# [1 calls, 1 total]
    • -100.00% (100.00%) Object#work [1 calls, 1 total] +100.00% (100.00%) Object#work [1 calls, 1 total]
      • -95.90% (95.90%) Array#each [2 calls, 2 total] +95.30% (95.30%) Array#each [2 calls, 2 total]
          -
        • - -39.05% (40.72%) Array#map [3000000 calls, 3000000 total] -
            -
          • +
          • -26.47% (67.80%) <Class::Date>#parse [2750940 calls, 2750940 total] +19.10% (20.04%) Array#map [3000000 calls, 3000000 total]
            • -4.82% (18.21%) Regexp#match [5501880 calls, 5501880 total] +2.51% (13.14%) Date#iso8601 [2750940 calls, 2750940 total]
            • -3.23% (12.20%) String#gsub! [2750940 calls, 2750940 total] +2.36% (12.38%) Hash#[] [5501880 calls, 8252820 total]
            • -2.07% (7.83%) MatchData#begin [2750940 calls, 2750940 total] +1.33% (6.98%) String#upcase [2750940 calls, 2751140 total]
            • -1.25% (4.71%) String#[]= [2750940 calls, 2750940 total] +1.10% (5.76%) String#to_i [2750940 calls, 2750940 total]
            • -
            • +
            • -
            • +
            • +
            +
          • +
        • -12.94% (13.49%) Object#parse_session [2750940 calls, 2750940 total] +16.54% (17.35%) Object#parse_session [2750940 calls, 2750940 total]
          • -3.91% (30.23%) String#split [2750940 calls, 6501881 total] +4.74% (28.67%) String#split [2750940 calls, 6501881 total]
          • -2.62% (20.22%) Array#[] [13754700 calls, 21756580 total] +3.63% (21.97%) Array#[] [13754700 calls, 21756580 total]
        • -
        • +
        • -4.90% (5.11%) String#split [3250940 calls, 6501881 total] +6.06% (6.36%) String#split [3250940 calls, 6501881 total]
        • -3.80% (3.97%) Hash#[] [2750940 calls, 2750940 total] +5.34% (5.60%) Hash#[] [2750940 calls, 8252820 total]
          • -1.52% (40.05%) Hash#hash [2750940 calls, 3250949 total] +2.07% (38.76%) Hash#hash [2750940 calls, 3250949 total]
        • -3.25% (3.38%) Array#sort [1000000 calls, 1000001 total] +4.70% (4.93%) Array#sort [1000000 calls, 1000001 total]
          • -1.31% (40.33%) Date#<=> [4784430 calls, 4784430 total] +1.89% (40.18%) Date#<=> [4784430 calls, 4784430 total]
        • -3.01% (3.14%) Set#add [2750940 calls, 2750940 total] +4.65% (4.88%) Set#add [2750940 calls, 2750940 total]
          • -1.05% (34.85%) Hash#[]= [2750940 calls, 3750941 total] +1.74% (37.37%) Hash#[]= [2750940 calls, 3751941 total]
        • -2.08% (2.16%) Object#parse_user [500000 calls, 500000 total] +2.68% (2.81%) Object#parse_user [500000 calls, 500000 total]
        • -1.77% (1.85%) Array#any? [500000 calls, 500000 total] +2.16% (2.27%) String#== [6001880 calls, 6001880 total] +
        • +
        • + +2.13% (2.24%) Array#any? [500000 calls, 500000 total]
        • -1.75% (1.82%) Hash#[]= [1000000 calls, 3750941 total] +2.03% (2.13%) Hash#[]= [1000000 calls, 3751941 total]
            -
        • -1.25% (1.30%) String#== [6001880 calls, 6001880 total] +1.73% (1.82%) Array#[] [6001880 calls, 21756580 total]
        • -1.23% (1.28%) Array#[] [6001880 calls, 21756580 total] +1.13% (1.18%) Array#push [2750940 calls, 3250940 total]
        • -
        • -0.88% (0.92%) Array#all? [500000 calls, 500000 total] +1.07% (1.13%) Array#all? [500000 calls, 500000 total]
        • -
      • -1.47% (1.47%) Hash#each [1 calls, 2 total] +1.98% (1.98%) Hash#each [1 calls, 2 total]
      • -1.21% (1.21%) JSON::Ext::Generator::GeneratorMethods::Hash#to_json [1 calls, 1 total] +1.02% (1.02%) JSON::Ext::Generator::GeneratorMethods::Hash#to_json [1 calls, 1 total]