nanoMMO

MMORPG server load test, with flame graphs from Pyroscope

Bots that take quests, hunt for them and turn them in, two thousand of them against our server, with its CPU profiled all the time by Pyroscope, through eBPF, in Grafana.

Watch on YouTube

We load test our MMORPG's server again, twenty-six episodes after the last time, with bots that do what players do now: they take quests from the NPCs, hunt the monsters the quests ask for, and turn them in. And this time, the server is profiled all the time, with Grafana's Pyroscope: Alloy samples our C++ servers from the kernel with eBPF, with no change to their code, and Grafana shows a flame graph of where the worlds spend their CPU, under their metrics and logs. Then two thousand bots, and what the flame graph shows at the limit, from the broadcast to the kernel's UDP stack. This is episode 46 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.

Topics: Performance

The code

Each step on screen is a tag in the game's repository. The code at the end of the episode is ep046/end.

  1. 2:33ep046/step-1Bots that questBrowseChanges
  2. 13:42ep046/step-2Profiles, all the timeBrowseChanges

Transcript

Intro

0:00 Hello and welcome back to episode forty-six, where we're going to load test our server again, with bots that take quests, and keep a profile of its CPU all the time, with Pyroscope. Let's begin with a quick look at where we're going to be by the end of the episode.

0:17 A thousand bots are taking quests and hunting skeletons on our server, and the dashboard has their tick times, as before. Under the logs, there's a flame graph of where the server spends its CPU, which Pyroscope keeps all the time.

0:31 On the website of the series you can find more information about the project. The code for the project is open source on GitHub, tagged at each step of each episode so that you can follow along.

Last time

0:43 Now, before we get to today's work, let's quickly go over what we did last time. We fixed what CI found on the push before, and the NPCs became people, like the players. A quest is content, with the NPC who gives it, how many of which monster it asks for, and its reward. The server decides who can take a quest, and counts each kill for it, and turning it in gives the experience and all the prizes, or none. The quests are saved as JSON in the player's row, through Redis, an NPC with a quest for you has a mark over its name, and the world fuzzer takes and turns in quests too.

Today

1:17 We last measured how many players our server can hold in episode twenty, and since then we've added a lot: worlds, lands, chat, NPCs, skills, items, dungeons and quests, so today we're going to measure again. First, the bots are going to do what players do now, which means taking quests, hunting for them and turning them in. Then, instead of profiling the server by hand for ten seconds, like in episodes eight and twenty, we're going to keep its profiles all the time, with Grafana's Pyroscope, and look at them as a flame graph on our dashboard. Then we're going to put two thousand bots on the server, and see where its time goes at the limit. All right, let's get started, with the numbers from episode twenty.

Twenty-six episodes later

1:58 In episode twenty, with each player getting what's within sixty-four metres of them, and names and levels once a second, the server kept its tick within the fifty milliseconds it has, twenty times a second, as far as we measured. With twelve hundred players, the most we tried, the median tick took about twenty-eight milliseconds. Back then, the bots did no more than run around the island. Since then they've learnt to chat, ask the storyteller for stories, use skills, drink potions and wear what they find, but they still don't fight for anything, and none of them has taken a quest yet. So before we measure, the bots are going to quest.

Bots that quest

