2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 using Tizen.NUI.BaseComponents;
21 using Tizen.NUI.Constants;
24 class PerformanceTestExample : NUIApplication
26 const uint ROWS_COUNT = 32;
27 const uint COLUMNS_COUNT = 32;
28 const uint TOTAL_COLUMNS_COUNT = 80;
29 const uint DURATION_PER_COLUMNS = 50; // miliseconds
30 const float VIEW_MARGIN_RATE = 0.1f;
32 TEST_TYPE_COLOR = 0, ///< Test with simple color
33 TEST_TYPE_IMAGE, ///< Test with simple image
34 TEST_TYPE_TEXT, ///< Test with simple text label
35 TEST_TYPE_ROUNDED_COLOR, ///< Test with rounded color
36 TEST_TYPE_BORDER_COLOR, ///< Test with borderline color
37 TEST_TYPE_ROUNDED_BORDER_COLOR, ///< Test with rounded borderline color
38 TEST_TYPE_BLUR_COLOR, ///< Test with blur color
39 TEST_TYPE_ROUNDED_BLUR_COLOR, ///< Test with blur color
43 static string TestTypeString(ViewTestType type)
47 case ViewTestType.TEST_TYPE_COLOR: return "COLOR";
48 case ViewTestType.TEST_TYPE_IMAGE: return "IMAGE";
49 case ViewTestType.TEST_TYPE_TEXT: return "TEXT";
50 case ViewTestType.TEST_TYPE_ROUNDED_COLOR: return "ROUNDED COLOR";
51 case ViewTestType.TEST_TYPE_BORDER_COLOR: return "BORDER COLOR";
52 case ViewTestType.TEST_TYPE_ROUNDED_BORDER_COLOR:return "ROUNDED BORDER COLOR";
53 case ViewTestType.TEST_TYPE_BLUR_COLOR: return "BLUR COLOR";
54 case ViewTestType.TEST_TYPE_ROUNDED_BLUR_COLOR: return "ROUNDED BLUR COLOR";
55 default: return "UNKNOWN";
59 static string IMAGE_DIR = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "image/";
60 readonly static string[] IMAGE_PATH = {
61 IMAGE_DIR + "gallery-medium-1.jpg",
62 IMAGE_DIR + "gallery-medium-2.jpg",
63 IMAGE_DIR + "gallery-medium-3.jpg",
64 IMAGE_DIR + "gallery-medium-4.jpg",
65 IMAGE_DIR + "gallery-medium-5.jpg",
66 IMAGE_DIR + "gallery-medium-6.jpg",
67 IMAGE_DIR + "gallery-medium-7.jpg",
68 IMAGE_DIR + "gallery-medium-8.jpg",
69 IMAGE_DIR + "gallery-medium-9.jpg",
70 IMAGE_DIR + "gallery-medium-10.jpg",
71 IMAGE_DIR + "gallery-medium-11.jpg",
72 IMAGE_DIR + "gallery-medium-12.jpg",
73 IMAGE_DIR + "gallery-medium-13.jpg",
74 IMAGE_DIR + "gallery-medium-14.jpg",
75 IMAGE_DIR + "gallery-medium-15.jpg",
76 IMAGE_DIR + "gallery-medium-16.jpg",
77 IMAGE_DIR + "gallery-medium-17.jpg",
78 IMAGE_DIR + "gallery-medium-18.jpg",
79 IMAGE_DIR + "gallery-medium-19.jpg",
80 IMAGE_DIR + "gallery-medium-20.jpg",
81 IMAGE_DIR + "gallery-medium-21.jpg",
82 IMAGE_DIR + "gallery-medium-22.jpg",
83 IMAGE_DIR + "gallery-medium-23.jpg",
84 IMAGE_DIR + "gallery-medium-24.jpg",
85 IMAGE_DIR + "gallery-medium-25.jpg",
86 IMAGE_DIR + "gallery-medium-26.jpg",
87 IMAGE_DIR + "gallery-medium-27.jpg",
88 IMAGE_DIR + "gallery-medium-28.jpg",
89 IMAGE_DIR + "gallery-medium-29.jpg",
90 IMAGE_DIR + "gallery-medium-30.jpg",
91 IMAGE_DIR + "gallery-medium-31.jpg",
92 IMAGE_DIR + "gallery-medium-32.jpg",
93 IMAGE_DIR + "gallery-medium-33.jpg",
94 IMAGE_DIR + "gallery-medium-34.jpg",
95 IMAGE_DIR + "gallery-medium-35.jpg",
96 IMAGE_DIR + "gallery-medium-36.jpg",
97 IMAGE_DIR + "gallery-medium-37.jpg",
98 IMAGE_DIR + "gallery-medium-38.jpg",
99 IMAGE_DIR + "gallery-medium-39.jpg",
100 IMAGE_DIR + "gallery-medium-40.jpg",
101 IMAGE_DIR + "gallery-medium-41.jpg",
102 IMAGE_DIR + "gallery-medium-42.jpg",
103 IMAGE_DIR + "gallery-medium-43.jpg",
104 IMAGE_DIR + "gallery-medium-44.jpg",
105 IMAGE_DIR + "gallery-medium-45.jpg",
106 IMAGE_DIR + "gallery-medium-46.jpg",
107 IMAGE_DIR + "gallery-medium-47.jpg",
108 IMAGE_DIR + "gallery-medium-48.jpg",
109 IMAGE_DIR + "gallery-medium-49.jpg",
110 IMAGE_DIR + "gallery-medium-50.jpg",
111 IMAGE_DIR + "gallery-medium-51.jpg",
112 IMAGE_DIR + "gallery-medium-52.jpg",
113 IMAGE_DIR + "gallery-medium-53.jpg",
118 const double trimRate = 0.34;
119 global::System.Collections.Generic.List<double> v;
128 v = new global::System.Collections.Generic.List<double>();
132 public void add(double x)
138 public double getAverage()
140 if(vcnt == 0)return 0.0;
143 public double getTrimedAverage()
145 if(vcnt == 0)return 0.0;
148 int removedCnt = (int)(vcnt * trimRate * 0.5);
149 int trimVcnt = vcnt - removedCnt * 2;
155 for(int i = removedCnt; i < vcnt - removedCnt; i++)
160 return trimVsum / trimVcnt;
163 Statistic mCreationStatistic;
167 ViewTestType mTestType = ViewTestType.TEST_TYPE_COLOR;
169 // To keep reference count.
170 global::System.Collections.Generic.LinkedList<View> mViewList;
171 global::System.Collections.Generic.LinkedList<Timer> mTimerList;
172 global::System.Collections.Generic.LinkedList<Animation> mAnimationList;
174 uint mColumnsCount = ROWS_COUNT;
175 uint mRowsCount = COLUMNS_COUNT;
176 uint mTotalColumnsCount = TOTAL_COLUMNS_COUNT;
177 uint mDurationPerColumns = DURATION_PER_COLUMNS; // miliseconds
182 DateTime appStartTime;
184 protected void CreateScene()
186 appStartTime = DateTime.Now;
188 mViewList = new global::System.Collections.Generic.LinkedList<View>();
189 mTimerList = new global::System.Collections.Generic.LinkedList<Timer>();
190 mAnimationList = new global::System.Collections.Generic.LinkedList<Animation>();
192 mWindow = Window.Instance;
193 mWindow.BackgroundColor = Color.White;
194 mWindowSize = mWindow.WindowSize;
196 mSize = new Vector2(mWindowSize.X / mColumnsCount, mWindowSize.Y / mRowsCount);
198 Timer timer = new Timer(mDurationPerColumns);
199 timer.Tick += OnTick;
200 mTimerList.AddLast(timer);
205 mCreationStatistic = new Statistic();
209 mWindow.KeyEvent += OnKeyEvent;
211 bool OnTick(object o, EventArgs e)
214 if(tickCount < mTotalColumnsCount * (int)ViewTestType.TEST_TYPE_MAX)
217 Timer timer = new Timer(mDurationPerColumns);
218 timer.Tick += OnTick;
219 mTimerList.AddLast(timer);
226 startTime = DateTime.Now;
228 View columnView = new View()
230 BackgroundColor = Color.Blue,
231 Size = new Size(mSize.X, mWindowSize.Y),
232 Position = new Position(mWindowSize.X, 0.0f),
235 for(int i = 0; i < mRowsCount; ++i)
240 case ViewTestType.TEST_TYPE_COLOR:
243 bgView = CreateColor();
246 case ViewTestType.TEST_TYPE_IMAGE:
248 bgView = CreateImage();
251 case ViewTestType.TEST_TYPE_TEXT:
253 bgView = CreateTextLabel();
256 case ViewTestType.TEST_TYPE_ROUNDED_COLOR:
258 bgView = CreateRoundedColor();
261 case ViewTestType.TEST_TYPE_BORDER_COLOR:
263 bgView = CreateBorderColor(Math.Min(mSize.X, mSize.Y) * VIEW_MARGIN_RATE);
266 case ViewTestType.TEST_TYPE_ROUNDED_BORDER_COLOR:
268 bgView = CreateRoundedBorderColor(Math.Min(mSize.X, mSize.Y) * VIEW_MARGIN_RATE);
271 case ViewTestType.TEST_TYPE_BLUR_COLOR:
273 bgView = CreateBlurColor(Math.Min(mSize.X, mSize.Y) * VIEW_MARGIN_RATE * 0.5f);
276 case ViewTestType.TEST_TYPE_ROUNDED_BLUR_COLOR:
278 bgView = CreateRoundedBlurColor(Math.Min(mSize.X, mSize.Y) * VIEW_MARGIN_RATE * 0.5f);
282 bgView.Size = new Size(mSize.X * (1.0f - VIEW_MARGIN_RATE), mSize.Y * (1.0f - VIEW_MARGIN_RATE));
283 bgView.Position = new Position(mSize.X * VIEW_MARGIN_RATE * 0.5f, (mSize.Y * VIEW_MARGIN_RATE * 0.5f) + (mSize.Y * (float)i));
284 columnView.Add(bgView);
287 mWindow.Add(columnView);
288 mViewList.AddLast(columnView);
290 // Add move animation
291 Animation animation = new Animation((int)(mDurationPerColumns * (mColumnsCount + 1)));
292 animation.AnimateTo(columnView, "PositionX", -mSize.X);
293 animation.Finished += OnAnimationFinished;
296 mAnimationList.AddLast(animation);
298 endTime = DateTime.Now;
300 mCreationStatistic.add((endTime - startTime).TotalMilliseconds);
302 if(tickCount % mTotalColumnsCount == 0)
304 Tizen.Log.Error("NUI.PerfNew", $"Average of creation {mRowsCount} NUI({TestTypeString(mTestType)}) : {mCreationStatistic.getTrimedAverage()} ms\n");
305 mCreationStatistic.clear();
306 mTestType = (ViewTestType)(((int)(mTestType) + 1) % (int)(ViewTestType.TEST_TYPE_MAX));
312 void OnKeyEvent(object source, Window.KeyEventArgs e)
314 if (e.Key.State == Key.StateType.Down)
317 //Streamline.AnnotateChannelEnd(0);
319 switch( e.Key.KeyPressedName )
332 void OnAnimationFinished(object o, EventArgs e)
334 // We can assume that front of mViewLIst must be deleted.
335 mViewList.First.Value.Unparent();
336 mViewList.First.Value.Dispose();
337 mViewList.RemoveFirst();
339 // Dereference timer safety
340 mTimerList.First.Value.Dispose();
341 mTimerList.RemoveFirst();
343 // Dereference animation safety
344 mAnimationList.RemoveFirst();
348 Animation me = o as Animation;
351 // If all views are deleted, quit this application. byebye~
352 if(deleteCount == mTotalColumnsCount * (int)ViewTestType.TEST_TYPE_MAX)
354 appEndTime = DateTime.Now;
355 Tizen.Log.Error("NUI.PerfNew", $"Duration of all app running time : {((appEndTime - appStartTime)).TotalMilliseconds} ms\n");
362 private View CreateColor()
364 View bgView = new View(){
365 BackgroundColor = Color.Yellow,
369 private int gImageCount = 0;
370 private View CreateImage()
372 ImageView bgView = new ImageView(){
373 BackgroundColor = Color.Yellow,
374 ResourceUrl = IMAGE_PATH[(gImageCount++) % (IMAGE_PATH.Length)],
378 private View CreateTextLabel()
380 TextLabel bgView = new TextLabel(){
381 Text = "Hello, World!",
385 private View CreateRoundedColor()
387 View bgView = new View(){
388 BackgroundColor = Color.Yellow,
390 CornerRadiusPolicy = VisualTransformPolicyType.Relative,
395 private View CreateBorderColor(float requiredBorderlineWidth)
397 View bgView = new View(){
398 BackgroundColor = Color.Yellow,
399 BorderlineColor = Color.Red,
400 BorderlineWidth = requiredBorderlineWidth,
404 private View CreateRoundedBorderColor(float requiredBorderlineWidth)
406 View bgView = new View(){
407 BackgroundColor = Color.Yellow,
409 CornerRadiusPolicy = VisualTransformPolicyType.Relative,
410 BorderlineColor = Color.Red,
411 BorderlineWidth = requiredBorderlineWidth,
415 private View CreateBlurColor(float requiredBlurRadius)
417 View bgView = new View();
419 using(PropertyMap map = new PropertyMap())
421 map.Insert((int)Visual.Property.Type, new PropertyValue((int)Visual.Type.Color));
422 map.Insert((int)ColorVisualProperty.MixColor, new PropertyValue(Color.Yellow));
423 map.Insert((int)ColorVisualProperty.BlurRadius, new PropertyValue(requiredBlurRadius));
425 bgView.Background = map;
430 private View CreateRoundedBlurColor(float requiredBlurRadius)
432 View bgView = new View();
434 using(PropertyMap map = new PropertyMap())
436 map.Insert((int)Visual.Property.Type, new PropertyValue((int)Visual.Type.Color));
437 map.Insert((int)ColorVisualProperty.MixColor, new PropertyValue(Color.Yellow));
438 map.Insert((int)ColorVisualProperty.BlurRadius, new PropertyValue(requiredBlurRadius));
439 map.Insert((int)Visual.Property.CornerRadius, new PropertyValue(0.5f));
440 map.Insert((int)Visual.Property.CornerRadiusPolicy, new PropertyValue((int)VisualTransformPolicyType.Relative));
442 bgView.Background = map;
448 public void Activate()
452 public void FullGC(){
453 global::System.GC.Collect();
454 global::System.GC.WaitForPendingFinalizers();
455 global::System.GC.Collect();
458 public void Deactivate()
462 private void DestroyScene()
466 protected override void OnCreate()
468 // Up call to the Base class first
474 /// The main entry point for the application.
476 [STAThread] // Forces app to use one thread to access NUI
477 static void Main(string[] args)
479 PerformanceTestExample example = new PerformanceTestExample();