-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2022, by Samuel Williams. | ||
|
||
module Async | ||
class Idler | ||
def initialize(parent: Task.current) | ||
@parent = parent | ||
@list = List.new | ||
end | ||
|
||
def async(*arguments, parent: (@parent or Task.current), **options) | ||
wait | ||
|
||
parent.async(*arguments, **options) | ||
end | ||
|
||
def wait | ||
|
||
end | ||
end | ||
end |
This file contains 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 contains 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,33 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require_relative 'lib/async' | ||
|
||
def measure_load(sample_duration = 1.0) | ||
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
sleep(sample_duration) | ||
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time | ||
|
||
return (duration / sample_duration) - 1.0 | ||
end | ||
|
||
def add_load(n = 100) | ||
n.times do | ||
Async do | ||
while true | ||
sleep 0.01 | ||
end | ||
end | ||
end | ||
end | ||
|
||
Async do |task| | ||
while true | ||
load = measure_load | ||
puts "Load: #{sprintf("%0.3f%%", load * 100.0)} children: #{task.children&.size}" | ||
|
||
if load <= 0.01 | ||
puts "Adding load..." | ||
add_load | ||
end | ||
end | ||
end |