2 *******************************************************************************
4 * Copyright (C) 2016 and later: Unicode, Inc. and others.
5 * License & terms of use: http://www.unicode.org/copyright.html#License
7 *******************************************************************************
8 *******************************************************************************
10 * Copyright (C) 2009, International Business Machines
11 * Corporation and others. All Rights Reserved.
13 *******************************************************************************
14 * file name: ucnvavailperf.cpp
16 * tab size: 8 (not used)
19 * created on: 2009apr06
20 * created by: Markus W. Scherer
22 * Test performance (time & memory) of ucnv_countAvailable(),
23 * for a before-and-after comparison of
24 * ticket 6441: make ucnv_countAvailable() not fully load converters
26 * Run with one optional command-line argument:
27 * You can specify the path to the ICU data directory.
29 * I built the common (icuuc) library with the following modification,
30 * switching between old (pre-ticket-6441) behavior of actually
31 * trying to load all converters and new behavior of just doing enough
32 * to test availability.
34 * Code in the ucnv_bld.c haveAvailableConverterList() function:
36 // old pre-ticket-6441 behavior
37 ucnv_close(ucnv_createConverter(&tempConverter, converterName, &localStatus));
38 if (U_SUCCESS(localStatus)) {
41 if (ucnv_canCreateConverter(converterName, &localStatus)) {
47 #include "unicode/utypes.h"
48 #include "unicode/putil.h"
49 #include "unicode/uclean.h"
50 #include "unicode/ucnv.h"
51 #include "unicode/utimer.h"
53 static size_t icuMemUsage = 0;
58 my_alloc(const void *context, size_t size) {
59 size_t *p = (size_t *)malloc(size + sizeof(size_t));
70 my_free(const void *context, void *mem) {
72 const size_t *p = (const size_t *)mem - 1;
78 // Not used in the common library.
80 my_realloc(const void *context, void *mem, size_t size) {
81 my_free(context, mem);
87 int main(int argc, const char *argv[]) {
88 UErrorCode errorCode = U_ZERO_ERROR;
90 // Hook in our own memory allocation functions so that we can measure
92 u_setMemoryFunctions(NULL, my_alloc, my_realloc, my_free, &errorCode);
93 if(U_FAILURE(errorCode)) {
95 "u_setMemoryFunctions() failed - %s\n",
96 u_errorName(errorCode));
101 printf("u_setDataDirectory(%s)\n", argv[1]);
102 u_setDataDirectory(argv[1]);
105 // Preload a purely algorithmic converter via an alias,
106 // to make sure that relevant data can be loaded and to set up
107 // caches and such that are needed even if none of the data-driven
108 // converters needs to be loaded.
109 ucnv_close(ucnv_open("ibm-1208", &errorCode));
110 if(U_FAILURE(errorCode)) {
112 "unable to open UTF-8 converter via an alias - %s\n",
113 u_errorName(errorCode));
117 printf("memory usage after ucnv_open(ibm-1208): %lu\n", (long)icuMemUsage);
120 utimer_getTime(&start_time);
121 // Measure the time to find out the list of actually available converters.
122 int32_t count = ucnv_countAvailable();
123 double elapsed = utimer_getElapsedSeconds(&start_time);
124 printf("ucnv_countAvailable() reports that %d converters are available.\n", count);
125 printf("ucnv_countAvailable() took %g seconds to figure this out.\n", elapsed);
126 printf("memory usage after ucnv_countAvailable(): %lu\n", (long)icuMemUsage);
129 printf("memory usage after ucnv_flushCache(): %lu\n", (long)icuMemUsage);
132 printf("memory usage after u_cleanup(): %lu\n", (long)icuMemUsage);