Hardik Windlass | 03fe56f | 2019-11-27 09:11:25 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package model |
| 17 | |
| 18 | import ( |
Hardik Windlass | 03fe56f | 2019-11-27 09:11:25 +0000 | [diff] [blame] | 19 | "reflect" |
| 20 | "testing" |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 21 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 23 | "github.com/stretchr/testify/assert" |
Hardik Windlass | 03fe56f | 2019-11-27 09:11:25 +0000 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func TestProfiling(t *testing.T) { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 27 | want := &Profiling{} |
Hardik Windlass | 03fe56f | 2019-11-27 09:11:25 +0000 | [diff] [blame] | 28 | result := GetProfiling() |
| 29 | if reflect.TypeOf(result) != reflect.TypeOf(want) { |
| 30 | t.Errorf("GetProfiling() = result: %v, want: %v", result, want) |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * GetProfiling() returns a singleton instance of the Profiling structure. |
| 35 | * Verifying this by interchangably calling other methods through above |
| 36 | * returned "result" instance and by again calling GetProfiling() method, |
| 37 | * and comparing the results, i.e. all are getting executed on the single |
| 38 | * same profiling instance. |
| 39 | */ |
| 40 | |
| 41 | log.Info("/***** Unit Test Begin: Profiling Report: *****/") |
| 42 | result.Report() |
| 43 | |
| 44 | GetProfiling().AddToDatabaseRetrieveTime(2.0) |
| 45 | assert.Equal(t, 2.0, result.DatabaseRetrieveTime) |
| 46 | assert.Equal(t, 1, result.DatabaseRetrieveCount) |
| 47 | result.AddToDatabaseRetrieveTime(3.0) |
| 48 | assert.Equal(t, 5.0, GetProfiling().DatabaseRetrieveTime) |
| 49 | assert.Equal(t, 2, GetProfiling().DatabaseRetrieveCount) |
| 50 | |
| 51 | GetProfiling().AddToInMemoryModelTime(2.0) |
| 52 | assert.Equal(t, 2.0, result.InMemoryModelTime) |
| 53 | assert.Equal(t, 1, result.InMemoryModelCount) |
| 54 | result.AddToInMemoryModelTime(3.0) |
| 55 | assert.Equal(t, 5.0, GetProfiling().InMemoryModelTime) |
| 56 | assert.Equal(t, 2, GetProfiling().InMemoryModelCount) |
| 57 | |
| 58 | GetProfiling().AddToInMemoryProcessTime(2.0) |
| 59 | assert.Equal(t, 2.0, result.InMemoryProcessTime) |
| 60 | result.AddToInMemoryProcessTime(3.0) |
| 61 | assert.Equal(t, 5.0, GetProfiling().InMemoryProcessTime) |
| 62 | |
| 63 | GetProfiling().AddToDatabaseStoreTime(2.0) |
| 64 | assert.Equal(t, 2.0, result.DatabaseStoreTime) |
| 65 | result.AddToDatabaseStoreTime(3.0) |
| 66 | assert.Equal(t, 5.0, GetProfiling().DatabaseStoreTime) |
| 67 | |
| 68 | GetProfiling().AddToInMemoryLockTime(2.0) |
| 69 | assert.Equal(t, 2.0, result.InMemoryLockTime) |
| 70 | assert.Equal(t, 1, result.InMemoryLockCount) |
| 71 | result.AddToInMemoryLockTime(3.0) |
| 72 | assert.Equal(t, 5.0, GetProfiling().InMemoryLockTime) |
| 73 | assert.Equal(t, 2, GetProfiling().InMemoryLockCount) |
| 74 | |
| 75 | log.Info("/***** Unit Test End: Profiling Report: *****/") |
| 76 | GetProfiling().Report() |
| 77 | |
| 78 | result.Reset() |
| 79 | } |