2 * Copyright (c) 2023 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 = 40;
27 const uint COLUMNS_COUNT = 40;
28 const uint TOTAL_COLUMNS_COUNT = 80;
29 const uint DURATION_PER_COLUMNS = 50; // miliseconds
30 // Increase animation time cause OnTick time can be delayed.
31 const uint DURATION_OF_ANIMATION = (DURATION_PER_COLUMNS * (COLUMNS_COUNT * 4 / 3)); // miliseconds.
34 const float VIEW_MARGIN_RATE = 0.2f;
37 TEST_TYPE_COLOR = 0, ///< Test with simple color
38 TEST_TYPE_IMAGE, ///< Test with simple image
39 TEST_TYPE_TEXT, ///< Test with simple text label
40 TEST_TYPE_ROUNDED_COLOR, ///< Test with rounded color
41 TEST_TYPE_BORDER_COLOR, ///< Test with borderline color
42 TEST_TYPE_ROUNDED_BORDER_COLOR, ///< Test with rounded borderline color
43 TEST_TYPE_BLUR_COLOR, ///< Test with blur color
44 TEST_TYPE_ROUNDED_BLUR_COLOR, ///< Test with blur color
48 static string TestTypeString(ViewTestType type)
52 case ViewTestType.TEST_TYPE_COLOR: return "COLOR";
53 case ViewTestType.TEST_TYPE_IMAGE: return "IMAGE";
54 case ViewTestType.TEST_TYPE_TEXT: return "TEXT";
55 case ViewTestType.TEST_TYPE_ROUNDED_COLOR: return "ROUNDED COLOR";
56 case ViewTestType.TEST_TYPE_BORDER_COLOR: return "BORDER COLOR";
57 case ViewTestType.TEST_TYPE_ROUNDED_BORDER_COLOR:return "ROUNDED BORDER COLOR";
58 case ViewTestType.TEST_TYPE_BLUR_COLOR: return "BLUR COLOR";
59 case ViewTestType.TEST_TYPE_ROUNDED_BLUR_COLOR: return "ROUNDED BLUR COLOR";
60 default: return "UNKNOWN";
64 static string IMAGE_DIR = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "image/";
65 readonly static string[] IMAGE_PATH =
67 IMAGE_DIR + "gallery-small-1.jpg",
72 const double trimRate = 0.34;
73 global::System.Collections.Generic.List<double> v;
82 v = new global::System.Collections.Generic.List<double>();
86 public void add(double x)
92 public double getAverage()
94 if(vcnt == 0)return 0.0;
97 public double getTrimedAverage()
99 if(vcnt == 0)return 0.0;
102 int removedCnt = (int)(vcnt * trimRate * 0.5); // floor
103 int trimVcnt = vcnt - removedCnt * 2;
109 for(int i = removedCnt; i < vcnt - removedCnt; i++)
114 return trimVsum / trimVcnt;
117 Statistic mCreationStatistic;
121 ViewTestType mTestType = ViewTestType.TEST_TYPE_COLOR;
123 // To keep reference count.
124 global::System.Collections.Generic.LinkedList<View> mCreatingControlList;
125 global::System.Collections.Generic.LinkedList<View> mRemovingControlList;
126 global::System.Collections.Generic.LinkedList<Timer> mTimerList;
127 global::System.Collections.Generic.LinkedList<Animation> mCreatingAnimationList;
128 global::System.Collections.Generic.LinkedList<Animation> mRemovingAnimationList;
130 uint mColumnsCount = ROWS_COUNT;
131 uint mRowsCount = COLUMNS_COUNT;
132 uint mTotalColumnsCount = TOTAL_COLUMNS_COUNT;
133 uint mDurationPerColumns = DURATION_PER_COLUMNS; // miliseconds
135 uint mCreateCount = 0u;
136 uint mDeleteCount = 0u;
137 uint mImageCount = 0u;
139 DateTime appStartTime;
141 protected void CreateScene()
143 appStartTime = DateTime.Now;
145 mCreatingControlList = new global::System.Collections.Generic.LinkedList<View>();
146 mRemovingControlList = new global::System.Collections.Generic.LinkedList<View>();
147 mTimerList = new global::System.Collections.Generic.LinkedList<Timer>();
148 mCreatingAnimationList = new global::System.Collections.Generic.LinkedList<Animation>();
149 mRemovingAnimationList = new global::System.Collections.Generic.LinkedList<Animation>();
151 mWindow = Window.Instance;
152 mWindow.BackgroundColor = Color.White;
153 mWindowSize = mWindow.WindowSize;
155 mSize = new Vector2(mWindowSize.X / mColumnsCount, mWindowSize.Y / mRowsCount);
157 Timer timer = new Timer(mDurationPerColumns);
158 timer.Tick += OnTick;
159 mTimerList.AddLast(timer);
163 mCreationStatistic = new Statistic();
167 mWindow.KeyEvent += OnKeyEvent;
169 bool OnTick(object o, EventArgs e)
172 if(mCreateCount < mColumnsCount)
175 Timer timer = new Timer(mDurationPerColumns);
176 timer.Tick += OnTick;
177 mTimerList.AddLast(timer);
183 void CreateColumnView()
188 startTime = DateTime.Now;
190 View columnView = new View()
192 BackgroundColor = Color.Blue,
193 Size = new Size(mSize.X, mWindowSize.Y),
194 Position = new Position(mSize.X * (mCreateCount % mColumnsCount), -mWindowSize.Y),
196 ParentOrigin = Position.ParentOriginTopLeft,
197 PivotPoint = Position.PivotPointTopLeft,
198 PositionUsesPivotPoint = true,
201 for(int i = 0; i < mRowsCount; ++i)
206 case ViewTestType.TEST_TYPE_COLOR:
209 bgView = CreateColor();
212 case ViewTestType.TEST_TYPE_IMAGE:
214 bgView = CreateImage();
217 case ViewTestType.TEST_TYPE_TEXT:
219 bgView = CreateTextLabel();
222 case ViewTestType.TEST_TYPE_ROUNDED_COLOR:
224 bgView = CreateRoundedColor();
227 case ViewTestType.TEST_TYPE_BORDER_COLOR:
229 bgView = CreateBorderColor(Math.Min(mSize.X, mSize.Y) * VIEW_MARGIN_RATE);
232 case ViewTestType.TEST_TYPE_ROUNDED_BORDER_COLOR:
234 bgView = CreateRoundedBorderColor(Math.Min(mSize.X, mSize.Y) * VIEW_MARGIN_RATE);
237 case ViewTestType.TEST_TYPE_BLUR_COLOR:
239 bgView = CreateBlurColor(Math.Min(mSize.X, mSize.Y) * VIEW_MARGIN_RATE * 0.5f);
242 case ViewTestType.TEST_TYPE_ROUNDED_BLUR_COLOR:
244 bgView = CreateRoundedBlurColor(Math.Min(mSize.X, mSize.Y) * VIEW_MARGIN_RATE * 0.5f);
248 bgView.Size = new Size(mSize.X * (1.0f - VIEW_MARGIN_RATE), mSize.Y * (1.0f - VIEW_MARGIN_RATE));
249 bgView.Position = new Position(mSize.X * VIEW_MARGIN_RATE * 0.5f, (mSize.Y * VIEW_MARGIN_RATE * 0.5f) + (mSize.Y * (float)i));
250 columnView.Add(bgView);
253 mWindow.Add(columnView);
254 mCreatingControlList.AddLast(columnView);
256 // Add appearing animation
257 Animation appearingAnimation = new Animation((int)(DURATION_OF_ANIMATION));
258 appearingAnimation.AnimateTo(columnView, "PositionY", 0.0f);
259 appearingAnimation.Finished += OnAppearAnimationFinished;
260 appearingAnimation.Play();
262 mCreatingAnimationList.AddLast(appearingAnimation);
264 endTime = DateTime.Now;
266 mCreationStatistic.add((endTime - startTime).TotalMilliseconds);
270 if(mCreateCount % mTotalColumnsCount == 0)
272 Tizen.Log.Error("NUI.PerfNew", $"Average of creation {mRowsCount} NUI({TestTypeString(mTestType)}) : {mCreationStatistic.getTrimedAverage()} ms\n");
273 mCreationStatistic.clear();
274 mTestType = (ViewTestType)(((int)(mTestType) + 1) % (int)(ViewTestType.TEST_TYPE_MAX));
278 void OnKeyEvent(object source, Window.KeyEventArgs e)
280 if (e.Key.State == Key.StateType.Down)
283 //Streamline.AnnotateChannelEnd(0);
285 switch( e.Key.KeyPressedName )
298 void OnAppearAnimationFinished(object o, EventArgs e)
300 // We can assume that front of mControlList must be disappearing.
301 var currentControl = mCreatingControlList.First.Value;
302 mCreatingControlList.RemoveFirst();
304 // Dereference timer safety
305 if(mTimerList.Count > 0)
307 mTimerList.First.Value.Dispose();
308 mTimerList.RemoveFirst();
311 // Dereference animation safety
312 mCreatingAnimationList.RemoveFirst();
314 mRemovingControlList.AddLast(currentControl);
316 if(mCreateCount < mTotalColumnsCount * (int)(ViewTestType.TEST_TYPE_MAX))
321 // Add disappearing animation
322 Animation disappearingAnimation = new Animation((int)(DURATION_OF_ANIMATION));
323 disappearingAnimation.AnimateTo(currentControl, "PositionY", (float)mWindowSize.Y);
324 disappearingAnimation.Finished += OnDisappearAnimationFinished;
325 disappearingAnimation.Play();
327 mRemovingAnimationList.AddLast(disappearingAnimation);
329 void OnDisappearAnimationFinished(object o, EventArgs e)
331 // We can assume that front of mControlList must be disappearing.
332 var currentControl = mRemovingControlList.First.Value;
333 mRemovingControlList.RemoveFirst();
335 // We can assume that front of mViewList must be deleted.
336 currentControl.Unparent();
337 currentControl.DisposeRecursively();
339 // Dereference animation safety
340 mRemovingAnimationList.RemoveFirst();
344 // If all views are deleted, quit this application. byebye~
345 if(mDeleteCount == mTotalColumnsCount * (int)ViewTestType.TEST_TYPE_MAX)
347 appEndTime = DateTime.Now;
348 Tizen.Log.Error("NUI.PerfNew", $"Duration of all app running time : {((appEndTime - appStartTime)).TotalMilliseconds} ms\n");
355 private View CreateColor()
357 View bgView = new View()
359 BackgroundColor = Color.Yellow,
361 ParentOrigin = Position.ParentOriginTopLeft,
362 PivotPoint = Position.PivotPointTopLeft,
363 PositionUsesPivotPoint = true,
367 private View CreateImage()
369 ImageView bgView = new ImageView()
371 ResourceUrl = IMAGE_PATH[(mImageCount++) % (IMAGE_PATH.Length)],
373 ParentOrigin = Position.ParentOriginTopLeft,
374 PivotPoint = Position.PivotPointTopLeft,
375 PositionUsesPivotPoint = true,
379 private View CreateTextLabel()
381 TextLabel bgView = new TextLabel()
383 Text = "Hello, World!",
385 ParentOrigin = Position.ParentOriginTopLeft,
386 PivotPoint = Position.PivotPointTopLeft,
387 PositionUsesPivotPoint = true,
391 private View CreateRoundedColor()
393 View bgView = new View()
395 BackgroundColor = Color.Yellow,
397 CornerRadiusPolicy = VisualTransformPolicyType.Relative,
399 ParentOrigin = Position.ParentOriginTopLeft,
400 PivotPoint = Position.PivotPointTopLeft,
401 PositionUsesPivotPoint = true,
406 private View CreateBorderColor(float requiredBorderlineWidth)
408 View bgView = new View()
410 BackgroundColor = Color.Yellow,
411 BorderlineColor = Color.Red,
412 BorderlineWidth = requiredBorderlineWidth,
414 ParentOrigin = Position.ParentOriginTopLeft,
415 PivotPoint = Position.PivotPointTopLeft,
416 PositionUsesPivotPoint = true,
420 private View CreateRoundedBorderColor(float requiredBorderlineWidth)
422 View bgView = new View()
424 BackgroundColor = Color.Yellow,
426 CornerRadiusPolicy = VisualTransformPolicyType.Relative,
427 BorderlineColor = Color.Red,
428 BorderlineWidth = requiredBorderlineWidth,
430 ParentOrigin = Position.ParentOriginTopLeft,
431 PivotPoint = Position.PivotPointTopLeft,
432 PositionUsesPivotPoint = true,
436 private View CreateBlurColor(float requiredBlurRadius)
438 View bgView = new View()
440 ParentOrigin = Position.ParentOriginTopLeft,
441 PivotPoint = Position.PivotPointTopLeft,
442 PositionUsesPivotPoint = true,
445 using(PropertyMap map = new PropertyMap())
447 map.Insert((int)Visual.Property.Type, new PropertyValue((int)Visual.Type.Color));
448 map.Insert((int)ColorVisualProperty.MixColor, new PropertyValue(Color.Yellow));
449 map.Insert((int)ColorVisualProperty.BlurRadius, new PropertyValue(requiredBlurRadius));
451 bgView.Background = map;
456 private View CreateRoundedBlurColor(float requiredBlurRadius)
458 View bgView = new View()
460 ParentOrigin = Position.ParentOriginTopLeft,
461 PivotPoint = Position.PivotPointTopLeft,
462 PositionUsesPivotPoint = true,
465 using(PropertyMap map = new PropertyMap())
467 map.Insert((int)Visual.Property.Type, new PropertyValue((int)Visual.Type.Color));
468 map.Insert((int)ColorVisualProperty.MixColor, new PropertyValue(Color.Yellow));
469 map.Insert((int)ColorVisualProperty.BlurRadius, new PropertyValue(requiredBlurRadius));
470 map.Insert((int)Visual.Property.CornerRadius, new PropertyValue(0.5f));
471 map.Insert((int)Visual.Property.CornerRadiusPolicy, new PropertyValue((int)VisualTransformPolicyType.Relative));
473 bgView.Background = map;
479 public void Activate()
485 global::System.GC.Collect();
486 global::System.GC.WaitForPendingFinalizers();
487 global::System.GC.Collect();
490 public void Deactivate()
495 private void CleanList<T>(ref global::System.Collections.Generic.LinkedList<T> list) where T : BaseHandle
497 while(list.Count > 0)
499 list.First.Value.Dispose();
505 private void DestroyScene()
507 // Dereference safety
508 CleanList(ref mCreatingControlList);
509 CleanList(ref mRemovingControlList);
510 CleanList(ref mTimerList);
511 CleanList(ref mCreatingAnimationList);
512 CleanList(ref mRemovingAnimationList);
515 protected override void OnCreate()
517 // Up call to the Base class first
523 /// The main entry point for the application.
525 [STAThread] // Forces app to use one thread to access NUI
526 static void Main(string[] args)
528 PerformanceTestExample example = new PerformanceTestExample();