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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/*
**************************************************************************
* © 2016 and later: Unicode, Inc. and others.
* License & terms of use: http://www.unicode.org/copyright.html
*************************************************************************
*************************************************************************
* Copyright (C) 2002-2014, International Business Machines
* Corporation and others. All Rights Reserved.
*************************************************************************
* file name: utfperf.cpp
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2005Nov17
* created by: Raymond Yang
*
* Ported from utfper.c created by Markus W. Scherer
* Performance test program for Unicode converters
*/
#include <stdio.h>
#include <stdlib.h>
#include "unicode/uperf.h"
#include "cmemory.h" // for UPRV_LENGTHOF
#include "uoptions.h"
/* definitions and text buffers */
#define INPUT_CAPACITY (1024*1024)
#define INTERMEDIATE_CAPACITY 4096
#define INTERMEDIATE_SMALL_CAPACITY 20
#define PIVOT_CAPACITY 1024
#define OUTPUT_CAPACITY INPUT_CAPACITY
static char utf8[INPUT_CAPACITY];
static char16_t pivot[INTERMEDIATE_CAPACITY];
static char16_t output[OUTPUT_CAPACITY];
static char intermediate[OUTPUT_CAPACITY];
static int32_t utf8Length, encodedLength, outputLength, countInputCodePoints;
static int32_t fromUCallbackCount;
// Command-line options specific to utfperf.
// Options do not have abbreviations: Force readable command lines.
// (Using U+0001 for abbreviation characters.)
enum {
CHARSET,
CHUNK_LENGTH,
PIVOT_LENGTH,
UTFPERF_OPTIONS_COUNT
};
static UOption options[UTFPERF_OPTIONS_COUNT]={
UOPTION_DEF("charset", '\x01', UOPT_REQUIRES_ARG),
UOPTION_DEF("chunk", '\x01', UOPT_REQUIRES_ARG),
UOPTION_DEF("pivot", '\x01', UOPT_REQUIRES_ARG)
};
static const char *const utfperf_usage =
"\t--charset Charset for which to test performance, e.g. windows-1251.\n"
"\t Default: UTF-8\n"
"\t--chunk Length (in bytes) of charset output chunks. [4096]\n"
"\t--pivot Length (in UChars) of the UTF-16 pivot buffer, if applicable.\n"
"\t [1024]\n";
// Test object.
class UtfPerformanceTest : public UPerfTest{
public:
UtfPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status)
: UPerfTest(argc, argv, options, UPRV_LENGTHOF(options), utfperf_usage, status) {
if (U_SUCCESS(status)) {
charset = options[CHARSET].value;
chunkLength = atoi(options[CHUNK_LENGTH].value);
if (chunkLength < 1 || OUTPUT_CAPACITY < chunkLength) {
fprintf(stderr, "error: chunk length must be 1..%ld\n", static_cast<long>(OUTPUT_CAPACITY));
status = U_ILLEGAL_ARGUMENT_ERROR;
}
pivotLength = atoi(options[PIVOT_LENGTH].value);
if (pivotLength < 1 || PIVOT_CAPACITY < pivotLength) {
fprintf(stderr, "error: pivot length must be 1..%ld\n", static_cast<long>(PIVOT_CAPACITY));
status = U_ILLEGAL_ARGUMENT_ERROR;
}
int32_t inputLength;
UPerfTest::getBuffer(inputLength, status);
countInputCodePoints = u_countChar32(buffer, bufferLen);
u_strToUTF8(utf8, static_cast<int32_t>(sizeof(utf8)), &utf8Length, buffer, bufferLen, &status);
}
}
UPerfFunction* runIndexedTest(int32_t index, UBool exec, const char*& name, char* par = nullptr) override;
const char16_t *getBuffer() const { return buffer; }
int32_t getBufferLen() const { return bufferLen; }
const char *charset;
int32_t chunkLength, pivotLength;
};
U_CDECL_BEGIN
// Custom callback for counting callback calls.
static void U_CALLCONV
fromUCallback(const void *context,
UConverterFromUnicodeArgs *fromUArgs,
const char16_t *codeUnits,
int32_t length,
UChar32 codePoint,
UConverterCallbackReason reason,
UErrorCode *pErrorCode) {
if (reason <= UCNV_IRREGULAR) {
++fromUCallbackCount;
}
UCNV_FROM_U_CALLBACK_SUBSTITUTE(context, fromUArgs, codeUnits, length, codePoint, reason, pErrorCode);
}
U_CDECL_END
// Base class for Roundtrip, FromUnicode and FromUTF8 with common setup.
class Command : public UPerfFunction {
protected:
Command(const UtfPerformanceTest &testcase)
: testcase(testcase),
input(testcase.getBuffer()), inputLength(testcase.getBufferLen()),
errorCode(U_ZERO_ERROR) {
cnv=ucnv_open(testcase.charset, &errorCode);
if (U_FAILURE(errorCode)) {
fprintf(stderr, "error opening converter for \"%s\" - %s\n", testcase.charset, u_errorName(errorCode));
}
ucnv_setFromUCallBack(cnv, fromUCallback, nullptr, nullptr, nullptr, &errorCode);
}
public:
virtual ~Command(){
if(U_SUCCESS(errorCode)) {
ucnv_close(cnv);
}
}
// virtual void call(UErrorCode* pErrorCode) { ... }
long getOperationsPerIteration() override {
return countInputCodePoints;
}
const UtfPerformanceTest &testcase;
const char16_t *input;
int32_t inputLength;
UErrorCode errorCode;
UConverter *cnv;
};
// Test roundtrip UTF-16->encoding->UTF-16.
class Roundtrip : public Command {
protected:
Roundtrip(const UtfPerformanceTest &testcase) : Command(testcase) {}
public:
static UPerfFunction* get(const UtfPerformanceTest &testcase) {
Roundtrip * t = new Roundtrip(testcase);
if (U_SUCCESS(t->errorCode)){
return t;
} else {
delete t;
return nullptr;
}
}
void call(UErrorCode* pErrorCode) override {
const char16_t *pIn, *pInLimit;
char16_t *pOut, *pOutLimit;
char *pInter, *pInterLimit;
const char *p;
UBool flush;
ucnv_reset(cnv);
fromUCallbackCount=0;
pIn=input;
pInLimit=input+inputLength;
pOut=output;
pOutLimit=output+OUTPUT_CAPACITY;
pInterLimit=intermediate+testcase.chunkLength;
encodedLength=outputLength=0;
flush=false;
do {
/* convert a block of [pIn..pInLimit[ to the encoding in intermediate[] */
pInter=intermediate;
ucnv_fromUnicode(cnv, &pInter, pInterLimit, &pIn, pInLimit, nullptr, true, pErrorCode);
encodedLength += static_cast<int32_t>(pInter - intermediate);
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
/* make sure that we convert once more to really flush */
*pErrorCode=U_ZERO_ERROR;
} else if(U_FAILURE(*pErrorCode)) {
return;
} else if(pIn==pInLimit) {
flush=true;
}
/* convert the block [intermediate..pInter[ back to UTF-16 */
p=intermediate;
ucnv_toUnicode(cnv, &pOut, pOutLimit,&p, pInter,nullptr, flush,pErrorCode);
if(U_FAILURE(*pErrorCode)) {
return;
}
/* intermediate must have been consumed (p==pInter) because of the converter semantics */
} while(!flush);
outputLength=pOut-output;
if(inputLength!=outputLength) {
fprintf(stderr, "error: roundtrip failed, inputLength %d!=outputLength %d\n", inputLength, outputLength);
*pErrorCode=U_INTERNAL_PROGRAM_ERROR;
}
}
};
// Test one-way conversion UTF-16->encoding.
class FromUnicode : public Command {
protected:
FromUnicode(const UtfPerformanceTest &testcase) : Command(testcase) {}
public:
static UPerfFunction* get(const UtfPerformanceTest &testcase) {
FromUnicode * t = new FromUnicode(testcase);
if (U_SUCCESS(t->errorCode)){
return t;
} else {
delete t;
return nullptr;
}
}
void call(UErrorCode* pErrorCode) override {
const char16_t *pIn, *pInLimit;
char *pInter, *pInterLimit;
ucnv_resetFromUnicode(cnv);
fromUCallbackCount=0;
pIn=input;
pInLimit=input+inputLength;
pInterLimit=intermediate+testcase.chunkLength;
encodedLength=0;
for(;;) {
pInter=intermediate;
ucnv_fromUnicode(cnv, &pInter, pInterLimit, &pIn, pInLimit, nullptr, true, pErrorCode);
encodedLength += static_cast<int32_t>(pInter - intermediate);
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
/* make sure that we convert once more to really flush */
*pErrorCode=U_ZERO_ERROR;
} else if(U_FAILURE(*pErrorCode)) {
return;
} else {
break; // all done
}
}
}
};
// Test one-way conversion UTF-8->encoding.
class FromUTF8 : public Command {
protected:
FromUTF8(const UtfPerformanceTest &testcase)
: Command(testcase),
utf8Cnv(nullptr),
input8(utf8), input8Length(utf8Length) {
utf8Cnv=ucnv_open("UTF-8", &errorCode);
}
public:
static UPerfFunction* get(const UtfPerformanceTest &testcase) {
FromUTF8 * t = new FromUTF8(testcase);
if (U_SUCCESS(t->errorCode)){
return t;
} else {
delete t;
return nullptr;
}
}
~FromUTF8() {
ucnv_close(utf8Cnv);
}
void call(UErrorCode* pErrorCode) override {
const char *pIn, *pInLimit;
char *pInter, *pInterLimit;
char16_t *pivotSource, *pivotTarget, *pivotLimit;
ucnv_resetToUnicode(utf8Cnv);
ucnv_resetFromUnicode(cnv);
fromUCallbackCount=0;
pIn=input8;
pInLimit=input8+input8Length;
pInterLimit=intermediate+testcase.chunkLength;
pivotSource=pivotTarget=pivot;
pivotLimit=pivot+testcase.pivotLength;
encodedLength=0;
for(;;) {
pInter=intermediate;
ucnv_convertEx(cnv, utf8Cnv,
&pInter, pInterLimit,
&pIn, pInLimit,
pivot, &pivotSource, &pivotTarget, pivotLimit,
false, true, pErrorCode);
encodedLength += static_cast<int32_t>(pInter - intermediate);
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
/* make sure that we convert once more to really flush */
*pErrorCode=U_ZERO_ERROR;
} else if(U_FAILURE(*pErrorCode)) {
return;
} else {
break; // all done
}
}
}
protected:
UConverter *utf8Cnv;
const char *input8;
int32_t input8Length;
};
UPerfFunction* UtfPerformanceTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* par) {
switch (index) {
case 0: name = "Roundtrip"; if (exec) return Roundtrip::get(*this); break;
case 1: name = "FromUnicode"; if (exec) return FromUnicode::get(*this); break;
case 2: name = "FromUTF8"; if (exec) return FromUTF8::get(*this); break;
default: name = ""; break;
}
return nullptr;
}
int main(int argc, const char *argv[])
{
// Default values for command-line options.
options[CHARSET].value = "UTF-8";
options[CHUNK_LENGTH].value = "4096";
options[PIVOT_LENGTH].value = "1024";
UErrorCode status = U_ZERO_ERROR;
UtfPerformanceTest test(argc, argv, status);
if (U_FAILURE(status)){
printf("The error is %s\n", u_errorName(status));
test.usage();
return status;
}
if (test.run() == false){
fprintf(stderr, "FAILED: Tests could not be run please check the "
"arguments.\n");
return -1;
}
if (fromUCallbackCount > 0) {
printf("Number of fromUnicode callback calls in the last iteration: %ld\n", static_cast<long>(fromUCallbackCount));
}
return 0;
}
|