Skip to content

Instantly share code, notes, and snippets.

@skull-squadron
Created May 22, 2023 06:06
Show Gist options
  • Save skull-squadron/bb0f063799639c15c3c559df4ca99fbc to your computer and use it in GitHub Desktop.
Save skull-squadron/bb0f063799639c15c3c559df4ca99fbc to your computer and use it in GitHub Desktop.
1 M fibers running in Crystal
NUM_FIBERS = 1_000_000 # no. of Fibers
UPDATE_INTERVAL = 10_000 # show starting progress every these many fibers
PRINTERS = 100 # at most these no. of fibers will print dots
PRINT_INTERVAL = 10 # every increments of x
PRINT_NTH_FIBER = NUM_FIBERS//PRINTERS
MINI_NAP = Time::Span.new(nanoseconds: 1_000_000)
GC_STATS_PERIOD = Time::Span.new(seconds: 10)
start = Time.utc
GC.disable unless ENV.fetch("NO_GC_DISABLE", nil)
DOT = '.'
NUM_FIBERS.times do |n|
STDERR.puts "spawned #{n} fibers" if !n.zero? && (n % UPDATE_INTERVAL).zero?
spawn do
x = 1
if (n % PRINT_NTH_FIBER).zero?
loop do
STDERR.print DOT if (x % PRINT_INTERVAL).zero?
x += 1
sleep(MINI_NAP)
end
else
loop do
x += 1
sleep(MINI_NAP)
end
end
end
end
ready = Time.utc
elapsed = ready - start
rate = NUM_FIBERS / elapsed.to_f
STDERR.puts "spawned %d fibers in %s seconds (%10.4f fibers/s)" % {NUM_FIBERS, elapsed, rate}
GC.enable unless ENV.fetch("NO_GC_DISABLE", nil)
loop do
if ENV.fetch("GC_COLLECT", nil)
STDERR.print "collecting..."
GC.collect
STDERR.puts "done"
end
STDERR.puts "gc stats"
STDERR.puts
STDERR.puts GC.stats.pretty_inspect
STDERR.puts
STDERR.puts
STDERR.puts "gc prof_stats"
STDERR.puts
STDERR.puts GC.prof_stats.pretty_inspect
STDERR.puts
STDERR.puts
sleep(GC_STATS_PERIOD)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment