1 /*
2 *******************************************************************************
3 *
4 * © 2016 and later: Unicode, Inc. and others.
5 * License & terms of use: http://www.unicode.org/copyright.html#License
6 *
7 *******************************************************************************
8 *******************************************************************************
9 *
10 * Copyright (C) 2009, International Business Machines
11 * Corporation and others. All Rights Reserved.
12 *
13 *******************************************************************************
14 * file name: ucnvavailperf.cpp
15 * encoding: UTF-8
16 * tab size: 8 (not used)
17 * indentation:4
18 *
19 * created on: 2009apr06
20 * created by: Markus W. Scherer
21 *
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
25 *
26 * Run with one optional command-line argument:
27 * You can specify the path to the ICU data directory.
28 *
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.
33 *
34 * Code in the ucnv_bld.c haveAvailableConverterList() function:
35 #if 0
36 // old pre-ticket-6441 behavior
37 ucnv_close(ucnv_createConverter(&tempConverter, converterName, &localStatus));
38 if (U_SUCCESS(localStatus)) {
39 #else
40 // new behavior
41 if (ucnv_canCreateConverter(converterName, &localStatus)) {
42 #endif
43 */
44
45 #include <malloc.h>
46 #include <stdio.h>
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"
52
53 static size_t icuMemUsage = 0;
54
55 U_CDECL_BEGIN
56
57 void *U_CALLCONV
my_alloc(const void * context,size_t size)58 my_alloc(const void *context, size_t size) {
59 size_t *p = (size_t *)malloc(size + sizeof(size_t));
60 if (p != NULL) {
61 icuMemUsage += size;
62 *p = size;
63 return p + 1;
64 } else {
65 return NULL;
66 }
67 }
68
69 void U_CALLCONV
my_free(const void * context,void * mem)70 my_free(const void *context, void *mem) {
71 if (mem != NULL) {
72 const size_t *p = (const size_t *)mem - 1;
73 icuMemUsage -= *p;
74 free((void *)p);
75 }
76 }
77
78 // Not used in the common library.
79 void *U_CALLCONV
my_realloc(const void * context,void * mem,size_t size)80 my_realloc(const void *context, void *mem, size_t size) {
81 my_free(context, mem);
82 return NULL;
83 }
84
85 U_CDECL_END
86
main(int argc,const char * argv[])87 int main(int argc, const char *argv[]) {
88 UErrorCode errorCode = U_ZERO_ERROR;
89
90 // Hook in our own memory allocation functions so that we can measure
91 // the memory usage.
92 u_setMemoryFunctions(NULL, my_alloc, my_realloc, my_free, &errorCode);
93 if(U_FAILURE(errorCode)) {
94 fprintf(stderr,
95 "u_setMemoryFunctions() failed - %s\n",
96 u_errorName(errorCode));
97 return errorCode;
98 }
99
100 if (argc > 1) {
101 printf("u_setDataDirectory(%s)\n", argv[1]);
102 u_setDataDirectory(argv[1]);
103 }
104
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)) {
111 fprintf(stderr,
112 "unable to open UTF-8 converter via an alias - %s\n",
113 u_errorName(errorCode));
114 return errorCode;
115 }
116
117 printf("memory usage after ucnv_open(ibm-1208): %lu\n", (long)icuMemUsage);
118
119 UTimer start_time;
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);
127
128 ucnv_flushCache();
129 printf("memory usage after ucnv_flushCache(): %lu\n", (long)icuMemUsage);
130
131 u_cleanup();
132 printf("memory usage after u_cleanup(): %lu\n", (long)icuMemUsage);
133
134 return 0;
135 }
136