+ All Categories
Home > Documents > Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and...

Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and...

Date post: 27-May-2020
Category:
Upload: others
View: 9 times
Download: 0 times
Share this document with a friend
50
Benchmarking C++ From video games to algorithmic trading Alexander Radchenko
Transcript
Page 1: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Benchmarking C++From video games toalgorithmic tradingAlexander Radchenko

Page 2: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Quiz. How long it takes to run ?• 3.5GHz Xeon at CentOS 7

�2

• Write your name • Write your guess as a single

number • Write time units clearly • Answers will be collected in the

next 5 minutes

Page 3: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Outline• Performance challenges in games • How games tackle performance • Performance challenges in trading • How trading tackles performance • Lightweight tracing use case

�3

Page 4: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

My background• Game development for 15 years • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii, iOS, Android, PC

• 3 years @ Optiver – Low latency trading systems

• Performance matters in both domains

�4

Page 5: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Why performance matters ?• Slow running game is no fun to play – Guess what’s the second most common complaint about any PC

game ? • Slow trading system is not making money – In fact, it might lose your money

�5

Page 6: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Games• Soft real-time systems • Performance is important • Normally run at 30 frames per second • Consistent CPU/GPU load • Occasional spikes • Throughput is the king

�6

Page 7: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Game loop• Performance as a currency – Graphics – Animations – Physics

PROCESS INPUT RENDERUPDATE

GAME

�7

Page 8: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Performance challenges in games• PC and Mobiles – Fragmented HW

• Game consoles – Fixed HW ☺

– They are cheap for a reason ☹ – Proprietary tools and devkits

�8

Page 9: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Performance challenges in games

�9

Page 10: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

How games tackle performance• Reference game levels • Custom profilers

• Whole game session • Single frame

�10

Page 11: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

World of Tanks• Online MMO shooter • Fragmented platform • Wide range of HW – Old laptops – High-end desktops – Everything in between

�11

Page 12: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Replays• Record incoming network traffic • Initially created to repro bugs • Very useful tool for performance testing • At some point released to the public

�12

Page 13: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Replays: problems• Protocol upgrades • Game map changes may invalidate replay • Security

�13

Page 14: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Regression testing and replays• Avoiding performance degradation • Categorize HW: low, medium, high • Run replays on a fixed set of HW • 2s / 5s window averaged frame rate

�14

Page 15: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Trading• Low latency request processing systems • Performance is a currency –Everyone will identify big opportunities –Race to the exchange –Winner takes all

�15

Page 16: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Trading• Most of the time system is idle • Bursts on big events • Latency is the king – Speed to take profitable trades – Speed to adjust our own orders

�16

Page 17: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Trading• Dedicated high end Linux HW • Speedlab environment to test performance • Lightweight tracing in speedlab and production • Using time series DB to store captured data –Easy data retrieval for given time range –Historical data analysis

�17

Page 18: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

TRADING STACK

Money loop

INFORMATION EXECUTION

STRATEGY

EXCHANGE

�18

Page 19: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Performance challenges in trading• Cache !

�19

Page 20: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Cache• Generally L3 is shared across all cores • Pick your neighbours wisely • HT threads share L1. – This is one of the reasons why we disable HT

• You want all your data to be in cache ! • Cache warming techniques – Keep running – Keep touching memory

�20

Page 21: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

How trading measures latency

Information

Auto trader

Execution

Auto trader

Software timestamps

Hardware timestamps

�21

TRADING STACK

INFORMATION

STRATEGY

EXCHANGE

EXECUTION

Page 22: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Using timestamps• Latency histograms – simulated environment – production

• Detecting outliers • Drilling down specific events

�22

Page 23: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Lightweight tracing• How light it is ? – HW timestamp cost is a few nanoseconds – SW timestamp is higher, still very cheap

• Very useful for understanding performance profile • Visualizing and recognizing patterns

�23

Page 24: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Low Latency Fizzbuzz • https://github.com/phejet/benchmarkingcpp_games_trading • C++ server which reads input data • Outputs Fizz, Buzz, FizzBuzz or just a number • How to make it fast ? • Measure first !!!

�24

Page 25: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Fizzbuzz• How long do you think it takes run this code ? • 3.5GHz Xeon at CentOS 7

�25

Page 26: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Quiz results

�26

Page 27: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Request processing

�27

Page 28: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Timing

�28

Page 29: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Timing

�29

Page 30: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Using Epoch

�30

Page 31: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Timings output

�31

Page 32: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Macro benchmark

�32

Page 33: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Quick feedback• Time in nanoseconds

�33

Page 34: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Jupyter notebooks• Open-source web application • Create and share documents that contain – Live code – Equations – Visualizations – Narrative text

�34

Page 35: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Jupyter notebook for in-depth analysis

�35

Page 36: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Histogram as text

Looks big

�36

Page 37: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Beware of outliers

Outlier

�37

Page 38: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Discarding outliers

Max value more reasonable

�38

Page 39: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Distribution is strange…

Not unimodal ?

�39

Page 40: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Bimodal distribution

�40

Page 41: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Optiver profiler

�41

• In-house tracing profiler • Mark interesting parts of your code – Scope guards to capture entry/exit timestamps and function name – Single named events

• Nanosecond precision • Multiple tools to view results • Tarantula is the most interesting one

Page 42: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Tarantula

�42

Page 43: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Two codepaths !

Non FizzBuzz code path

�43

Page 44: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Optimisation

�44

• FizzBuzz logic is the most expensive part of our request processing • How can we make it faster ?

Page 45: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Brute force approach

�45

• Write custom function instead of using std::to_string • Return result as const char* and use static buffer

Page 46: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Look at high level

�46

Page 47: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Avoid int->string conversion

�47

Page 48: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Measuring Optimised code

�48

Page 49: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Closing• It’s very hard to guess execution time by just looking at code • Having a simple and reproducible way to measure performance is

very important • Visualising performance data helps to understand it • Understanding is a necessary first step before optimization • When optimizing code, always look at the high level picture

�49

Page 50: Benchmarking C++ From video games to algorithmic trading€¦ · • 3D graphics programming and optimisation • Shipped 8 titles on various platforms – PS2, PS3, Xbox 360, Wii,

Questions ?

• Alexander Radchenko • [email protected] • https://github.com/phejet/benchmarkingcpp_games_trading • @phejet on Twitter


Recommended