Class: Rack::SmartCompress::CpuAdvisor

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/smart_compress/cpu_advisor.rb

Constant Summary collapse

SAMPLE_INTERVAL =

Sample at most once per second

1.0

Class Method Summary collapse

Class Method Details

.adjusted_level(encoder_name, default_level) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/smart_compress/cpu_advisor.rb', line 29

def adjusted_level(encoder_name, default_level)
  return default_level unless high_load?

  case encoder_name
  when "zstd"
    1 # Fastest Zstd level
  when "br"
    1 # Fastest Brotli quality level
  when "gzip", "deflate"
    Zlib::BEST_SPEED # Fastest Gzip level (1)
  else
    default_level
  end
end

.high_load?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
# File 'lib/rack/smart_compress/cpu_advisor.rb', line 12

def high_load?
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  if @last_check.nil? || (now - @last_check) >= SAMPLE_INTERVAL
    @last_check = now
    @cached_high_load = compute_high_load
  end
  @cached_high_load
end

.ncpu_countObject



21
22
23
24
25
26
27
# File 'lib/rack/smart_compress/cpu_advisor.rb', line 21

def ncpu_count
  @ncpu_count ||= begin
    Etc.nprocessors
  rescue StandardError
    2
  end
end

.reset!Object



44
45
46
47
48
# File 'lib/rack/smart_compress/cpu_advisor.rb', line 44

def reset!
  @last_check = nil
  @cached_high_load = nil
  @ncpu_count = nil
end