Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion spec/commands/new_command_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,14 @@ describe AmberCLI::Commands::NewCommand do
production["session"]["expires"].as_i.should eq(1_209_600)
production["static"]["headers"]["Cache-Control"].as_s.should eq("no-cache")

AmberCLI::Commands::NewCommand::GC_INITIAL_HEAP_MIB.should be > 0
# 8 MiB keeps the generated app under 20 MiB peak memory; see the constant.
AmberCLI::Commands::NewCommand::GC_INITIAL_HEAP_MIB.should eq 8
%w[development test production].each do |environment_name|
environment = YAML.parse(File.read(File.join(destination, "config/environments/#{environment_name}.yml")))
environment["server"]["gc_initial_heap_mib"].as_i.should eq(AmberCLI::Commands::NewCommand::GC_INITIAL_HEAP_MIB)
end
File.read(File.join(destination, "README.md")).should contain("server.gc_initial_heap_mib")
File.read(File.join(destination, "README.md")).should contain("trades memory for less collector work")

routes = File.read(File.join(destination, "config/routes.cr"))
lean_block = routes[routes.index!("pipeline :lean_read do")...routes.index!("pipeline :web do")]
Expand Down
12 changes: 8 additions & 4 deletions src/amber_cli/commands/new_command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ module AmberCLI::Commands
VALID_TEMPLATES = %w[ecr]

# Starting garbage-collector heap, in MiB, written to `server.gc_initial_heap_mib`
# in every generated environment file. 16 MiB is the smallest starting heap that
# kept POST p50 at or below beta.10 in a generated app (4 and 8 MiB did not); it
# adds about 8 MiB of peak memory on a small JSON route.
GC_INITIAL_HEAP_MIB = 16
# in every generated environment file. 8 MiB keeps a generated app under 20 MiB
# peak memory on its page-cached, JSON and form-post routes at 32 connections
# (15.2 to 16.8 MiB, against 23.5 to 25.6 MiB at 16 MiB). A 16 MiB heap collects
# less often, so allocation-heavy routes cost about 18% more CPU per request at
# 8 MiB; an app that prefers that trade raises the value per environment.
GC_INITIAL_HEAP_MIB = 8

getter database : String = "sqlite"
getter template : String = "ecr"
Expand Down Expand Up @@ -522,6 +524,8 @@ give a page a session, move its route to `routes :web`.
The garbage collector starts with a #{GC_INITIAL_HEAP_MIB} MiB heap (`server.gc_initial_heap_mib`
in `config/environments/*.yml`, or `AMBER_SERVER_GC_INITIAL_HEAP_MIB`), so a busy server
does not collect over and over while its heap grows. 0 keeps the collector's own size.
A larger value trades memory for less collector work: at 16 MiB the app peaks near
24 MiB under load instead of 16 MiB, and JSON and form posts use less CPU per request.

Production writes no per-request log line. The edge proxy keeps access logs. The
`Amber::Pipe::Logger` lines in `config/routes.cr` show where to turn it back on.
Expand Down
Loading