Skip to content

Commit

Permalink
Use the custom counters correctly for the benchmarks (I think)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed Mar 12, 2024
1 parent 65dda86 commit a57770f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 27 deletions.
42 changes: 30 additions & 12 deletions src/benchmark/benchmarks/benchmark.assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace zasm::benchmarks
Program program(MachineMode::AMD64);
Assembler assembler(program);

size_t numInstructions = 0;
for (auto _ : state)
{
state.PauseTiming();
Expand All @@ -21,9 +22,12 @@ namespace zasm::benchmarks

assembler.nop();

state.counters["Instructions"] = benchmark::Counter(
1.0, benchmark::Counter::kIsIterationInvariantRate, benchmark::Counter::OneK::kIs1000);
numInstructions++;
}

state.counters["Instructions"] = benchmark::Counter(
static_cast<double>(numInstructions), benchmark::Counter::kIsIterationInvariantRate,
benchmark::Counter::OneK::kIs1000);
}
BENCHMARK(BM_Assembler_EmitSingle_0_Operands)->Unit(benchmark::kMicrosecond);

Expand All @@ -34,6 +38,7 @@ namespace zasm::benchmarks
Program program(MachineMode::AMD64);
Assembler assembler(program);

size_t numInstructions = 0;
for (auto _ : state)
{
state.PauseTiming();
Expand All @@ -43,9 +48,12 @@ namespace zasm::benchmarks

assembler.push(rax);

state.counters["Instructions"] = benchmark::Counter(
1.0, benchmark::Counter::kIsIterationInvariantRate, benchmark::Counter::OneK::kIs1000);
numInstructions++;
}

state.counters["Instructions"] = benchmark::Counter(
static_cast<double>(numInstructions), benchmark::Counter::kIsIterationInvariantRate,
benchmark::Counter::OneK::kIs1000);
}
BENCHMARK(BM_Assembler_EmitSingle_1_Operands)->Unit(benchmark::kMicrosecond);

Expand All @@ -56,6 +64,7 @@ namespace zasm::benchmarks
Program program(MachineMode::AMD64);
Assembler assembler(program);

size_t numInstructions = 0;
for (auto _ : state)
{
state.PauseTiming();
Expand All @@ -65,9 +74,12 @@ namespace zasm::benchmarks

assembler.mov(rax, rax);

state.counters["Instructions"] = benchmark::Counter(
1.0, benchmark::Counter::kIsIterationInvariantRate, benchmark::Counter::OneK::kIs1000);
numInstructions++;
}

state.counters["Instructions"] = benchmark::Counter(
static_cast<double>(numInstructions), benchmark::Counter::kIsIterationInvariantRate,
benchmark::Counter::OneK::kIs1000);
}
BENCHMARK(BM_Assembler_EmitSingle_2_Operands)->Unit(benchmark::kMicrosecond);

Expand All @@ -78,6 +90,7 @@ namespace zasm::benchmarks
Program program(MachineMode::AMD64);
Assembler assembler(program);

size_t numInstructions = 0;
for (auto _ : state)
{
state.PauseTiming();
Expand All @@ -87,9 +100,12 @@ namespace zasm::benchmarks

assembler.rorx(rcx, rdx, Imm(1));

state.counters["Instructions"] = benchmark::Counter(
1.0, benchmark::Counter::kIsIterationInvariantRate, benchmark::Counter::OneK::kIs1000);
numInstructions++;
}

state.counters["Instructions"] = benchmark::Counter(
static_cast<double>(numInstructions), benchmark::Counter::kIsIterationInvariantRate,
benchmark::Counter::OneK::kIs1000);
}
BENCHMARK(BM_Assembler_EmitSingle_3_Operands)->Unit(benchmark::kMicrosecond);

Expand All @@ -100,6 +116,7 @@ namespace zasm::benchmarks
Program program(MachineMode::AMD64);
Assembler assembler(program);

size_t numInstructions = 0;
for (auto _ : state)
{
state.PauseTiming();
Expand All @@ -110,12 +127,13 @@ namespace zasm::benchmarks
for (const auto& instr : zasm::tests::data::Instructions)
{
instr.emitter(assembler);
numInstructions++;
}

state.counters["Instructions"] = benchmark::Counter(
static_cast<double>(program.size()), benchmark::Counter::kIsIterationInvariantRate,
benchmark::Counter::OneK::kIs1000);
}

