From 884e6c0e8cd8b58a35b2cd3d357fcb3122a7e898 Mon Sep 17 00:00:00 2001 From: Richard Date: Sat, 25 Jan 2025 15:05:55 -0800 Subject: [PATCH] test: add patch benchmark --- tests/benchmarks/atom.bench.luau | 46 ++++++++++++++--------------- tests/benchmarks/patch.bench.luau | 48 +++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 tests/benchmarks/patch.bench.luau diff --git a/tests/benchmarks/atom.bench.luau b/tests/benchmarks/atom.bench.luau index af410ee..3bae3ca 100644 --- a/tests/benchmarks/atom.bench.luau +++ b/tests/benchmarks/atom.bench.luau @@ -4,29 +4,27 @@ local Charm = require(ReplicatedStorage.DevPackages.Charm) local computed = Charm.computed local atom = Charm.atom -local function parameterGenerator() - return atom(0) -end - -local benchmarks = { - atom = function(_, state: any) - for _ = 1, 500 do - state(math.random(1, 2)) - end - end, - - computed = function(_, state: any) - computed(function() - return 2 * state() - end) - - for _ = 1, 500 do - state(math.random(1, 2)) - end - end, -} - return { - ParameterGenerator = parameterGenerator, - Functions = benchmarks, + ParameterGenerator = function() end, + Functions = { + atom = function() + local state = atom(0) + + for _ = 1, 500 do + state(math.random(1, 2)) + end + end, + + computed = function() + local state = atom(0) + + local _double = computed(function() + return 2 * state() + end) + + for _ = 1, 500 do + state(math.random(1, 2)) + end + end, + }, } diff --git a/tests/benchmarks/patch.bench.luau b/tests/benchmarks/patch.bench.luau new file mode 100644 index 0000000..6ea53f6 --- /dev/null +++ b/tests/benchmarks/patch.bench.luau @@ -0,0 +1,48 @@ +local ReplicatedStorage = game:GetService("ReplicatedStorage") + +local patch = require(ReplicatedStorage.DevPackages.CharmSync.patch) + +local USERS = 1000 + +local function generate() + local users = {} + + for user = 1, USERS do + users[user] = { + coins = math.random(1, 2), + gems = math.random(1, 2), + } + end + + return { users = users } +end + +local function update(state) + state = table.clone(state) + state.users = table.clone(state.users) + + for user = 1, USERS do + if math.random() > 0.5 then + continue + end + + state.users[user] = { + coins = math.random(1, 2), + gems = math.random(1, 2), + } + end + + return state +end + +return { + ParameterGenerator = function() + local state = generate() + return state, update(state) + end, + Functions = { + diff = function(_, a, b) + patch.diff(a, b) + end, + }, +}