blob: 8b515af213fe85df28647d6853f1ffa80e39cdd4 [file] [log] [blame]
Hardik Windlass03fe56f2019-11-27 09:11:25 +00001/*
2 * Copyright 2018-present Open Networking Foundation
3
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
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16package model
17
18import (
Hardik Windlass03fe56f2019-11-27 09:11:25 +000019 "reflect"
20 "testing"
npujar9a30c702019-11-14 17:06:39 +053021
npujar9a30c702019-11-14 17:06:39 +053022 "github.com/stretchr/testify/assert"
Hardik Windlass03fe56f2019-11-27 09:11:25 +000023)
24
25func TestProfiling(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +053026 want := &Profiling{}
Hardik Windlass03fe56f2019-11-27 09:11:25 +000027 result := GetProfiling()
28 if reflect.TypeOf(result) != reflect.TypeOf(want) {
29 t.Errorf("GetProfiling() = result: %v, want: %v", result, want)
30 }
31
32 /*
33 * GetProfiling() returns a singleton instance of the Profiling structure.
34 * Verifying this by interchangably calling other methods through above
35 * returned "result" instance and by again calling GetProfiling() method,
36 * and comparing the results, i.e. all are getting executed on the single
37 * same profiling instance.
38 */
39
Girish Kumarf56a4682020-03-20 20:07:46 +000040 logger.Info("/***** Unit Test Begin: Profiling Report: *****/")
Hardik Windlass03fe56f2019-11-27 09:11:25 +000041 result.Report()
42
43 GetProfiling().AddToDatabaseRetrieveTime(2.0)
44 assert.Equal(t, 2.0, result.DatabaseRetrieveTime)
45 assert.Equal(t, 1, result.DatabaseRetrieveCount)
46 result.AddToDatabaseRetrieveTime(3.0)
47 assert.Equal(t, 5.0, GetProfiling().DatabaseRetrieveTime)
48 assert.Equal(t, 2, GetProfiling().DatabaseRetrieveCount)
49
50 GetProfiling().AddToInMemoryModelTime(2.0)
51 assert.Equal(t, 2.0, result.InMemoryModelTime)
52 assert.Equal(t, 1, result.InMemoryModelCount)
53 result.AddToInMemoryModelTime(3.0)
54 assert.Equal(t, 5.0, GetProfiling().InMemoryModelTime)
55 assert.Equal(t, 2, GetProfiling().InMemoryModelCount)
56
57 GetProfiling().AddToInMemoryProcessTime(2.0)
58 assert.Equal(t, 2.0, result.InMemoryProcessTime)
59 result.AddToInMemoryProcessTime(3.0)
60 assert.Equal(t, 5.0, GetProfiling().InMemoryProcessTime)
61
62 GetProfiling().AddToDatabaseStoreTime(2.0)
63 assert.Equal(t, 2.0, result.DatabaseStoreTime)
64 result.AddToDatabaseStoreTime(3.0)
65 assert.Equal(t, 5.0, GetProfiling().DatabaseStoreTime)
66
67 GetProfiling().AddToInMemoryLockTime(2.0)
68 assert.Equal(t, 2.0, result.InMemoryLockTime)
69 assert.Equal(t, 1, result.InMemoryLockCount)
70 result.AddToInMemoryLockTime(3.0)
71 assert.Equal(t, 5.0, GetProfiling().InMemoryLockTime)
72 assert.Equal(t, 2, GetProfiling().InMemoryLockCount)
73
Girish Kumarf56a4682020-03-20 20:07:46 +000074 logger.Info("/***** Unit Test End: Profiling Report: *****/")
Hardik Windlass03fe56f2019-11-27 09:11:25 +000075 GetProfiling().Report()
76
77 result.Reset()
78}