blob: 968aac6ae3889246bd3ff9b4d6ae74fdc7936426 [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
serkant.uluderya2ae470f2020-01-21 11:13:09 -080022 "github.com/opencord/voltha-lib-go/v3/pkg/log"
npujar9a30c702019-11-14 17:06:39 +053023 "github.com/stretchr/testify/assert"
Hardik Windlass03fe56f2019-11-27 09:11:25 +000024)
25
26func TestProfiling(t *testing.T) {
npujar9a30c702019-11-14 17:06:39 +053027 want := &Profiling{}
Hardik Windlass03fe56f2019-11-27 09:11:25 +000028 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}