Imported Upstream version 58.1
[platform/upstream/icu.git] / source / test / perf / leperf / leperf.cpp
1 /**************************************************************************
2 *
3 *   Copyright (C) 2016 and later: Unicode, Inc. and others.
4 *   License & terms of use: http://www.unicode.org/copyright.html#License
5 *
6 ***************************************************************************
7 ***************************************************************************
8 *
9 *   Copyright (C) 2013, International Business Machines
10 *   Corporation and others.  All Rights Reserved.
11 *
12 ************************************************************************/
13
14 #include "unicode/utimer.h"
15 #include "unicode/ustdio.h"
16 #include "layout/LETypes.h"
17 #include "layout/LayoutEngine.h"
18 #include "layout/LEScripts.h"
19 #include "SimpleFontInstance.h"
20 #include "PortableFontInstance.h"
21
22 class Params {
23 public:
24   LEFontInstance *font;
25   LEUnicode *chars;
26   le_int32 charLen;
27   ScriptCodes script;
28   le_int32 glyphCount;
29 };
30
31 LEUnicode     ArabChars[] = {
32         0x0045, 0x006E, 0x0067, 0x006C, 0x0069, 0x0073, 0x0068, 0x0020, // "English "
33         0x0645, 0x0627, 0x0646, 0x062A, 0x0648, 0x0634,                 // MEM ALIF KAF NOON TEH WAW SHEEN
34         0x0020, 0x0074, 0x0065, 0x0078, 0x0074, 0x02E, 0                   // " text."
35     };
36
37 void iterate(void * p) {
38   Params* params = (Params*) p;
39
40     LEErrorCode status = LE_NO_ERROR;
41     LEFontInstance *font = params->font;
42     LayoutEngine *engine = LayoutEngine::layoutEngineFactory(font, params->script, -1, status);
43     LEGlyphID *glyphs    = NULL;
44     le_int32  *indices   = NULL;
45     float     *positions = NULL;
46     le_int32   glyphCount = 0;
47     LEUnicode *chars = params->chars;
48     glyphCount = engine->layoutChars(chars, 0, params->charLen, params->charLen, TRUE, 0.0, 0.0, status);
49     glyphs    = LE_NEW_ARRAY(LEGlyphID, glyphCount + 10);
50     indices   = LE_NEW_ARRAY(le_int32, glyphCount + 10);
51     positions = LE_NEW_ARRAY(float, glyphCount + 10);
52     engine->getGlyphs(glyphs, status);
53     params->glyphCount = glyphCount;
54     
55     
56     delete glyphs;
57     delete indices;
58     delete positions;
59     delete engine;
60     //delete font;
61 }
62
63 int main(int argc, const char *argv[]) {
64   double len=10.0;
65   for(int i=1;i<argc;i++) {
66     puts("arg:");
67     puts(argv[i]);
68     if(argv[i][0]=='p') {
69       printf("hit enter-pid=%d", getpid());
70       getchar();
71     } else if(argv[i][0]>='0' && argv[i][0]<='9') {
72       len = (1.0)*(argv[i][0]-'0');
73     }
74   }
75   u_printf("leperf: Testing %s for %.fs...\n", U_ICU_VERSION, len);
76   LEErrorCode status = LE_NO_ERROR;
77   //uloc_setDefault("en_US", &status);
78   Params p;
79
80 #if 0  
81   p.script = arabScriptCode;
82   p.chars = ArabChars;
83   p.charLen = sizeof(ArabChars)/sizeof(ArabChars[0]);
84 #else
85   p.script = latnScriptCode;
86   p.chars = new LEUnicode[257];
87   for(int i=0;i<256;i++) {
88     p.chars[i] = i+1;
89   }
90   p.chars[256] = 0;
91   p.charLen = 256;
92 #endif
93   
94   int32_t loopCount;
95   double timeTaken;
96   double timeNs;
97 #if 0
98   p.font = new SimpleFontInstance(12, status);
99   u_printf("leperf: Running SFI...\r");
100   timeTaken = utimer_loopUntilDone(len, &loopCount, iterate, &p);
101   u_printf("leperf: SFI .. took %.fs %.2fns/ea\nleperf: .. iter= %d\n", timeTaken, 1000000000.0*(timeTaken/(double)loopCount), (int32_t)loopCount);
102   delete p.font;
103 #endif
104     PortableFontInstance *font;
105     LEErrorCode fontStatus = LE_NO_ERROR;
106     const char *fontPath = "myfont.ttf";
107
108     font = new PortableFontInstance(fontPath, 12, fontStatus);
109
110     p.font = font;
111     loopCount=0;
112     u_printf("leperf: testing %s\n", fontPath);
113     u_printf("leperf: Running ...\r");
114   timeTaken = utimer_loopUntilDone(len, &loopCount, iterate, &p);
115   timeNs = 1000000000.0*(timeTaken/(double)loopCount);
116   u_printf("leperf: PFI .. took %.fs %.2fns/ea\nleperf: .. iter= %d\n", timeTaken, timeNs, (int32_t)loopCount);
117   u_printf("leperf: DATA|\"%s\"|%.2f|\n", U_ICU_VERSION, timeNs);
118   u_printf("leperf: glyphs=%d\n", p.glyphCount);
119   return 0;
120 }
121
122 // hack - #include these for easier build.
123 #include "SimpleFontInstance.cpp"
124 #include "PortableFontInstance.cpp"
125 #include "cmaps.cpp"
126 #include "FontTableCache.cpp"