From b429ee47d82ebb8f033edfe739d6cf3d4f006d39 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 1 Aug 2026 11:47:54 -0400 Subject: [PATCH] Changes from before AGS where I was adding Lua perf tests but not magic_stats yet. --- Makefile | 5 +- meson.build | 1 + tests/data.hpp | 150 +++++++++++++++++++++++++++++++++ tests/lua_perf_tests.cpp | 136 ++++++++++++++++++++++++++++++ tests/main.cpp | 2 + tests/perf_tests.cpp | 178 +++++---------------------------------- tests/soa_tests.cpp | 2 - 7 files changed, 312 insertions(+), 162 deletions(-) create mode 100644 tests/data.hpp create mode 100644 tests/lua_perf_tests.cpp diff --git a/Makefile b/Makefile index 28f2cb8..0e7fb21 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -all: build test +all: build test_perf reset: ifeq '$(OS)' 'Windows_NT' @@ -18,6 +18,9 @@ debug_build: meson setup --wipe builddir -Db_ndebug=true --buildtype debugoptimized meson compile -j 10 -C builddir +test_perf: build + ./builddir/sol2_test -m "perf" + test: build ./builddir/sol2_test diff --git a/meson.build b/meson.build index 2adff3d..addbd9c 100644 --- a/meson.build +++ b/meson.build @@ -32,6 +32,7 @@ executable('sol2_test', [ 'tests/bao_tests.cpp', 'tests/soa_tests.cpp', 'tests/perf_tests.cpp', + 'tests/lua_perf_tests.cpp', 'src/stats.cpp', 'tests/main.cpp', ], diff --git a/tests/data.hpp b/tests/data.hpp new file mode 100644 index 0000000..bb82a38 --- /dev/null +++ b/tests/data.hpp @@ -0,0 +1,150 @@ +#pragma once + +struct LargeSOA { + std::vector a01; + std::vector a02; + std::vector a03; + std::vector a04; + std::vector a05; + std::vector a06; + std::vector a07; + std::vector a08; + std::vector a09; + std::vector a10; + std::vector a11; + std::vector a12; + std::vector a13; + std::vector a14; + std::vector a15; + std::vector a16; + std::vector a17; + std::vector a18; + std::vector a19; + std::vector a20; + + void push_back(int x) { + a01.push_back(x); + a02.push_back(x); + a03.push_back(x); + a04.push_back(x); + a05.push_back(x); + a06.push_back(x); + a07.push_back(x); + a08.push_back(x); + a09.push_back(x); + a10.push_back(x); + a11.push_back(x); + a12.push_back(x); + a13.push_back(x); + a14.push_back(x); + a15.push_back(x); + a16.push_back(x); + a17.push_back(x); + a18.push_back(x); + a19.push_back(x); + a20.push_back(x); + } +}; + +struct LargeStruct { + int a01; + int a02; + int a03; + int a04; + int a05; + int a06; + int a07; + int a08; + int a09; + int a10; + int a11; + int a12; + int a13; + int a14; + int a15; + int a16; + int a17; + int a18; + int a19; + int a20; + + LargeStruct(int x) : + a01(x), + a02(x), + a03(x), + a04(x), + a05(x), + a06(x), + a07(x), + a08(x), + a09(x), + a10(x), + a11(x), + a12(x), + a13(x), + a14(x), + a15(x), + a16(x), + a17(x), + a18(x), + a19(x), + a20(x) {} +}; + + +struct S1 { + int a01; + int a02; + int a03; + int a04; + + S1(int x) : a01(x), a02(x), a03(x), a04(x) { } +}; + +struct S2 { + int a05; + int a06; + int a07; + int a08; + S2(int x) : a05(x), a06(x), a07(x), a08(x) { } +}; + +struct S3 { + int a09; + int a10; + int a11; + int a12; + S3(int x) : a09(x), a10(x), a11(x), a12(x) { } +}; + +struct S4 { + int a13; + int a14; + int a15; + int a16; + S4(int x) : a13(x), a14(x), a15(x), a16(x) { } +}; + +struct S5 { + int a17; + int a18; + int a19; + int a20; + S5(int x) : a17(x), a18(x), a19(x), a20(x) { } +}; + +struct MultipleAOS { + std::vector s1; + std::vector s2; + std::vector s3; + std::vector s4; + std::vector s5; + + void push_back(int x) { + s1.push_back({x}); + s2.push_back({x}); + s3.push_back({x}); + s4.push_back({x}); + s5.push_back({x}); + } +}; diff --git a/tests/lua_perf_tests.cpp b/tests/lua_perf_tests.cpp new file mode 100644 index 0000000..bca83f1 --- /dev/null +++ b/tests/lua_perf_tests.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include "stats.hpp" +#include "data.hpp" +#define SOL_ALL_SAFETIES_ON 1 +#include + +using namespace fuc2; + +namespace lua_perf_tests { + struct Samples { + std::vector maos_many_for; + std::vector lua_many_for; + }; + + void test_maos_performance(Samples& samples, int sample_size, int sample_number) { + auto maos = new MultipleAOS(); + + for(int i = 0; i < sample_size; i++) { + maos->push_back(i); + } + + Stats maos_many_for; + for(int sample = 0; sample < sample_number; sample++) { + auto start = maos_many_for.time_start(); + + for(auto& el : maos->s1) { + el.a01 += el.a02 + sample; + el.a02 += el.a03 + sample; + el.a03 += el.a04 + sample; + } + + for(auto& el : maos->s2) { + el.a05 += el.a06 + sample; + el.a06 += el.a07 + sample; + el.a07 += el.a08 + sample; + } + + for(auto& el : maos->s3) { + el.a09 += el.a10 + sample; + el.a10 += el.a11 + sample; + el.a11 += el.a12 + sample; + } + + for(auto& el : maos->s4) { + el.a13 += el.a14 + sample; + el.a14 += el.a15 + sample; + el.a15 += el.a16 + sample; + } + + for(auto& el : maos->s5) { + el.a17 += el.a18 + sample; + el.a18 += el.a19 + sample; + el.a19 += el.a20 + sample; + } + + maos_many_for.sample_time(start); + } + + samples.maos_many_for.push_back(maos_many_for); + } + + void test_lua_maos_perf(Samples& samples, int sample_size, int sample_number) { + sol::state lua; + lua.open_libraries(sol::lib::base); + + auto maos = new MultipleAOS(); + + for(int i = 0; i < sample_size; i++) { + maos->push_back(i); + } + + // setup lua for MOAS + lua.new_usertype("S1", + "a01", &S1::a01, + "a02", &S1::a02, + "a03", &S1::a03, + "a04", &S1::a04); + + lua.script(R"( + function effed_handler(v, size, sample) + for i = 1,size,1 do + v[i].a01 = v[i].a01 + v[i].a02 + sample + v[i].a02 = v[i].a02 + v[i].a03 + sample + v[i].a03 = v[i].a03 + v[i].a04 + sample + end + end + + function handler(v, sample) + v.a01 = v.a01 + v.a02 + sample + v.a02 = v.a02 + v.a03 + sample + v.a03 = v.a03 + v.a04 + sample + end + )"); + + sol::function handler = lua["handler"]; + + Stats lua_many_for; + for(int sample = 0; sample < sample_number; sample++) { + auto start = lua_many_for.time_start(); + + for(auto& el : maos->s1) { + handler(el, sample); + } + + lua_many_for.sample_time(start); + } + + samples.lua_many_for.push_back(lua_many_for); + } + + void test_all_performance() { + Samples samples; + int sample_size = 100; + int sample_count = 100; + + for(int i = 0; i < 10; i++) { + test_maos_performance(samples, sample_size, sample_count); + test_lua_maos_perf(samples, sample_size, sample_count); + } + + for(size_t i = 0; i < samples.maos_many_for.size(); i++) { + fmt::println("{:.2f} {:.2f}", + samples.maos_many_for[i].mean(), + samples.lua_many_for[i].mean()); + } + } + + fuc2::Set TESTS{ + .name="lua_perf", + .tests={ + TEST(test_all_performance), + } + }; +} diff --git a/tests/main.cpp b/tests/main.cpp index 4b8932f..14e96bb 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -4,6 +4,7 @@ TEST_SET(sol2_tests); TEST_SET(bao_tests); TEST_SET(soa_tests); TEST_SET(perf_tests); +TEST_SET(lua_perf_tests); using namespace fuc2; @@ -13,6 +14,7 @@ int main(int argc, char* argv[]) { bao_tests::TESTS, soa_tests::TESTS, perf_tests::TESTS, + lua_perf_tests::TESTS, }; return run_tests(tests, argc, argv); diff --git a/tests/perf_tests.cpp b/tests/perf_tests.cpp index 4f46485..3b7c117 100644 --- a/tests/perf_tests.cpp +++ b/tests/perf_tests.cpp @@ -2,159 +2,11 @@ #include #include #include "stats.hpp" +#include "data.hpp" using namespace fuc2; namespace perf_tests { - struct LargeSOA { - std::vector a01; - std::vector a02; - std::vector a03; - std::vector a04; - std::vector a05; - std::vector a06; - std::vector a07; - std::vector a08; - std::vector a09; - std::vector a10; - std::vector a11; - std::vector a12; - std::vector a13; - std::vector a14; - std::vector a15; - std::vector a16; - std::vector a17; - std::vector a18; - std::vector a19; - std::vector a20; - - void push_back(int x) { - a01.push_back(x); - a02.push_back(x); - a03.push_back(x); - a04.push_back(x); - a05.push_back(x); - a06.push_back(x); - a07.push_back(x); - a08.push_back(x); - a09.push_back(x); - a10.push_back(x); - a11.push_back(x); - a12.push_back(x); - a13.push_back(x); - a14.push_back(x); - a15.push_back(x); - a16.push_back(x); - a17.push_back(x); - a18.push_back(x); - a19.push_back(x); - a20.push_back(x); - } - }; - - struct LargeStruct { - int a01; - int a02; - int a03; - int a04; - int a05; - int a06; - int a07; - int a08; - int a09; - int a10; - int a11; - int a12; - int a13; - int a14; - int a15; - int a16; - int a17; - int a18; - int a19; - int a20; - - LargeStruct(int x) : - a01(x), - a02(x), - a03(x), - a04(x), - a05(x), - a06(x), - a07(x), - a08(x), - a09(x), - a10(x), - a11(x), - a12(x), - a13(x), - a14(x), - a15(x), - a16(x), - a17(x), - a18(x), - a19(x), - a20(x) {} - }; - - - struct S1 { - int a01; - int a02; - int a03; - int a04; - - S1(int x) : a01(x), a02(x), a03(x), a04(x) { } - }; - - struct S2 { - int a05; - int a06; - int a07; - int a08; - S2(int x) : a05(x), a06(x), a07(x), a08(x) { } - }; - - struct S3 { - int a09; - int a10; - int a11; - int a12; - S3(int x) : a09(x), a10(x), a11(x), a12(x) { } - }; - - struct S4 { - int a13; - int a14; - int a15; - int a16; - S4(int x) : a13(x), a14(x), a15(x), a16(x) { } - }; - - struct S5 { - int a17; - int a18; - int a19; - int a20; - S5(int x) : a17(x), a18(x), a19(x), a20(x) { } - }; - - struct MultipleAOS { - std::vector s1; - std::vector s2; - std::vector s3; - std::vector s4; - std::vector s5; - - void push_back(int x) { - s1.push_back({x}); - s2.push_back({x}); - s3.push_back({x}); - s4.push_back({x}); - s5.push_back({x}); - } - }; - struct Samples { std::vector soa_big_for; std::vector soa_many_for; @@ -431,7 +283,7 @@ namespace perf_tests { int sample_size = 100; int sample_count = 100; - for(int i = 0; i < 100; i++) { + for(int i = 0; i < 10; i++) { // quick and dirty "randomness" if(i % 2 == 0) { test_large_soa(samples, sample_size, sample_count); @@ -480,24 +332,32 @@ namespace perf_tests { maos_many_for_2.sample(samples.maos_many_for_2[i].mean()); } + soa_big_for.dump("soa_big_for"); + soa_many_for.dump("soa_many_for"); + ls_big_for.dump("ls_big_for"); + ls_many_for.dump("ls_many_for"); + maos_big_for.dump("maos_big_for"); + maos_many_for.dump("maos_many_for"); + maos_many_for_2.dump("maos_many_for_2"); + // totally not sure if this t-test is legit, must study more soa_big_for.t_test(ls_big_for); soa_big_for.t_test(maos_big_for); - soa_many_for.t_test(ls_many_for); - soa_many_for.t_test(maos_many_for); + // soa_many_for.t_test(ls_many_for); + // soa_many_for.t_test(maos_many_for); - ls_many_for.t_test(maos_many_for); - ls_big_for.t_test(maos_big_for); + // ls_many_for.t_test(maos_many_for); + // ls_big_for.t_test(maos_big_for); - // the most pathologic case - fmt::println("---- {}", soa_big_for.mean() - ls_many_for.mean()); - soa_big_for.t_test(ls_many_for); + // // the most pathologic case + // fmt::println("---- {}", soa_big_for.mean() - ls_many_for.mean()); + // soa_big_for.t_test(ls_many_for); - fmt::println("--- MAOS many_for vs many_for_2"); + // fmt::println("--- MAOS many_for vs many_for_2"); maos_many_for.t_test(maos_many_for_2); - fmt::println("--- MAOS big_for vs many_for_2"); + // fmt::println("--- MAOS big_for vs many_for_2"); maos_big_for.t_test(maos_many_for_2); } diff --git a/tests/soa_tests.cpp b/tests/soa_tests.cpp index 9c00d66..bf2d283 100644 --- a/tests/soa_tests.cpp +++ b/tests/soa_tests.cpp @@ -10,8 +10,6 @@ using namespace fuc2; namespace soa_tests { - - struct SOAStyle { std::vector name; std::vector hp;