2:33 What a bot does for its quests goes in a file of its own, quests dot h, next to bag dot h, where the bots decide what to drink and wear. It takes optional, our content's header, for the quests, and the protocol's. An errand is a trip a bot makes for its quests. It's a struct, as it's plain data with no rule to keep between its fields, which is what the style guide keeps structs for. Its kind is an enum class, so its values live in its own scope, as Errand Kind k Hunt, and don't turn into integers by accident. A bot hunts a monster, takes a quest, or turns one in. Then where the bot goes, as an x and a z, and the quest, our protocol's message with the NPC's id and the quest's, which is what the server takes in take and in turn in. A hunt leaves it empty, as it goes to a monster. The fields have default values, so an errand made with designated initialisers, which name the fields they set, leaves the rest at their defaults. Then next errand, which decides the bot's next errand from the content, the quests the bot is on, what it sees of the world, and where it stands. It returns an optional, as a bot often has nothing to do for its quests where it is. It decides in this order: a quest the bot has done goes back to its giver, if the giver is here, and otherwise it goes after the nearest monster that a quest it's on asks for. Failing that, it takes a quest it hasn't done from an NPC here who gives it. Keeping that decision in a function of its own, apart from the bot's networking, means we can test it on a world we make up, with no server running. In quests dot c c, we take algorithm, c math for the distances, c std int, limits and optional. Giver, in the unnamed namespace, finds the NPC here who gives a quest. Ranges find takes a projection as its last argument, here each NPC's kind, so it compares the kinds with the quest's giver without a lambda. Giver returns a pointer to the NPC, or null when nobody here gives it. At giver comes next, for the trips to an NPC. It takes a kind, an NPC and a quest's id, and it returns its result by value, which is cheap, as that's a few numbers and a small message. It starts the errand at the NPC's position, which it copies out of the NPC's message. Then it fills in the quest, with the NPC's id and the quest's, for taking it or turning it in. Both helpers are small, and next errand uses each of them twice. Then next errand, with the signature from the header. It keeps the best hunt it has found so far in an optional, which starts empty, and the nearest distance starts at infinity, from limits, so that the first monster it finds is nearer. Then it goes over the quests the bot is on, a map from each quest's id to how many it has killed for it, and looks each one up in the content, skipping any that the content doesn't have. A quest with all its kills goes back to its giver straight away, if the giver is here, as there's nothing more to do for it. Otherwise, the bot looks at the monsters it sees, of the kind the quest asks for and alive, and keeps the nearest, over all the quests it's on. Std hypot gives the distance from the bot to each monster on the ground, from the differences in x and in z, as the height doesn't matter for a walk. It keeps that monster's distance in nearest, and an errand to hunt it, at its position. A hunt comes before taking another quest, so that a bot works on what it has. And last, it goes over the content's quests, in the order of their ids. It skips the ones it's on, and the ones it's done, which ranges find looks for in the done list, and takes the first one whose giver is here. When none of them is, it returns nothing, and the bot carries on wandering until it sees one. And in the bot's CMake file, the library gets quests dot c c, and the tests get its test.

6:25 In bot dot h, the bot takes our new header, and parts dot h. Parts is the class the client puts each world together in, from the datagrams it comes in over UDP, and it keeps the monsters and the NPCs it has heard of. Up to now, the bot looked at each datagram on its own, which was enough to know where it stood. An errand needs the whole world around the bot, so it gets a parts of its own, like the client. It keeps its quests as they come, and the errand the trip is for, when the trip is for a quest. In bot dot c c, c math, for the distance to the end of a trip. Two distances, in metres: how close a bot gets to a monster, well within the sword's reach of one point six, and how close it gets to an NPC, which hears you from four metres. The server sends a player's quests on their own, over TCP, like the bag, so the bot keeps them and returns. Each world the bot receives goes into parts, as it would in the client. Going through a gate drops the errand along with the trip, as its monster or NPC is in the land the bot left. Then wander, which runs each second or so. Between trips, quests come before the other trips a bot makes. Next errand looks at the world as parts has put it together, and an errand starts a trip towards its monster or NPC. The trip ends within one point two metres of a monster, and three metres of an NPC. Otherwise, the trips go where they went before, around the island and to the storyteller. That part doesn't change, apart from where it sits, under the errands, and a bot with no errand still takes one trip in eight to the storyteller once it has seen it. When travel returns no input, the trip has ended, so the bot checks how far it is from where it was going. It has to be closer than the trip's there, the distance we gave the trip for its errand. Travel also returns nothing for a trip that stopped getting closer, at the sea or a lake, so the distance is what tells the two apart. There, it swings, as a player holds space, or sends the quest to take or to turn in, which the server checks again, as it does for players. Each of those goes into the input sent to the server, along with what the bot does with its bag, as before. A bot that stopped getting closer, at the sea or a lake, wanders around for a while instead, so that it isn't stuck running into the water at a monster on the other side. And the next errand goes after whatever's nearest then, as monsters move.

