SlideShare a Scribd company logo
Performance Optimization 101
Louis-Philippe Gauthier
Team leader @ AdGear Trader
Exercise
HTTP API server
API

GET /date - returns today’s date
GET /time - returns the unix time in seconds
HTTP API server
TODO

•

accepting connections

•

parsing http requests

•

routing

•

building responses
HTTP API server
accepting connections
HTTP API server
accepting connections
HTTP API server
accepting connections

•

gen_tcp:controlling_process/2 is slow

•

spawn worker with ListenSocket

•

worker accepts and ack’s listener
HTTP API server
accepting connections
HTTP API server
accepting connections
HTTP API server
accepting connections
HTTP API server
accepting connections

•

use proc_lib instead of gen_server

•

socket options:
•

binary

•

{backlog, 4196}

•

{raw, 6, 9, <<30:32/native>>}
HTTP API server
parsing request
HTTP API server
parsing request
HTTP API server
parsing request

•

binary matching is very powerful!

•

working with binaries is more memory efficient
•

binaries over 64 bytes are shared (not copied)

•

faster than the built-in http parser (BIF) when
running on many cores and using hipe

•

keep state in a record
•

O(1) lookups
HTTP API server
routing
HTTP API server
routing

pattern matching is awesome!!
HTTP API server
building response
HTTP API server
building response
HTTP API server
building response
HTTP API server
building response

•

ETS is your friend!

•

cache time date in ETS public table
•

{read_concurrency, true}

•

if you store a binary over 64 bytes, it won’t get
copied!

•

have a gen_server update the cache
•

every second for the time

•

every day for the date
HTTP API server
building response

•

do not try to rewrite everything

•

use community projects and contribute back!

•

often your application will spend most of its time
talking to external services

•

premature optimization is usually bad
Gotchas
slow functions / modules

•

erlang:now/0 vs os:timestamp/0

•

proplists:get_value() vs lists:keyfind()

•

timer:send_after() vs erlang:send_after()

•

gen_udp:send() vs erlang:port_command()

•

avoid erlang:controlling_process() if you can

•

avoid base64, string, unicode modules
Tools
Profiling
info

•

useful to find slow code paths

•

fprof
•
•

output is really hard to understand

•
•

uses erlang:trace/3

erlgrind to read in kcachegrind

eflame
•

also uses erlang:trace/3

•

nice graphical output
Eflame
info

•

flamechart.pl (from Joyent)

•

makes it visually easy to find slow function calls
Eflame
how to
Eflame
info
Micro benchmarks
info

•

start with profiling

•

useful for experimentation and to validate
hypothesis

•

small benchmarking library called timing
•

uses the excellent bear (statistics) library
Micro benchmarks
how to
Micro benchmarks
info

# parallel processes

erlang:now/0

os:timestamp/0

1

0.99

0.87

10

22.87

2.54

100

168.23

16.99

1000

664.46

51.98
Hipe
info

•

native, {hipe, [o3]}

•

doesn’t mix with NIFs
•

•

on_load

switching between non-native and native code is
expensive
•

different call stacks

•

might overload the code_server (bug?)

•

—enable-native-libs

•

hipe_bifs (sshhh)
Hipe
how to
NIFs
info

•

function that is implemented in C instead of Erlang

•

can be dangerous…
•
•

OOM (memory leak)

•
•

crash VM (segfault)
must return < 500 us (to be safe…)

ideally should yield and use enif_consume_timeslice
•

•

what is a reduction?

dirty schedulers (R17)
•

finally!
Process Tuning
info

•

tune min_heap_size on spawn

•

fullsweep_after if you have memory issues
•

•

force gc

+hms (set default min_heap_size)
Process Tuning
info
Monitoring
info

•

statsderl for application metrics

•

vmstats for VM metrics

•

system_stats for OS metrics

•

erlang:system_monitor/2

•

entop for live system exploration
Statsderl
info

•

statsd client

•

very cheap to call (async)

•

offers 3 kinds of metrics:
•

counters - for counting (e.g QPS)

•

gauges - for absolute values (e.g. system memory)

•

timers - similar to gauges but with extra statistics
Statsderl
how to
VM Stats
info

•

process count

•

messages in queues

•

run queue length

•

memory (total, proc_used, atom_used, binary, ETS)

•

scheduler utilization (per scheduler)

•

garbage collection (count, words reclaimed)

•

reductions

•

IO bytes (in/out)
VM Stats
info
System Stats
info

•

load1, load5, load15

•

cpu percent
•

can be misleading because of spinning
schedulers

•

virtual memory size

•

resident memory size
•

very useful to track those OOM crashes
!
System Stats
info
System Monitor
info

•

monitoring for:
•
•

busy_dist_port

•

long_gc

•

long_schedule

•
•

busy_port

large_heap