state.counters["Instructions"] = benchmark::Counter(
static_cast<double>(numInstructions), benchmark::Counter::kIsIterationInvariantRate,
benchmark::Counter::OneK::kIs1000);
}
BENCHMARK(BM_Assembler_EmitAll)->Unit(benchmark::kMillisecond);

Expand Down
9 changes: 6 additions & 3 deletions src/benchmark/benchmarks/benchmark.formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ namespace zasm::benchmarks
}
}

size_t numNodes = 0;

for (auto _ : state)
{
auto res = formatter::toString(program);
benchmark::DoNotOptimize(res);

state.counters["PrintedNodes"] = benchmark::Counter(
static_cast<double>(program.size()), benchmark::Counter::kIsIterationInvariantRate,
benchmark::Counter::OneK::kIs1000);
numNodes += program.size();
}

state.counters["PrintedNodes"] = benchmark::Counter(
static_cast<double>(numNodes), benchmark::Counter::kIsRate, benchmark::Counter::OneK::kIs1000);
}
BENCHMARK(BM_Formatter_Program)->Unit(benchmark::kMillisecond);

Expand Down
9 changes: 7 additions & 2 deletions src/benchmark/benchmarks/benchmark.instructioninfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace zasm::benchmarks
Program program(MachineMode::AMD64);
Assembler assembler(program);

size_t numInstructions = 0;

for (auto s : state)
{
const auto count = std::size(tests::data::Instructions);
Expand All @@ -33,9 +35,12 @@ namespace zasm::benchmarks
benchmark::DoNotOptimize(instrInfo);
}

state.counters["InstructionInfos"] = benchmark::Counter(
static_cast<double>(count), benchmark::Counter::kIsIterationInvariantRate, benchmark::Counter::OneK::kIs1000);
numInstructions += count;
}

state.counters["InstructionInfos"] = benchmark::Counter(
static_cast<double>(numInstructions), benchmark::Counter::kIsRate,
benchmark::Counter::OneK::kIs1000);
}
BENCHMARK(BM_InstructionInfo)->Unit(benchmark::kMillisecond);

Expand Down
26 changes: 16 additions & 10 deletions src/benchmark/benchmarks/benchmark.serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,18 @@ namespace zasm::benchmarks
}
}
BENCHMARK(BM_SerializationBasic)->Unit(benchmark::kMillisecond);

template<int64_t TLabelStep>
static void BM_SerializationWithLabels(benchmark::State& state)

template<int64_t TLabelStep> static void BM_SerializationWithLabels(benchmark::State& state)
{
using namespace zasm::x86;

Program program(MachineMode::AMD64);
Assembler assembler(program);
Serializer serializer;

size_t numBytesEncoded = 0;
size_t numInstructions = 0;

for (auto _ : state)
{
state.PauseTiming();
Expand Down Expand Up @@ -95,16 +97,20 @@ namespace zasm::benchmarks

serializer.serialize(program, 0x00400000);

state.counters["BytesEncoded"] = benchmark::Counter(
static_cast<double>(serializer.getCodeSize()), benchmark::Counter::kIsIterationInvariantRate,
benchmark::Counter::OneK::kIs1024);
state.counters["Instructions"] = benchmark::Counter(
static_cast<double>(count), benchmark::Counter::kIsIterationInvariantRate,
benchmark::Counter::OneK::kIs1000);
numBytesEncoded += serializer.getCodeSize();
numInstructions += count;
}

state.counters["BytesEncoded"] = benchmark::Counter(
static_cast<double>(numBytesEncoded), benchmark::Counter::kIsRate,
benchmark::Counter::OneK::kIs1024);

state.counters["Instructions"] = benchmark::Counter(
static_cast<double>(numInstructions), benchmark::Counter::kIsRate,
benchmark::Counter::OneK::kIs1000);
}
BENCHMARK_TEMPLATE(BM_SerializationWithLabels, 128)->Unit(benchmark::kMillisecond);

BENCHMARK_TEMPLATE(BM_SerializationWithLabels, 64)->Unit(benchmark::kMillisecond);

BENCHMARK_TEMPLATE(BM_SerializationWithLabels, 32)->Unit(benchmark::kMillisecond);
Expand Down

0 comments on commit a57770f

Please sign in to comment.