8:54 The bot's quests get a test of their own, in quests test dot c c, next to the bag's. It takes the bot's quests header, c std int and optional, the content's header, for reading the game's own content, and GoogleTest. In the game's content, the guard gives bones by the gate, for three skeletons, so the constants at the top name the ids the test uses: the quest, the guard, and the two kinds of skeleton. Content reads the folder that CMake passes in, like in the bag's test. It dereferences the status or that read content returns, as the test counts on the game's own files reading fine. Next comes seen, which builds what a bot sees, the way parts puts it together for the bot. It fills in its message as the server would, with protobuf's add functions, which append an element to a repeated field and return a pointer to it. Each test gets a fresh one, so a test can change it without getting in another's way. It has the guard, with an id of its own, seven, ten metres east of the bot, which stands at the middle. A lambda adds a monster of a kind at an x. It captures the world by reference, which is what the ampersand in its brackets says. That keeps the three lines after it short: two skeletons, twenty metres east and eight metres west, and an Ash Skeleton two metres away, nearer than either. A bot on no quests takes bones by the gate from the guard. Next errand gets the game's content, an empty set of quests, what the bot sees, and the bot's position at the middle, at zero and zero. The test checks the errand's kind, the NPC, by the guard's id, and the quest, and that the bot heads for the guard, ten metres east. Then a bot on the quest, with one kill in its map of quests, which the test sets through mutable on, protobuf's way of changing a map field. It hunts the nearest skeleton, the one eight metres west, and not the Ash Skeleton, which is nearer, as the quest doesn't ask for those. That's the check that matters most, as a bot that went after the nearest monster of any kind wouldn't finish its quest. With all of the monsters dead, there's nothing to hunt, and nothing to take, as it's on the guard's quest already, so next errand returns nothing, and the bot wanders. This time, the map holds three kills for the quest, which is what it asks for, so the quest is done. The bot turns it in to the guard, by the guard's id and the quest's, and that comes before anything else. And a quest it's done stays done, as the server would refuse it again. With the guard's quest in its done list, and no others on, there's nothing here for the bot to do. Between them, the four tests have next errand return each kind of errand, and nothing, so a change that breaks one of those shows up here first.

11:38 We build the bot's tests at this step, and run them. The four new tests pass, along with the bag's two and the five wander tests.

11:48 To see the bots quest, we start the servers from compose, and sixty bots, and give them three minutes. Then we ask MySQL for their quests, as the saver wrote them into each player's row, grouped. Most of the bots are on the guard's quest, with between none and three kills, and some are on the Ash Warden's, as they went through the gate to the Ashlands. Some are on the healer's quest for the Crypt too, which a bot takes when it sees the healer and no skeleton nearby. And a few have done bones by the gate, and turned it in. One has even done the Ash Warden's, which asks for five Ash Skeletons.

Profiles, all the time

12:23 Pyroscope is Grafana's store for profiles, like Loki is for logs. Its README calls it a continuous profiling platform, which means it keeps the profiles of our programs all the time, the way Prometheus keeps their metrics. So when the server falls behind, the profile of that minute is already there, and we don't have to catch it in the act, like we did with perf in episodes eight and twenty. Profiles reach the Pyroscope server in a few ways: from an SDK compiled into the program, or from Grafana's Alloy, which we already run for our logs, or from OpenTelemetry's eBPF profiler. We're going to use Alloy, which comes with that eBPF profiler built in, so our servers' code doesn't change at all.

13:06 Here's how the pieces fit together, from our servers to Grafana. Our servers run as they are, with no profiling library in them. eBPF lets a program load small, checked programs into the Linux kernel, which run when something happens there. The profiler's program runs on a timer, on each CPU, and records the stack of whatever is running, our code and the kernel's alike. Alloy reads those stacks, keeps the ones from our servers' containers, and sends them to Pyroscope each fifteen seconds, which keeps them by service and time. And Grafana draws them as a flame graph, for whatever time range the dashboard shows.

13:42 In compose dot yaml, Pyroscope is a service from Grafana's image, on port forty forty. Alloy is going to do the sampling, which takes two privileges. Privileged lets it load its eBPF program into the kernel, and pid host puts it in the host's process namespace, so that it sees the processes of the other containers, our servers among them. On a Mac, the host is Docker's Linux VM, whose kernel all the containers share. And Alloy starts after Pyroscope, which it sends the profiles to. In production, Pyroscope listens on the server's localhost, like Loki and Grafana, so its port isn't open to the internet, whatever the firewall lets in. In Alloy's config, the servers come from the containers we already find for the logs. The new relabel block takes its targets from the one we wrote for Loki, which has each container's service as a label already. We keep our six C plus plus servers, by their service. Relabel rules run in order, on each target, and keep drops the targets whose service doesn't match. The service then becomes the service name, the label Pyroscope keeps profiles by. And the container's id is how the profiler tells which processes belong to which target. Pyroscope eBPF is the profiler, which samples the processes of those containers. A C plus plus compiler mangles each name, folding the namespaces and the argument types into one symbol, so demangle turns the symbols back into names we can read, such as nanommo server tick, without the arguments. And pyroscope write sends the profiles on to Pyroscope, at its address on Docker's network, port forty forty, where the Pyroscope container listens. Grafana gets Pyroscope as a data source, next to Prometheus and Loki, with its address on Docker's network. Its uid is pyroscope, which the panel names, so that the dashboard finds it on any machine. And the dashboard gets a panel at the bottom, a flame graph of where the worlds spend their CPU. It's the thirteenth panel, under the logs, the full width of the dashboard, and its data source is Pyroscope, by its uid. Its query asks Pyroscope for a profile, where the panels above it ask Prometheus and Loki for series and lines. Its profile type is the process's CPU time, in nanoseconds, which is what the eBPF profiler records. And its label selector picks the world servers by their service name, with a regular expression, so it shows Ember and Frost and their next instances, over the dashboard's time range.

