]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/ucnvavailperf/ucnvavailperf.cpp
2 *******************************************************************************
4 * Copyright (C) 2009, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: ucnvavailperf.cpp
10 * tab size: 8 (not used)
13 * created on: 2009apr06
14 * created by: Markus W. Scherer
16 * Test performance (time & memory) of ucnv_countAvailable(),
17 * for a before-and-after comparison of
18 * ticket 6441: make ucnv_countAvailable() not fully load converters
20 * Run with one optional command-line argument:
21 * You can specify the path to the ICU data directory.
23 * I built the common (icuuc) library with the following modification,
24 * switching between old (pre-ticket-6441) behavior of actually
25 * trying to load all converters and new behavior of just doing enough
26 * to test availability.
28 * Code in the ucnv_bld.c haveAvailableConverterList() function:
30 // old pre-ticket-6441 behavior
31 ucnv_close(ucnv_createConverter(&tempConverter, converterName, &localStatus));
32 if (U_SUCCESS(localStatus)) {
35 if (ucnv_canCreateConverter(converterName, &localStatus)) {
41 #include "unicode/utypes.h"
42 #include "unicode/putil.h"
43 #include "unicode/uclean.h"
44 #include "unicode/ucnv.h"
45 #include "unicode/utimer.h"
47 static size_t icuMemUsage
= 0;
52 my_alloc(const void *context
, size_t size
) {
53 size_t *p
= (size_t *)malloc(size
+ sizeof(size_t));
64 my_free(const void *context
, void *mem
) {
66 const size_t *p
= (const size_t *)mem
- 1;
72 // Not used in the common library.
74 my_realloc(const void *context
, void *mem
, size_t size
) {
75 my_free(context
, mem
);
81 int main(int argc
, const char *argv
[]) {
82 UErrorCode errorCode
= U_ZERO_ERROR
;
84 // Hook in our own memory allocation functions so that we can measure
86 u_setMemoryFunctions(NULL
, my_alloc
, my_realloc
, my_free
, &errorCode
);
87 if(U_FAILURE(errorCode
)) {
89 "u_setMemoryFunctions() failed - %s\n",
90 u_errorName(errorCode
));
95 printf("u_setDataDirectory(%s)\n", argv
[1]);
96 u_setDataDirectory(argv
[1]);
99 // Preload a purely algorithmic converter via an alias,
100 // to make sure that relevant data can be loaded and to set up
101 // caches and such that are needed even if none of the data-driven
102 // converters needs to be loaded.
103 ucnv_close(ucnv_open("ibm-1208", &errorCode
));
104 if(U_FAILURE(errorCode
)) {
106 "unable to open UTF-8 converter via an alias - %s\n",
107 u_errorName(errorCode
));
111 printf("memory usage after ucnv_open(ibm-1208): %lu\n", (long)icuMemUsage
);
114 utimer_getTime(&start_time
);
115 // Measure the time to find out the list of actually available converters.
116 int32_t count
= ucnv_countAvailable();
117 double elapsed
= utimer_getElapsedSeconds(&start_time
);
118 printf("ucnv_countAvailable() reports that %d converters are available.\n", count
);
119 printf("ucnv_countAvailable() took %g seconds to figure this out.\n", elapsed
);
120 printf("memory usage after ucnv_countAvailable(): %lu\n", (long)icuMemUsage
);
123 printf("memory usage after ucnv_flushCache(): %lu\n", (long)icuMemUsage
);
126 printf("memory usage after u_cleanup(): %lu\n", (long)icuMemUsage
);