Benchmarks
These benchmarks measure Mockolate's runtime overhead against Moq, NSubstitute, FakeItEasy, TUnit.Mocks and Imposter for the same end-to-end mocking flow.
- Suite:
Mockolate.Benchmarksdriven by BenchmarkDotNet. - Job:
MediumRunwith the in-process toolchain. - Runner:
ubuntu-latestGitHub Actions agent, refreshed on every push tomain. - Each row shows time and allocated memory, sorted fastest-first; bars are scaled within the row so length corresponds to magnitude. The Mockolate row is highlighted as the baseline (
1.00x) and every other row shows its ratio relative to Mockolate. Lower is better. - Benchmarks parameterized with
[Params(1, 10)](Method/Property/Indexer) repeat their inner body 1× or 10× to surface fixed-cost vs per-call overhead; switch between them with the inline tabs.
- Method
- Property
- Indexer
- Event
- Callback
- Create mock
Set up a method, call it N times with It.IsAny<int>(), then verify it ran exactly N times.
IMyMethodInterface mock = IMyMethodInterface.CreateMock();
mock.Mock.Setup.MyFunc(It.IsAny<int>()).Returns(true);
for (int i = 0; i < N; i++)
mock.MyFunc(42);
mock.Mock.Verify.MyFunc(It.IsAny<int>()).Exactly(N);
Initialize an int property to 42, read+write it N times, then verify the getter and setter each ran exactly N times.
IMyPropertyInterface mock = IMyPropertyInterface.CreateMock();
mock.Mock.Setup.Counter.InitializeWith(42);
for (int i = 0; i < N; i++)
{
_ = mock.Counter;
mock.Counter = i;
}
mock.Mock.Verify.Counter.Got().Exactly(N);
mock.Mock.Verify.Counter.Set(It.IsAny<int>()).Exactly(N);
Set up a string this[int] indexer, read+write it N times, then verify the getter and setter each ran exactly N times.
IMyIndexerInterface mock = IMyIndexerInterface.CreateMock();
mock.Mock.Setup[It.IsAny<int>()].Returns("foo");
for (int i = 0; i < N; i++)
{
_ = mock[42];
mock[42] = "bar";
}
mock.Mock.Verify[It.IsAny<int>()].Got().Exactly(N);
mock.Mock.Verify[It.IsAny<int>()].Set(It.IsAny<string>()).Exactly(N);
Subscribe to a mock event, raise it once, then verify the subscription was recorded.
IMyEventInterface mock = IMyEventInterface.CreateMock();
EventHandler handler = (_, _) => { };
mock.SomeEvent += handler;
mock.Mock.Raise.SomeEvent(null, EventArgs.Empty);
mock.Mock.Verify.SomeEvent.Subscribed().Once();
Configure a callback on a method invocation and trigger it twice. This measures the per-invocation cost of side-effect setups.
int count = 0;
IMyCallbackInterface mock = IMyCallbackInterface.CreateMock();
mock.Mock.Setup.MyFunc(It.IsAny<int>()).Do(() => count++);
mock.MyFunc(1);
mock.MyFunc(2);
Cost of constructing an empty mock: no setup, no invocations, no verification. The fixed overhead every test pays.
ICalculatorService.CreateMock();