16:18 With three hundred bots on the server for a couple of minutes, the panel has their profiles, which Alloy sends each fifteen seconds. On the right, the flame graph has the whole process at the top, and under each function, the functions it calls, as wide as the time spent in them. The table on the left lists the functions by the time spent in them alone. Most of the width goes down through the tick, to the broadcast, which sends each player the world around them, and from there into the kernel, sending the datagrams. We're going to come back to it with a lot more bots.

Two thousand bots

16:52 Now the benchmark, with the stack from compose, Pyroscope and Alloy among it. Load dot s h adds a hundred bots at a time, like in episode twenty, but twenty times this time, for two thousand, and asks Prometheus about the last twenty-five seconds after each step. It takes about seventeen minutes, so it's sped up. The bots run on this Mac too, sharing Docker's twelve CPUs with the server, and each of them now puts its world together and hunts.

17:18 On the left are the ticks per second, for each number of players. Episode twenty's server kept its twenty a second up to twelve hundred players, the most we measured then. Before this episode, I ran last episode's code with its bots, which don't quest, on this Mac, and it kept up to twelve hundred too. With bots that quest, and Pyroscope and the rest of the stack running, the server keeps its twenty up to thirteen hundred players. At fourteen hundred, it manages about eighteen point five ticks a second, so the world runs slower than real time. Past that, it falls steadily, and at two thousand it manages about eight. On the right, what the server sends each second. With eleven hundred players, today's bots get one hundred and ninety megabytes a second, where last episode's got about one hundred and thirty. Bots that quest probably gather where the quests are, around the guard and the skeletons near the gate, so each one has more of the others within sixty-four metres to be sent. And from thirteen hundred players on, what the server sends stays at about two hundred and sixty megabytes a second, however many more join. That's as much as it manages to send in a second, so with more players, each tick takes longer instead, and that's where the ticks per second fall. The flame graph is going to show where that time goes.

Where the time goes

18:41 Here's the world server's profile from Pyroscope, from a run of this benchmark, over its last three steps, with eighteen hundred to two thousand bots, starting from the tick. The tick takes about ninety percent of the server's CPU. The world's step, with the monsters, the fights, the loot and the quests, is a thin sliver of it, about two percent, so the game's own work costs the server very little. About seventy-six percent is the broadcast, which puts together what each player sees, and sends it. Most of that is in the kernel's UDP send, and most of the send isn't UDP or IP code at all. On this Mac, the bots are on the other end of Docker's network, so the kernel carries on and delivers the datagram, through Docker's bridge and its firewall rules, to the bot's socket, in the server's own system call. That's about thirty-three percent of the server's CPU, spent on the bots' side. Players on other machines wouldn't cost the server that part, so on one machine, our benchmark likely gives a lower limit than the server would have with players spread around the internet. And whichever way we count it, sending datagrams is what the server's time goes into, as we saw in episode twenty, so that's where a faster server would start.

Wrap-up

19:53 So, today the bots learnt to quest: they take quests from the NPCs, hunt the monsters the quests ask for, and turn them in, so the server counts and saves quests under load too. To see the monsters and the NPCs around it, each bot puts its world together from its parts, like the client does. Pyroscope keeps our servers' profiles all the time, which Alloy samples from the kernel with eBPF, with no change to our code, and the dashboard shows them as a flame graph, under the metrics and the logs. With bots that quest, the server keeps up to about thirteen hundred players on this Mac, and at the limit, its time goes into sending, about thirty-three percent of it on the bots' side of Docker's network.

Playing it

21:11 Let me know in the comments what you'd like to see next. And if you want to follow along with the series, subscribe to the channel. I hope you find this useful, and thanks for watching.