riak_sysmon + lager / statsderl handler
System Monitor
how to
Dashboard
info
Entop
info

•

top(1)-like tool for the Erlang VM

•

can be used remotely

•

gives per process:
•
•

reductions

•

message queue length

•

!

pid / name

heap size
Entop
info
VM Tuning
info

•

+K true (kernel polling)

•

+sct db (scheduler bind)

•

+scl false (disable load distribution)

•

+sfwi 500 (force sheduler wakeup NIFs)

•

+spp true (port parallelism)

•

+zdbbl (distribution buffer busy limit)

•

test with production load (synthetic benchmarks
can be misleading)
Cset
info

•

tool to help create cpusets

•

reduces non voluntary
context-switches

•

reserve first two CPUs for
interrupts and background
jobs

•

reserve rest of CPUs for the
Erlang VM

•

linux only
Cpuset
how to
Lock counter
info
Other tools
info

•

system limits
•
•

•

ulimit -n
sysctl

dtrace / systemtap
•

application + OS tracing
Links
info
•

https://github.com/proger/eflame

•

https://github.com/lpgauth/timing

•

https://github.com/lpgauth/statsderl

•

https://github.com/ferd/vmstats

•

https://github.com/lpgauth/system-stats

•

https://github.com/mazenharake/entop

•

https://github.com/ratelle/cpuset
Thank you!
github: lpgauth!
irc: lpgauth (@erlounge)!
twitter: lpgauth

More Related Content

PDF
0.5mln packets per second with Erlang
PDF
Whoops! I Rewrote It in Rust
PDF
Using eBPF to Measure the k8s Cluster Health
PDF
High-Performance Networking Using eBPF, XDP, and io_uring
PDF
OSNoise Tracer: Who Is Stealing My CPU Time?
PDF
Understanding Apache Kafka P99 Latency at Scale
PDF
Extreme HTTP Performance Tuning: 1.2M API req/s on a 4 vCPU EC2 Instance
PDF
Optimizing Erlang Code for Speed
0.5mln packets per second with Erlang
Whoops! I Rewrote It in Rust
Using eBPF to Measure the k8s Cluster Health
High-Performance Networking Using eBPF, XDP, and io_uring
OSNoise Tracer: Who Is Stealing My CPU Time?
Understanding Apache Kafka P99 Latency at Scale
Extreme HTTP Performance Tuning: 1.2M API req/s on a 4 vCPU EC2 Instance
Optimizing Erlang Code for Speed

What's hot (20)

PPTX
Troubleshooting common oslo.messaging and RabbitMQ issues
PDF
Continuous Go Profiling & Observability
PDF
Unikraft: Fast, Specialized Unikernels the Easy Way
PDF
Doing QoS Before Ceph Cluster QoS is available - David Byte, Alex Lau
PDF
Automatic Operation Bot for Ceph - You Ji
PDF
Data Structures for High Resolution, Real-time Telemetry at Scale
PDF
Keeping Latency Low and Throughput High with Application-level Priority Manag...
PDF
Global deduplication for Ceph - Myoungwon Oh
PDF
Rust Is Safe. But Is It Fast?
PDF
Crimson: Ceph for the Age of NVMe and Persistent Memory
PPTX
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
PDF
Performance
PDF
How to tune Kafka® for production
PDF
Tips on High Performance Server Programming
PDF
Kafka Summit SF 2017 - One Day, One Data Hub, 100 Billion Messages: Kafka at ...
PPTX
Peter Zaitsev "18 ways to fix MySQL bottlenecks"
PDF
[POSS 2019] OVirt and Ceph: Perfect Combination.?
PDF
Object Compaction in Cloud for High Yield
PDF
Porting and Optimization of Numerical Libraries for ARM SVE
PPT
Epoll - from the kernel side
Troubleshooting common oslo.messaging and RabbitMQ issues
Continuous Go Profiling & Observability
Unikraft: Fast, Specialized Unikernels the Easy Way
Doing QoS Before Ceph Cluster QoS is available - David Byte, Alex Lau
Automatic Operation Bot for Ceph - You Ji
Data Structures for High Resolution, Real-time Telemetry at Scale
Keeping Latency Low and Throughput High with Application-level Priority Manag...
Global deduplication for Ceph - Myoungwon Oh
Rust Is Safe. But Is It Fast?
Crimson: Ceph for the Age of NVMe and Persistent Memory
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
Performance
How to tune Kafka® for production
Tips on High Performance Server Programming
Kafka Summit SF 2017 - One Day, One Data Hub, 100 Billion Messages: Kafka at ...
Peter Zaitsev "18 ways to fix MySQL bottlenecks"
[POSS 2019] OVirt and Ceph: Perfect Combination.?
Object Compaction in Cloud for High Yield
Porting and Optimization of Numerical Libraries for ARM SVE
Epoll - from the kernel side
Ad

Viewers also liked (20)

