Skip to content

usage: avoid creating unnecessary instance periods#126

Merged
bors[bot] merged 1 commit intomasterfrom
optimize-calculate-usage
Sep 10, 2019
Merged

usage: avoid creating unnecessary instance periods#126
bors[bot] merged 1 commit intomasterfrom
optimize-calculate-usage

Conversation

@davidor
Copy link
Copy Markdown
Contributor

@davidor davidor commented Sep 10, 2019

This PR introduces a performance optimization in the part of the code that calculates the current usage.

This is important for users with lots of metrics and limits. Without this, the code can generate many instance periods and it ends up consuming a significant part of the total CPU time.

@davidor davidor force-pushed the optimize-calculate-usage branch from b014c10 to 01f98fb Compare September 10, 2019 13:00
This is important. Without this, the code can generate many instance
periods and it ends up consuming a significant part of the total CPU
time.
@davidor davidor force-pushed the optimize-calculate-usage branch from 01f98fb to b3fe82b Compare September 10, 2019 13:05
@davidor
Copy link
Copy Markdown
Contributor Author

davidor commented Sep 10, 2019

bors r=@unleashed

bors Bot added a commit that referenced this pull request Sep 10, 2019
126: usage: avoid creating unnecessary instance periods r=unleashed a=davidor

This PR introduces a performance optimization in the part of the code that calculates the current usage.

This is important for users with lots of metrics and limits. Without this, the code can generate many instance periods and it ends up consuming a significant part of the total CPU time.

Co-authored-by: David Ortiz <z.david.ortiz@gmail.com>
@bors
Copy link
Copy Markdown
Contributor

bors Bot commented Sep 10, 2019

Build succeeded

@bors bors Bot merged commit b3fe82b into master Sep 10, 2019
@bors bors Bot deleted the optimize-calculate-usage branch September 10, 2019 13:12
@davidor
Copy link
Copy Markdown
Contributor Author

davidor commented Sep 10, 2019

For the record, I used stackprof to measure the performance impact of this change. I called Transactor.authorize for a service with 400 limits.

The method optimized in this PR is ThreeScale::Backend::Usage.usage. Notice how before, it was running like 35.8% of the time. With the change, that was reduced to 16%. Here are the results:

Notice that the % here will vary a lot depending on the number of metrics, limits, etc. of the service under test.

Before

==================================                                                                                                                                                            
  Mode: cpu(1000)                                                                                                                                                                             
  Samples: 78160 (0.00% miss rate)                                                                                                                                                            
  GC: 3538 (4.53%)                                                                                                                                                                            
==================================                                                                                                                                                            
     TOTAL    (pct)     SAMPLES    (pct)     FRAME                                                                                                                                            
     26920  (34.4%)       26920  (34.4%)     ThreeScale::Backend::Period::Boundary::Methods#minute_start                                                                                      
      8691  (11.1%)        8691  (11.1%)     ThreeScale::Backend::Transactor::Status::UsageReport#add_period                                                                                  
      5854   (7.5%)        5854   (7.5%)     ThreeScale::Backend::Period::Cache.get                                                                                                           
      3538   (4.5%)        3538   (4.5%)     (garbage collection)                                                                                                                             
      2408   (3.1%)        2408   (3.1%)     ThreeScale::Backend::TimeHacks#to_compact_s                                                                                                      
      2143   (2.7%)        2143   (2.7%)     ThreeScale::Backend::Transactor::Status#value_for_usage_limit                                                                                    
     26451  (33.8%)        2015   (2.6%)     ThreeScale::Backend::Transactor::Status::UsageReport#to_xml                                                                                      
      1724   (2.2%)        1724   (2.2%)     ThreeScale::Backend::Period::HelperMethods#get_granularity_class                                                                                 
      1561   (2.0%)        1561   (2.0%)     ThreeScale::Backend::StorageKeyHelpers#encode_key                                                                                                
      5213   (6.7%)        1553   (2.0%)     ThreeScale::Backend::Period::Minute#<=>                                                                                                          
      1482   (1.9%)        1482   (1.9%)     ThreeScale::Backend::Period::HelperMethods#get_granularity_class                                                                                 
     27988  (35.8%)        1373   (1.8%)     ThreeScale::Backend::Usage.usage                                                                                                                 
      2279   (2.9%)        1309   (1.7%)     ThreeScale::Backend::Application#metric_name                                                                                                     
      1299   (1.7%)        1299   (1.7%)     #<Module:0x00000002c7bd00>.service_key_prefix                                                                                                    
      3439   (4.4%)        1296   (1.7%)     ThreeScale::Backend::Transactor::Status::UsageReport#current_value                                                                               
      1262   (1.6%)        1262   (1.6%)     ThreeScale::Backend::Transactor::Status#authorized?                                                                                              
      1098   (1.4%)        1098   (1.4%)     #<Module:0x00000002c7bd00>.application_key_prefix                                                                                                
      1089   (1.4%)        1089   (1.4%)     ThreeScale::Backend::Period::Minute#to_s                                                                                                         
      3443   (4.4%)        1035   (1.3%)     #<Module:0x00000002c7bd00>.counter_key                                                                                                           
       970   (1.2%)         970   (1.2%)     ThreeScale::Backend::Application#metric_names                                                                                                    
       782   (1.0%)         782   (1.0%)     #<Module:0x00000002c7bd00>.metric_key_prefix                                                                                                     
       695   (0.9%)         695   (0.9%)     ThreeScale::Backend::UsageLimit#validate                                                                                                         
       641   (0.8%)         641   (0.8%)     ThreeScale::Backend::Period::HelperMethods#get_granularity_class
     16281  (20.8%)         636   (0.8%)     ThreeScale::Backend::Transactor::Status::UsageReport#initialize
      1990   (2.5%)         621   (0.8%)     ThreeScale::Backend::Transactor::Status::UsageReport#metric_name
       588   (0.8%)         588   (0.8%)     ThreeScale::Backend::Transactor::Status::UsageReport#max_value
      2855   (3.7%)         536   (0.7%)     ThreeScale::Backend::Transactor::Status::UsageReport#add_values
     26886  (34.4%)         435   (0.6%)     ThreeScale::Backend::Transactor::Status#add_reports_section
       397   (0.5%)         397   (0.5%)     Redis::Connection::Hiredis#write
     17927  (22.9%)         377   (0.5%)     ThreeScale::Backend::Transactor::Status#load_usage_reports

