File: ucnvavailperf.cpp

package info (click to toggle)
icu 78.2-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 123,988 kB
  • sloc: cpp: 527,891; ansic: 112,789; sh: 4,983; makefile: 4,657; perl: 3,199; python: 2,933; xml: 749; sed: 36; lisp: 12
file content (135 lines) | stat: -rw-r--r-- 4,339 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
*******************************************************************************
*
*   © 2016 and later: Unicode, Inc. and others.
*   License & terms of use: http://www.unicode.org/copyright.html
*
*******************************************************************************
*******************************************************************************
*
*   Copyright (C) 2009, International Business Machines
*   Corporation and others.  All Rights Reserved.
*
*******************************************************************************
*   file name:  ucnvavailperf.cpp
*   encoding:   UTF-8
*   tab size:   8 (not used)
*   indentation:4
*
*   created on: 2009apr06
*   created by: Markus W. Scherer
*
*   Test performance (time & memory) of ucnv_countAvailable(),
*   for a before-and-after comparison of
*   ticket 6441: make ucnv_countAvailable() not fully load converters
*
*   Run with one optional command-line argument:
*   You can specify the path to the ICU data directory.
*
*   I built the common (icuuc) library with the following modification,
*   switching between old (pre-ticket-6441) behavior of actually
*   trying to load all converters and new behavior of just doing enough
*   to test availability.
*
*   Code in the ucnv_bld.c haveAvailableConverterList() function:
#if 0
            // old pre-ticket-6441 behavior
            ucnv_close(ucnv_createConverter(&tempConverter, converterName, &localStatus));
            if (U_SUCCESS(localStatus)) {
#else
            // new behavior
            if (ucnv_canCreateConverter(converterName, &localStatus)) {
#endif
*/

#include <malloc.h>
#include <stdio.h>
#include "unicode/utypes.h"
#include "unicode/putil.h"
#include "unicode/uclean.h"
#include "unicode/ucnv.h"
#include "unicode/utimer.h"

static size_t icuMemUsage = 0;

U_CDECL_BEGIN

void *U_CALLCONV
my_alloc(const void *context, size_t size) {
    size_t *p = (size_t *)malloc(size + sizeof(size_t));
    if (p != nullptr) {
        icuMemUsage += size;
        *p = size;
        return p + 1;
    } else {
        return nullptr;
    }
}

void U_CALLCONV
my_free(const void *context, void *mem) {
    if (mem != nullptr) {
        const size_t *p = (const size_t *)mem - 1;
        icuMemUsage -= *p;
        free((void *)p);
    }
}

// Not used in the common library.
void *U_CALLCONV
my_realloc(const void *context, void *mem, size_t size) {
    my_free(context, mem);
    return nullptr;
}

U_CDECL_END

int main(int argc, const char *argv[]) {
    UErrorCode errorCode = U_ZERO_ERROR;

    // Hook in our own memory allocation functions so that we can measure
    // the memory usage.
    u_setMemoryFunctions(nullptr, my_alloc, my_realloc, my_free, &errorCode);
    if(U_FAILURE(errorCode)) {
        fprintf(stderr,
                "u_setMemoryFunctions() failed - %s\n",
                u_errorName(errorCode));
        return errorCode;
    }

    if (argc > 1) {
        printf("u_setDataDirectory(%s)\n", argv[1]);
        u_setDataDirectory(argv[1]);
    }

    // Preload a purely algorithmic converter via an alias,
    // to make sure that relevant data can be loaded and to set up
    // caches and such that are needed even if none of the data-driven
    // converters needs to be loaded.
    ucnv_close(ucnv_open("ibm-1208", &errorCode));
    if(U_FAILURE(errorCode)) {
        fprintf(stderr,
                "unable to open UTF-8 converter via an alias - %s\n",
                u_errorName(errorCode));
        return errorCode;
    }

    printf("memory usage after ucnv_open(ibm-1208): %lu\n", static_cast<long>(icuMemUsage));

    UTimer start_time;
    utimer_getTime(&start_time);
    // Measure the time to find out the list of actually available converters.
    int32_t count = ucnv_countAvailable();
    double elapsed = utimer_getElapsedSeconds(&start_time);
    printf("ucnv_countAvailable() reports that %d converters are available.\n", count);
    printf("ucnv_countAvailable() took %g seconds to figure this out.\n", elapsed);
    printf("memory usage after ucnv_countAvailable(): %lu\n", static_cast<long>(icuMemUsage));

    ucnv_flushCache();
    printf("memory usage after ucnv_flushCache(): %lu\n", static_cast<long>(icuMemUsage));

    u_cleanup();
    printf("memory usage after u_cleanup(): %lu\n", static_cast<long>(icuMemUsage));

    return 0;
}