PDF
High Performance Erlang - Pitfalls and Solutions
PDF
Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013
PDF
Debugging Complex Systems - Erlang Factory SF 2015
PDF
Scaling LoL Chat to 70M Players
PPT
Comparing Cpp And Erlang For Motorola Telecoms Software
PDF
Craig Kerstiens - Scalable Uniques in Postgres @ Postgres Open
PDF
Ari xivo astricon_2016
PDF
xPad - Building Simple Tablet OS with Gtk/WebKit
PDF
WEIGHT MANAGEMENT Do it yourself Motivation and Tips
PDF
webSocket通信を知らないiOSエンジニアが知っておいて損はしない(経験談的な)軽い話
PDF
Useful PostgreSQL Extensions
 
PPTX
Architectures for High Availability - QConSF
PDF
Erlang containers
PDF
Fabric, Cuisine and Watchdog for server administration in Python
PPTX
KazooCon 2014 - Kazoo Scalability
PDF
Introduction to Kafka Streams
PDF
Staying Afloat with Buoy: A High-Performance HTTP Client
PPT
Astricon 2010: Scaling Asterisk installations
PDF
User-tailored Inter-Widget Communication Extending the Shared Data Interface ...
PDF
Group Concpet Mapping Learning Analytics @ LASI Amsterdam
High Performance Erlang - Pitfalls and Solutions
Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013
Debugging Complex Systems - Erlang Factory SF 2015
Scaling LoL Chat to 70M Players
Comparing Cpp And Erlang For Motorola Telecoms Software
Craig Kerstiens - Scalable Uniques in Postgres @ Postgres Open
Ari xivo astricon_2016
xPad - Building Simple Tablet OS with Gtk/WebKit
WEIGHT MANAGEMENT Do it yourself Motivation and Tips
webSocket通信を知らないiOSエンジニアが知っておいて損はしない(経験談的な)軽い話
Useful PostgreSQL Extensions
 
Architectures for High Availability - QConSF
Erlang containers
Fabric, Cuisine and Watchdog for server administration in Python
KazooCon 2014 - Kazoo Scalability
Introduction to Kafka Streams
Staying Afloat with Buoy: A High-Performance HTTP Client
Astricon 2010: Scaling Asterisk installations
User-tailored Inter-Widget Communication Extending the Shared Data Interface ...
Group Concpet Mapping Learning Analytics @ LASI Amsterdam
Ad

Similar to Performance optimization 101 - Erlang Factory SF 2014 (20)

PDF
Infrastructure, Hiphop for PHP, deploy @ Hyves
PPTX
HBase Low Latency
PDF
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
PPTX
Does the SPL still have any relevance in the Brave New World of PHP7?
PPTX
Does the SPL still have any relevance in the Brave New World of PHP7?
PDF
Criteo meetup - S.R.E Tech Talk
PDF
Large-scale Web Apps @ Pinterest
PPTX
HBase Low Latency, StrataNYC 2014
PPTX
HBase: Where Online Meets Low Latency
PDF
Ingesting hdfs intosolrusingsparktrimmed
PDF
Apache HBase Low Latency
PDF
Portable PHP
PDF
Scaling ingest pipelines with high performance computing principles - Rajiv K...
PDF
Scaling tappsi
PPTX
Northeast PHP - High Performance PHP
PPTX
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
PDF
Induction training-cache
PPTX
Php internal architecture
PDF
Ruby and Distributed Storage Systems
PDF
Top ten-list
Infrastructure, Hiphop for PHP, deploy @ Hyves
HBase Low Latency
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
Criteo meetup - S.R.E Tech Talk
Large-scale Web Apps @ Pinterest
HBase Low Latency, StrataNYC 2014
HBase: Where Online Meets Low Latency
Ingesting hdfs intosolrusingsparktrimmed
Apache HBase Low Latency
Portable PHP
Scaling ingest pipelines with high performance computing principles - Rajiv K...
Scaling tappsi
Northeast PHP - High Performance PHP
ExpressionEngine - Simple Steps to Performance and Security (EECI 2014)
Induction training-cache
Php internal architecture
Ruby and Distributed Storage Systems
Top ten-list

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PPTX
Cloud computing and distributed systems.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Approach and Philosophy of On baking technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
Cloud computing and distributed systems.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Network Security Unit 5.pdf for BCA BBA.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
gpt5_lecture_notes_comprehensive_20250812015547.pdf
cuic standard and advanced reporting.pdf
Electronic commerce courselecture one. Pdf
A comparative analysis of optical character recognition models for extracting...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A Presentation on Artificial Intelligence
MIND Revenue Release Quarter 2 2025 Press Release
Spectral efficient network and resource selection model in 5G networks
sap open course for s4hana steps from ECC to s4
Approach and Philosophy of On baking technology

Performance optimization 101 - Erlang Factory SF 2014