nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
bench_main.cc
Go to the documentation of this file.
1#include <raylib.h>
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6#include <fstream>
7#include <iostream>
8#include <memory>
9#include <numbers>
10#include <utility>
11#include <vector>
12
13#include "absl/status/statusor.h"
14#include "absl/strings/numbers.h"
15#include "client/atmosphere.h"
16#include "client/camera.h"
17#include "client/renderer.h"
18#include "client/window.h"
19#include "terrain/terrain.h"
20
21// Walks once around the middle of the island, through the woods, and draws
22// the same frames each time, as fast as it can.
23int main(int argc, char* argv[]) {
24 int frames = 1200;
25 uint32_t seed = 1;
26 if ((argc > 1 && !absl::SimpleAtoi(argv[1], &frames)) || frames < 1 ||
27 (argc > 2 && !absl::SimpleAtoi(argv[2], &seed))) {
28 std::cerr << "usage: nanommo_client_bench [frames] [seed] [results.json]\n";
29 return 1;
30 }
31 absl::StatusOr<std::unique_ptr<nanommo::Window>> window =
32 nanommo::Window::Create("nanoMMO bench", 1280, 720);
33 if (!window.ok()) {
34 std::cerr << "nanommo client bench: " << window.status() << "\n";
35 return 1;
36 }
37 SetTargetFPS(0);
38 const nanommo::Terrain terrain(seed);
39 nanommo::Renderer renderer;
40 renderer.set_terrain(terrain, 0);
42
43 constexpr float kRadius = 80.0f;
44 // The first frames load and warm up the GPU, so they don't count.
45 constexpr int kWarmUp = 60;
46 const float step = 2.0f * std::numbers::pi_v<float> / frames;
47 std::vector<double> times;
48 int64_t draws = 0;
49 int64_t triangles = 0;
50 double last = GetTime();
51 for (int frame = -kWarmUp; frame < frames && !(*window)->ShouldClose();
52 ++frame) {
53 const float angle = std::max(frame, 0) * step;
54 const float x = kRadius * std::sin(angle);
55 const float z = kRadius * std::cos(angle);
56 // Turning with us, so that it looks over us at the middle of the island.
57 camera.Update({x, terrain.Height(x, z), z}, frame > 0 ? step : 0.0f, 0.0f,
58 0.0f, terrain);
59 renderer.set_camera(camera.camera());
60 BeginDrawing();
61 renderer.Draw();
62 EndDrawing();
63 const double now = GetTime();
64 if (frame >= 0) {
65 times.push_back((now - last) * 1000.0);
66 draws += renderer.work().draws;
67 triangles += renderer.work().triangles;
68 }
69 last = now;
70 }
71
72 std::vector<double> sorted = times;
73 std::ranges::sort(sorted);
74 const auto percentile = [&sorted](double share) {
75 return sorted[static_cast<std::size_t>(share * (sorted.size() - 1))];
76 };
77 double total = 0.0;
78 for (const double time : times) {
79 total += time;
80 }
81 const double fps = 1000.0 * times.size() / total;
82 std::cout << "frames: " << times.size() << "\n"
83 << "frames per second: " << fps << "\n"
84 << "frame time p50: " << percentile(0.5) << " ms\n"
85 << "frame time p99: " << percentile(0.99) << " ms\n"
86 << "draws a frame: " << draws / std::ssize(times) << "\n"
87 << "triangles a frame: " << triangles / std::ssize(times) << "\n";
88 // And as JSON, for scripts and charts to read.
89 if (argc > 3) {
90 std::ofstream json(argv[3]);
91 json << "{\"frames\": " << times.size() << ", \"fps\": " << fps
92 << ", \"p50_ms\": " << percentile(0.5)
93 << ", \"p99_ms\": " << percentile(0.99)
94 << ", \"draws\": " << draws / std::ssize(times)
95 << ", \"triangles\": " << triangles / std::ssize(times) << "}\n";
96 if (!json) {
97 std::cerr << "nanommo client bench: couldn't write " << argv[3] << "\n";
98 return 1;
99 }
100 }
101 return 0;
102}
Follows your player around, from behind and above.
Definition camera.h:14
const Camera3D & camera() const
Definition camera.h:28
void Update(Vector3 player, float turn, float tilt, float zoom, const Terrain &terrain)
Definition camera.cc:31
const Work & work() const
Definition renderer.h:94
void set_camera(const Camera3D &camera)
Definition renderer.h:77
void set_terrain(const Terrain &terrain, int land)
Definition renderer.cc:408
void Draw() const
Definition renderer.cc:477
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
Definition terrain.cc:102
static absl::StatusOr< std::unique_ptr< Window > > Create(const std::string &title, int width, int height, Visibility visibility=Visibility::kShown)
Definition window.cc:13
int main()
Definition login_main.cc:28