harfbuzz/perf/benchmark-shape.cc

142 lines
3.4 KiB
C++
Raw Normal View History

#include "benchmark/benchmark.h"
#include <cstring>
#include "hb.h"
2022-05-02 22:03:15 +02:00
2022-05-21 01:21:04 +02:00
#define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
struct test_input_t
{
const char *text_path;
const char *font_path;
2022-05-21 01:21:04 +02:00
bool is_variable;
} default_tests[] =
{
{"perf/texts/fa-thelittleprince.txt",
2022-05-21 01:21:04 +02:00
"perf/fonts/Amiri-Regular.ttf",
false},
{"perf/texts/fa-thelittleprince.txt",
2022-05-21 01:21:04 +02:00
"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
false},
{"perf/texts/fa-monologue.txt",
2022-05-21 01:21:04 +02:00
"perf/fonts/Amiri-Regular.ttf",
false},
{"perf/texts/fa-monologue.txt",
2022-05-21 01:21:04 +02:00
"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
false},
{"perf/texts/en-thelittleprince.txt",
"perf/fonts/Roboto-Regular.ttf",
false},
{"perf/texts/en-thelittleprince.txt",
2022-05-21 01:21:04 +02:00
SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf",
true},
{"perf/texts/en-words.txt",
"perf/fonts/Roboto-Regular.ttf",
false},
{"perf/texts/en-words.txt",
2022-05-21 01:21:04 +02:00
SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf",
true},
};
test_input_t *tests = default_tests;
unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
2022-05-02 22:03:15 +02:00
2022-05-21 01:21:04 +02:00
static void BM_Shape (benchmark::State &state,
bool is_var,
const test_input_t &input)
{
hb_font_t *font;
{
hb_blob_t *blob = hb_blob_create_from_file_or_fail (input.font_path);
assert (blob);
hb_face_t *face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
font = hb_font_create (face);
hb_face_destroy (face);
}
2022-05-21 01:21:04 +02:00
if (is_var)
{
hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
hb_font_set_variations (font, &wght, 1);
}
hb_blob_t *text_blob = hb_blob_create_from_file_or_fail (input.text_path);
assert (text_blob);
unsigned orig_text_length;
const char *orig_text = hb_blob_get_data (text_blob, &orig_text_length);
hb_buffer_t *buf = hb_buffer_create ();
for (auto _ : state)
{
unsigned text_length = orig_text_length;
const char *text = orig_text;
const char *end;
while ((end = (const char *) memchr (text, '\n', text_length)))
{
hb_buffer_clear_contents (buf);
hb_buffer_add_utf8 (buf, text, text_length, 0, end - text);
hb_buffer_guess_segment_properties (buf);
hb_shape (font, buf, nullptr, 0);
unsigned skip = end - text + 1;
text_length -= skip;
text += skip;
}
}
hb_buffer_destroy (buf);
hb_blob_destroy (text_blob);
hb_font_destroy (font);
}
int main(int argc, char** argv)
{
benchmark::Initialize(&argc, argv);
if (argc > 2)
{
num_tests = 1;
tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
tests[0].is_variable = true;
tests[0].text_path = argv[1];
tests[0].font_path = argv[2];
}
for (unsigned i = 0; i < num_tests; i++)
{
auto& test_input = tests[i];
2022-05-21 01:21:04 +02:00
for (int variable = 0; variable < int (test_input.is_variable) + 1; variable++)
{
char name[1024] = "BM_Shape";
const char *p;
strcat (name, "/");
p = strrchr (test_input.text_path, '/');
strcat (name, p ? p + 1 : test_input.text_path);
strcat (name, "/");
p = strrchr (test_input.font_path, '/');
strcat (name, p ? p + 1 : test_input.font_path);
2022-05-21 01:21:04 +02:00
strcat (name, variable ? "/var" : "");
2022-05-21 01:21:04 +02:00
benchmark::RegisterBenchmark (name, BM_Shape, variable, test_input)
->Unit(benchmark::kMillisecond);
}
}
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
if (tests != default_tests)
free (tests);
}