Skip to content

Instantly share code, notes, and snippets.

@vorce
Created July 5, 2022 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vorce/b64f149c440843790f0c4b97dc4d4343 to your computer and use it in GitHub Desktop.
Save vorce/b64f149c440843790f0c4b97dc4d4343 to your computer and use it in GitHub Desktop.
JUnitFormatter that outputs memory usage to a file
defmodule TestMem do
@moduledoc """
JUnitFormatter that outputs memory usage to a file.
Useful to measure your test suite's memory consumption.
"""
use GenServer
@impl true
def init(_opts) do
file = File.open!("memory_usage.csv", [:write, {:delayed_write, 1000, 200}])
{:ok, %{file: file}}
end
@impl true
def handle_cast({:test_finished, %ExUnit.Test{state: nil}}, config) do
IO.binwrite(config.file, "#{DateTime.utc_now()},#{:erlang.memory(:total)}\n")
{:noreply, config}
end
def handle_cast({:test_finished, %ExUnit.Test{state: {:failed, _failed}}}, config) do
IO.binwrite(config.file, "#{DateTime.utc_now()},#{:erlang.memory(:total)}\n")
{:noreply, config}
end
def handle_cast(_event, config), do: {:noreply, config}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment