Skip to content

Commit

Permalink
Add: Small base64 benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Nov 2, 2023
1 parent 253a8a5 commit 2fc6d59
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/benchmarks.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include <fstream>
#include <random>

#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>

#include "simdjson.h"

#include <fastgltf/parser.hpp>
#include <fastgltf/base64.hpp>
#include "gltf_path.hpp"

constexpr auto benchmarkOptions = fastgltf::Options::DontRequireValidAssetMember;
Expand Down Expand Up @@ -315,3 +317,58 @@ TEST_CASE("Small CRC32-C benchmark", "[gltf-benchmark]") {
};
}
#endif

TEST_CASE("Compare base64 decoding performance", "[gltf-benchmark]") {
std::string base64Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
constexpr std::size_t bufferSize = 2 * 1024 * 1024;

// We'll generate a random base64 buffer
std::random_device device;
std::mt19937 gen(device());
std::uniform_int_distribution<> distribution(0, base64Characters.size() - 1);
std::string generatedData;
generatedData.reserve(bufferSize);
for (std::size_t i = 0; i < bufferSize; ++i) {
generatedData.push_back(base64Characters[distribution(gen)]);
}

BENCHMARK("Run tinygltf's base64 decoder") {
return tinygltf::base64_decode(generatedData);
};

cgltf_options options {};
BENCHMARK("Run cgltf's base64 decoder") {
auto padding = fastgltf::base64::getPadding(generatedData);
auto outputSize = fastgltf::base64::getOutputSize(generatedData.size(), padding);
std::string output;
output.resize(outputSize);
auto* outputData = output.data();
return cgltf_load_buffer_base64(&options, generatedData.size(), generatedData.data(), reinterpret_cast<void**>(&outputData));
};

BENCHMARK("Run fastgltf's fallback base64 decoder") {
return fastgltf::base64::fallback_decode(generatedData);
};

#if defined(FASTGLTF_IS_X86)
const auto& impls = simdjson::get_available_implementations();
if (const auto* sse4 = impls["westmere"]; sse4 != nullptr && sse4->supported_by_runtime_system()) {
BENCHMARK("Run fastgltf's SSE4 base64 decoder") {
return fastgltf::base64::sse4_decode(generatedData);
};
}

if (const auto* avx2 = impls["haswell"]; avx2 != nullptr && avx2->supported_by_runtime_system()) {
BENCHMARK("Run fastgltf's AVX2 base64 decoder") {
return fastgltf::base64::avx2_decode(generatedData);
};
}
#elif defined(FASTGLTF_IS_A64)
const auto& impls = simdjson::get_available_implementations();
if (const auto* neon = impls["arm64"]; avx2 != nullptr && neon->supported_by_runtime_system()) {
BENCHMARK("Run fastgltf's Neon base64 decoder") {
return fastgltf::base64::neon_decode(generatedData);
};
}
#endif
}

0 comments on commit 2fc6d59

Please sign in to comment.