After

==================================
  Mode: cpu(1000)
  Samples: 70102 (0.00% miss rate)
  GC: 3390 (4.84%)
==================================
     TOTAL    (pct)     SAMPLES    (pct)     FRAME
     15027  (21.4%)       15027  (21.4%)     ThreeScale::Backend::Period::Boundary::Methods#minute_start
      9826  (14.0%)        9826  (14.0%)     ThreeScale::Backend::Transactor::Status::UsageReport#add_period
      3390   (4.8%)        3390   (4.8%)     (garbage collection)
      3318   (4.7%)        3318   (4.7%)     ThreeScale::Backend::Period::Cache.get
     11224  (16.0%)        3009   (4.3%)     ThreeScale::Backend::Usage.usage
      2543   (3.6%)        2543   (3.6%)     ThreeScale::Backend::Transactor::Status#value_for_usage_limit
     31639  (45.1%)        2387   (3.4%)     ThreeScale::Backend::Transactor::Status::UsageReport#to_xml
      2156   (3.1%)        2156   (3.1%)     ThreeScale::Backend::TimeHacks#to_compact_s
      2009   (2.9%)        2009   (2.9%)     ThreeScale::Backend::Period::HelperMethods#get_granularity_class
      6212   (8.9%)        1902   (2.7%)     ThreeScale::Backend::Period::Minute#<=>
      1803   (2.6%)        1803   (2.6%)     ThreeScale::Backend::Period::HelperMethods#get_granularity_class
      3031   (4.3%)        1648   (2.4%)     ThreeScale::Backend::Application#metric_name
      1611   (2.3%)        1611   (2.3%)     ThreeScale::Backend::Transactor::Status#authorized?
      4098   (5.8%)        1555   (2.2%)     ThreeScale::Backend::Transactor::Status::UsageReport#current_value
      1383   (2.0%)        1383   (2.0%)     ThreeScale::Backend::Application#metric_names
      1364   (1.9%)        1364   (1.9%)     ThreeScale::Backend::Period::Minute#to_s
      1305   (1.9%)        1305   (1.9%)     ThreeScale::Backend::StorageKeyHelpers#encode_key
      1164   (1.7%)        1164   (1.7%)     #<Module:0x00000003360f80>.service_key_prefix
      3061   (4.4%)         905   (1.3%)     #<Module:0x00000003360f80>.counter_key
       868   (1.2%)         868   (1.2%)     ThreeScale::Backend::Period::HelperMethods#get_granularity_class
       818   (1.2%)         818   (1.2%)     ThreeScale::Backend::Transactor::Status::UsageReport#max_value
       814   (1.2%)         814   (1.2%)     ThreeScale::Backend::UsageLimit#validate
     19119  (27.3%)         752   (1.1%)     ThreeScale::Backend::Transactor::Status::UsageReport#initialize
      2569   (3.7%)         750   (1.1%)     ThreeScale::Backend::Transactor::Status::UsageReport#metric_name
       722   (1.0%)         722   (1.0%)     #<Module:0x00000003360f80>.metric_key_prefix
       718   (1.0%)         718   (1.0%)     #<Module:0x00000003360f80>.application_key_prefix
      3590   (5.1%)         595   (0.8%)     ThreeScale::Backend::Transactor::Status::UsageReport#add_values
       509   (0.7%)         509   (0.7%)     ThreeScale::Backend::Transactor::Status::UsageReport#metric_id
     32133  (45.8%)         494   (0.7%)     ThreeScale::Backend::Transactor::Status#add_reports_section
       416   (0.6%)         416   (0.6%)     Redis::Connection::Hiredis#write

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants