Merge pull request #3613 from harfbuzz/threads-test

Threads test
This commit is contained in:
Behdad Esfahbod 2022-05-30 05:52:21 -06:00 committed by GitHub
commit 3e64abd5d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 262 additions and 6 deletions

View File

@ -437,6 +437,7 @@ test/shape/data/text-rendering-tests/Makefile
test/subset/Makefile test/subset/Makefile
test/subset/data/Makefile test/subset/data/Makefile
test/subset/data/repack_tests/Makefile test/subset/data/repack_tests/Makefile
test/threads/Makefile
perf/Makefile perf/Makefile
docs/Makefile docs/Makefile
docs/version.xml docs/version.xml

View File

@ -1,4 +1,5 @@
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
#include <cassert>
#include <cstring> #include <cstring>
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
@ -29,8 +30,8 @@ struct test_input_t
{false, SUBSET_FONT_BASE_PATH "NotoSerifMyanmar-Regular.otf"}, {false, SUBSET_FONT_BASE_PATH "NotoSerifMyanmar-Regular.otf"},
}; };
test_input_t *tests = default_tests; static test_input_t *tests = default_tests;
unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]); static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
enum backend_t { HARFBUZZ, FREETYPE }; enum backend_t { HARFBUZZ, FREETYPE };

View File

@ -3,6 +3,7 @@
*/ */
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
#include <cassert>
#include <cstdlib> #include <cstdlib>
#include "hb.h" #include "hb.h"

View File

@ -3,6 +3,7 @@
*/ */
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
#include <cassert>
#include <cstdlib> #include <cstdlib>
#include "hb.h" #include "hb.h"

View File

@ -5,6 +5,8 @@
#include "config.h" #include "config.h"
#endif #endif
#include <cassert>
#include "hb.h" #include "hb.h"
#include "hb-ot.h" #include "hb-ot.h"
#ifdef HAVE_FREETYPE #ifdef HAVE_FREETYPE
@ -53,8 +55,8 @@ struct test_input_t
true}, true},
}; };
test_input_t *tests = default_tests; static test_input_t *tests = default_tests;
unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]); static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
enum backend_t { HARFBUZZ, FREETYPE }; enum backend_t { HARFBUZZ, FREETYPE };

View File

@ -1,4 +1,5 @@
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
#include <cassert>
#include <cstring> #include <cstring>
#include "hb-subset.h" #include "hb-subset.h"

View File

@ -29,7 +29,6 @@
#ifndef HB_HH #ifndef HB_HH
#define HB_HH #define HB_HH
#ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning( disable: 4068 ) /* Unknown pragma */ #pragma warning( disable: 4068 ) /* Unknown pragma */

View File

@ -2,7 +2,7 @@
NULL = NULL =
EXTRA_DIST = EXTRA_DIST =
SUBDIRS = api shape subset fuzzing SUBDIRS = api shape subset fuzzing threads
EXTRA_DIST += \ EXTRA_DIST += \
meson.build \ meson.build \

View File

@ -2,3 +2,4 @@ subdir('api')
subdir('shape') subdir('shape')
subdir('subset') subdir('subset')
subdir('fuzzing') subdir('fuzzing')
subdir('threads')

16
test/threads/Makefile.am Normal file
View File

@ -0,0 +1,16 @@
# Process this file with automake to produce Makefile.in
NULL =
EXTRA_DIST =
SUBDIRS =
EXTRA_DIST += \
meson.build \
hb-shape-threads.cc \
$(NULL)
# Convenience targets:
lib:
@$(MAKE) $(AM_MAKEFLAGS) -C $(top_builddir)/src lib
-include $(top_srcdir)/git.mk

View File

@ -0,0 +1,220 @@
#include <cassert>
#include <cstring>
#include <thread>
#include <condition_variable>
#include <vector>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "hb.h"
#include "hb-ot.h"
#ifdef HAVE_FREETYPE
#include "hb-ft.h"
#endif
#define SUBSET_FONT_BASE_PATH "test/subset/data/fonts/"
struct test_input_t
{
const char *text_path;
const char *font_path;
bool is_variable;
} default_tests[] =
{
{"perf/texts/fa-thelittleprince.txt",
"perf/fonts/Amiri-Regular.ttf",
false},
{"perf/texts/fa-thelittleprince.txt",
"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
false},
{"perf/texts/fa-monologue.txt",
"perf/fonts/Amiri-Regular.ttf",
false},
{"perf/texts/fa-monologue.txt",
"perf/fonts/NotoNastaliqUrdu-Regular.ttf",
false},
{"perf/texts/en-thelittleprince.txt",
"perf/fonts/Roboto-Regular.ttf",
false},
{"perf/texts/en-thelittleprince.txt",
SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf",
true},
{"perf/texts/en-words.txt",
"perf/fonts/Roboto-Regular.ttf",
false},
{"perf/texts/en-words.txt",
SUBSET_FONT_BASE_PATH "SourceSerifVariable-Roman.ttf",
true},
};
static test_input_t *tests = default_tests;
static unsigned num_tests = sizeof (default_tests) / sizeof (default_tests[0]);
enum backend_t { HARFBUZZ, FREETYPE };
// https://en.cppreference.com/w/cpp/thread/condition_variable/wait
static std::condition_variable cv;
static std::mutex cv_m;
static bool ready = false;
static unsigned num_repetitions = 1;
static unsigned num_threads = 3;
static void shape (const test_input_t &input,
hb_font_t *font)
{
// Wait till all threads are ready.
{
std::unique_lock<std::mutex> lk (cv_m);
cv.wait(lk, [] {return ready;});
}
const char *lang_str = strrchr (input.text_path, '/');
lang_str = lang_str ? lang_str + 1 : input.text_path;
hb_language_t language = hb_language_from_string (lang_str, -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 ();
hb_buffer_set_flags (buf, HB_BUFFER_FLAG_VERIFY);
for (unsigned i = 0; i < num_repetitions; i++)
{
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_buffer_set_language (buf, language);
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);
}
static void test_backend (backend_t backend,
const char *backend_name,
bool variable,
const test_input_t &test_input)
{
char name[1024] = "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);
strcat (name, variable ? "/var" : "");
strcat (name, "/");
strcat (name, backend_name);
printf ("Testing %s\n", name);
hb_font_t *font;
{
hb_blob_t *blob = hb_blob_create_from_file_or_fail (test_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);
}
if (variable)
{
hb_variation_t wght = {HB_TAG ('w','g','h','t'), 500};
hb_font_set_variations (font, &wght, 1);
}
switch (backend)
{
case HARFBUZZ:
hb_ot_font_set_funcs (font);
break;
case FREETYPE:
#ifdef HAVE_FREETYPE
hb_ft_font_set_funcs (font);
#endif
break;
}
std::vector<std::thread> threads;
for (unsigned i = 0; i < num_threads; i++)
threads.push_back (std::thread (shape, test_input, font));
{
std::unique_lock<std::mutex> lk (cv_m);
ready = true;
}
cv.notify_all();
for (unsigned i = 0; i < num_threads; i++)
threads[i].join ();
hb_font_destroy (font);
}
int main(int argc, char** argv)
{
if (argc > 1)
num_threads = atoi (argv[1]);
if (argc > 2)
num_repetitions = atoi (argv[2]);
/* Dummy call to alleviate _guess_segment_properties thread safety-ness
* https://github.com/harfbuzz/harfbuzz/issues/1191 */
hb_language_get_default ();
if (argc > 4)
{
num_tests = (argc - 3) / 2;
tests = (test_input_t *) calloc (num_tests, sizeof (test_input_t));
for (unsigned i = 0; i < num_tests; i++)
{
tests[i].is_variable = true;
tests[i].text_path = argv[3 + i * 2];
tests[i].font_path = argv[4 + i * 2];
}
}
printf ("Num threads %u; num repetitions %u\n", num_threads, num_repetitions);
for (unsigned i = 0; i < num_tests; i++)
{
auto& test_input = tests[i];
for (int variable = 0; variable < int (test_input.is_variable) + 1; variable++)
{
bool is_var = (bool) variable;
test_backend (HARFBUZZ, "hb", is_var, test_input);
#ifdef HAVE_FREETYPE
test_backend (FREETYPE, "ft", is_var, test_input);
#endif
}
}
if (tests != default_tests)
free (tests);
}

13
test/threads/meson.build Normal file
View File

@ -0,0 +1,13 @@
test('hb-shape-threads', executable('hb-shape-threads', 'hb-shape-threads.cc',
dependencies: [
freetype_dep, thread_dep
],
cpp_args: [],
include_directories: [incconfig, incsrc],
link_with: [libharfbuzz],
install: false,
),
workdir: meson.current_source_dir() / '..' / '..',
timeout: 300,
suite: ['threads', 'slow'],
)