1/***********************************************************************
2 * © 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html
4 *
5 ***********************************************************************
6 ***********************************************************************
7 * COPYRIGHT:
8 * Copyright (C) 2002-2006 International Business Machines Corporation
9 * and others. All Rights Reserved.
10 *
11 ***********************************************************************/
12/*****************************************************************************
13* File stringperf.cpp
14*
15* Modification History:
16* Name                     Description
17* Doug Wang                Second version
18* Doug Wang                First Version
19******************************************************************************
20*/
21
22/**
23 * This program tests UnicodeString performance.
24 * APIs tested: UnicodeString
25 * ICU4C
26 * Windows 2000/XP, Linux
27 */
28
29#include "stringperf.h"
30
31
32int main(int argc, const char *argv[])
33{
34    UErrorCode status = U_ZERO_ERROR;
35
36    bCatenatePrealloc=true;
37
38    StringPerformanceTest test(argc, argv, status);
39    if (U_FAILURE(status)){
40        return status;
41    }
42
43    int loops = LOOPS;
44    if (bCatenatePrealloc) {
45        int to_alloc = loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN);
46        catICU = new UnicodeString(to_alloc,'a',0);
47        //catICU = new UnicodeString();
48
49        catStd = new stlstring();
50        catStd -> reserve(loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN));
51        //catStd -> reserve(110000000);
52    } else {
53        catICU = new UnicodeString();
54        catStd = new stlstring();
55    }
56
57    if (test.run() == false){
58        fprintf(stderr, "FAILED: Tests could not be run please check the "
59            "arguments.\n");
60        return -1;
61    }
62
63    delete catICU;
64    delete catStd;
65    return 0;
66}
67
68StringPerformanceTest::StringPerformanceTest(int32_t argc, const char *argv[],
69                                             UErrorCode &status)
70                                             : UPerfTest(argc, argv, status)
71{
72    filelines_=NULL;
73    StrBuffer=NULL;
74    StrBufferLen=0;
75
76    int32_t len =0;
77
78    if (status== U_ILLEGAL_ARGUMENT_ERROR){
79        //fprintf(stderr,gUsageString, "stringperf");
80        return;
81    }
82    if (U_FAILURE(status)){
83        fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n",
84            u_errorName(status));
85        return;
86    }
87
88
89    if(line_mode){
90        ULine* filelines = getLines(status);
91        if(U_FAILURE(status)){
92            fprintf(stderr, "FAILED to read lines from file and create UPerfTest object. Error: %s\n", u_errorName(status));
93            return;
94        }
95
96        filelines_ = new ULine[numLines];
97        for (int i =0; i < numLines; i++) {
98            len = filelines[i].len;
99            filelines_[i].name  = new UChar[len];
100            filelines_[i].len   = len;
101            memcpy(filelines_[i].name, filelines[i].name, len * U_SIZEOF_UCHAR);
102        }
103
104    }else if(bulk_mode){
105        int32_t srcLen = 0;
106        const UChar* src = getBuffer(srcLen,status);
107        if(U_FAILURE(status)){
108            fprintf(stderr, "FAILED to read buffer from file and create UPerfTest object. Error: %s\n", u_errorName(status));
109            return;
110        }
111
112        StrBuffer = new UChar[srcLen];
113        StrBufferLen = srcLen;
114        memcpy(StrBuffer, src, srcLen * U_SIZEOF_UCHAR);
115
116    }
117}
118
119StringPerformanceTest::~StringPerformanceTest()
120{
121    delete[] filelines_;
122    delete[] StrBuffer;
123}
124
125UPerfFunction* StringPerformanceTest::runIndexedTest(int32_t index, UBool exec,
126                                                   const char *&name,
127                                                   char* par)
128{
129    switch (index) {
130        TESTCASE(0, TestCtor);
131        TESTCASE(1, TestCtor1);
132        TESTCASE(2, TestCtor2);
133        TESTCASE(3, TestCtor3);
134        TESTCASE(4, TestAssign);
135        TESTCASE(5, TestAssign1);
136        TESTCASE(6, TestAssign2);
137        TESTCASE(7, TestGetch);
138        TESTCASE(8, TestCatenate);
139        TESTCASE(9, TestScan);
140        TESTCASE(10, TestScan1);
141        TESTCASE(11, TestScan2);
142
143        TESTCASE(12, TestStdLibCtor);
144        TESTCASE(13, TestStdLibCtor1);
145        TESTCASE(14, TestStdLibCtor2);
146        TESTCASE(15, TestStdLibCtor3);
147        TESTCASE(16, TestStdLibAssign);
148        TESTCASE(17, TestStdLibAssign1);
149        TESTCASE(18, TestStdLibAssign2);
150        TESTCASE(19, TestStdLibGetch);
151        TESTCASE(20, TestStdLibCatenate);
152        TESTCASE(21, TestStdLibScan);
153        TESTCASE(22, TestStdLibScan1);
154        TESTCASE(23, TestStdLibScan2);
155
156        default:
157            name = "";
158            return NULL;
159    }
160    return NULL;
161}
162
163UPerfFunction* StringPerformanceTest::TestCtor()
164{
165    if (line_mode) {
166        return new StringPerfFunction(ctor, filelines_, numLines, uselen);
167    } else {
168        return new StringPerfFunction(ctor, StrBuffer, StrBufferLen, uselen);
169    }
170}
171
172UPerfFunction* StringPerformanceTest::TestCtor1()
173{
174    if (line_mode) {
175        return new StringPerfFunction(ctor1, filelines_, numLines, uselen);
176    } else {
177        return new StringPerfFunction(ctor1, StrBuffer, StrBufferLen, uselen);
178    }
179}
180
181UPerfFunction* StringPerformanceTest::TestCtor2()
182{
183    if (line_mode) {
184        return new StringPerfFunction(ctor2, filelines_, numLines, uselen);
185    } else {
186        return new StringPerfFunction(ctor2, StrBuffer, StrBufferLen, uselen);
187    }
188}
189
190UPerfFunction* StringPerformanceTest::TestCtor3()
191{
192    if (line_mode) {
193        return new StringPerfFunction(ctor3, filelines_, numLines, uselen);
194    } else {
195        return new StringPerfFunction(ctor3, StrBuffer, StrBufferLen, uselen);
196    }
197}
198
199UPerfFunction* StringPerformanceTest::TestAssign()
200{
201    if (line_mode) {
202        return new StringPerfFunction(assign, filelines_, numLines, uselen);
203    } else {
204        return new StringPerfFunction(assign, StrBuffer, StrBufferLen, uselen);
205    }
206}
207
208UPerfFunction* StringPerformanceTest::TestAssign1()
209{
210    if (line_mode) {
211        return new StringPerfFunction(assign1, filelines_, numLines, uselen);
212    } else {
213        return new StringPerfFunction(assign1, StrBuffer, StrBufferLen, uselen);
214    }
215}
216
217UPerfFunction* StringPerformanceTest::TestAssign2()
218{
219    if (line_mode) {
220        return new StringPerfFunction(assign2, filelines_, numLines, uselen);
221    } else {
222        return new StringPerfFunction(assign2, StrBuffer, StrBufferLen, uselen);
223    }
224}
225
226
227UPerfFunction* StringPerformanceTest::TestGetch()
228{
229    if (line_mode) {
230        return new StringPerfFunction(getch, filelines_, numLines, uselen);
231    } else {
232        return new StringPerfFunction(getch, StrBuffer, StrBufferLen, uselen);
233    }
234}
235
236UPerfFunction* StringPerformanceTest::TestCatenate()
237{
238    if (line_mode) {
239        return new StringPerfFunction(catenate, filelines_, numLines, uselen);
240    } else {
241        //return new StringPerfFunction(catenate, buffer, bufferLen, uselen);
242        return new StringPerfFunction(catenate, StrBuffer, StrBufferLen, uselen);
243    }
244}
245
246UPerfFunction* StringPerformanceTest::TestScan()
247{
248    if (line_mode) {
249        return new StringPerfFunction(scan, filelines_, numLines, uselen);
250    } else {
251        return new StringPerfFunction(scan, StrBuffer, StrBufferLen, uselen);
252    }
253}
254
255UPerfFunction* StringPerformanceTest::TestScan1()
256{
257    if (line_mode) {
258        return new StringPerfFunction(scan1, filelines_, numLines, uselen);
259    } else {
260        return new StringPerfFunction(scan1, StrBuffer, StrBufferLen, uselen);
261    }
262}
263
264UPerfFunction* StringPerformanceTest::TestScan2()
265{
266    if (line_mode) {
267        return new StringPerfFunction(scan2, filelines_, numLines, uselen);
268    } else {
269        return new StringPerfFunction(scan2, StrBuffer, StrBufferLen, uselen);
270    }
271}
272
273UPerfFunction* StringPerformanceTest::TestStdLibCtor()
274{
275    if (line_mode) {
276        return new StringPerfFunction(StdLibCtor, filelines_, numLines, uselen);
277    } else {
278        return new StringPerfFunction(StdLibCtor, StrBuffer, StrBufferLen, uselen);
279    }
280}
281
282UPerfFunction* StringPerformanceTest::TestStdLibCtor1()
283{
284    if (line_mode) {
285        return new StringPerfFunction(StdLibCtor1, filelines_, numLines, uselen);
286    } else {
287        return new StringPerfFunction(StdLibCtor1, StrBuffer, StrBufferLen, uselen);
288    }
289}
290
291UPerfFunction* StringPerformanceTest::TestStdLibCtor2()
292{
293    if (line_mode) {
294        return new StringPerfFunction(StdLibCtor2, filelines_, numLines, uselen);
295    } else {
296        return new StringPerfFunction(StdLibCtor2, StrBuffer, StrBufferLen, uselen);
297    }
298}
299
300UPerfFunction* StringPerformanceTest::TestStdLibCtor3()
301{
302    if (line_mode) {
303        return new StringPerfFunction(StdLibCtor3, filelines_, numLines, uselen);
304    } else {
305        return new StringPerfFunction(StdLibCtor3, StrBuffer, StrBufferLen, uselen);
306    }
307}
308
309UPerfFunction* StringPerformanceTest::TestStdLibAssign()
310{
311    if (line_mode) {
312        return new StringPerfFunction(StdLibAssign, filelines_, numLines, uselen);
313    } else {
314        return new StringPerfFunction(StdLibAssign, StrBuffer, StrBufferLen, uselen);
315    }
316}
317
318UPerfFunction* StringPerformanceTest::TestStdLibAssign1()
319{
320    if (line_mode) {
321        return new StringPerfFunction(StdLibAssign1, filelines_, numLines, uselen);
322    } else {
323        return new StringPerfFunction(StdLibAssign1, StrBuffer, StrBufferLen, uselen);
324    }
325}
326
327UPerfFunction* StringPerformanceTest::TestStdLibAssign2()
328{
329    if (line_mode) {
330        return new StringPerfFunction(StdLibAssign2, filelines_, numLines, uselen);
331    } else {
332        return new StringPerfFunction(StdLibAssign2, StrBuffer, StrBufferLen, uselen);
333    }
334}
335
336UPerfFunction* StringPerformanceTest::TestStdLibGetch()
337{
338    if (line_mode) {
339        return new StringPerfFunction(StdLibGetch, filelines_, numLines, uselen);
340    } else {
341        return new StringPerfFunction(StdLibGetch, StrBuffer, StrBufferLen, uselen);
342    }
343}
344
345UPerfFunction* StringPerformanceTest::TestStdLibCatenate()
346{
347    if (line_mode) {
348        return new StringPerfFunction(StdLibCatenate, filelines_, numLines, uselen);
349    } else {
350        //return new StringPerfFunction(StdLibCatenate, buffer, bufferLen, uselen);
351        return new StringPerfFunction(StdLibCatenate, StrBuffer, StrBufferLen, uselen);
352    }
353}
354
355UPerfFunction* StringPerformanceTest::TestStdLibScan()
356{
357    if (line_mode) {
358        return new StringPerfFunction(StdLibScan, filelines_, numLines, uselen);
359    } else {
360        return new StringPerfFunction(StdLibScan, StrBuffer, StrBufferLen, uselen);
361    }
362}
363
364UPerfFunction* StringPerformanceTest::TestStdLibScan1()
365{
366    if (line_mode) {
367        return new StringPerfFunction(StdLibScan1, filelines_, numLines, uselen);
368    } else {
369        return new StringPerfFunction(StdLibScan1, StrBuffer, StrBufferLen, uselen);
370    }
371}
372
373UPerfFunction* StringPerformanceTest::TestStdLibScan2()
374{
375    if (line_mode) {
376        return new StringPerfFunction(StdLibScan2, filelines_, numLines, uselen);
377    } else {
378        return new StringPerfFunction(StdLibScan2, StrBuffer, StrBufferLen, uselen);
379    }
380}
381
382
383