Logging Statistics Using ETS

Learn how to create a statistics server for tracking progress using GenServer and ETS.

Tracking statistics using GenServer and ETS

During an evolution, we may want to track statistics about fitness, age, or variation in your population over the course of the evolution. For example, perhaps we want to determine the distribution of a particular gene at different generations during evolution. In this lesson, we’ll create a statistics server using a GenServer and an ETS table.

A GenServer is an abstraction around a state that models client-server behavior. It allows us to spin up a long-running process and alter its state through message passing. “ETS” stands for “Erlang Term Storage” and offers a built-in storage API through Erlang interpolation.

The GenServer will allow us to supervise the ETS table which in turn, will allow us to quickly and easily insert and lookup statistics across generations. In addition, it’ll be easy to expand this approach to all kinds of statistics and metrics.

Creating the statistics server

Start by creating a new utilities directory inside lib, and inside that new directory, add a new file named statistics.ex. This file will contain the implementation for the statistics server. Over here, start by defining a bare-bones GenServer implementation:

Get hands-on with 1200+ tech skills courses.