Class: SpeedtestNet::MeasureResult

Inherits:
Object
  • Object
show all
Defined in:
lib/speedtest_net/measure_result.rb

Instance Method Summary collapse

Constructor Details

#initialize(results = []) ⇒ MeasureResult

Returns a new instance of MeasureResult.



5
6
7
# File 'lib/speedtest_net/measure_result.rb', line 5

def initialize(results = [])
  @results = results.select { |result| result.instance_of?(Integer) || result.instance_of?(Float) }
end

Instance Method Details

#calculateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/speedtest_net/measure_result.rb', line 9

def calculate
  return 0.0 if @results.empty?

  # Fastest 10% and slowest 30% of results are then discarded
  # See: https://support.ookla.com/hc/en-us/articles/234575828-What-is-the-test-flow-and-methodology-for-the-Speedtest-
  sorted_results = @results.sort
  count = sorted_results.size

  faster = count - (count * 0.1).round # fastest 10%
  slower = (count * 0.3).round # slowest 30%
  target_results = sorted_results[slower...faster]

  # The remaining result are averaged together to determine
  # the final result
  target_results.sum.to_f / target_results.count
end