blob: c50c9f63e4d7f4c6f35902f30bd4b8aa3ba2a45a [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
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
17package model
18
19import (
Scott Bakere73f91e2019-10-17 12:58:11 -070020 "github.com/opencord/voltha-lib-go/pkg/log"
Scott Baker2c1c4822019-10-16 11:02:41 -070021 "sync"
22)
23
24// Profiling is used to store performance details collected at runtime
25type profiling struct {
26 sync.RWMutex
27 DatabaseRetrieveTime float64
28 DatabaseRetrieveCount int
29 InMemoryModelTime float64
30 InMemoryModelCount int
31 InMemoryProcessTime float64
32 DatabaseStoreTime float64
33 InMemoryLockTime float64
34 InMemoryLockCount int
35}
36
37var profilingInstance *profiling
38var profilingOnce sync.Once
39
40// GetProfiling returns a singleton instance of the Profiling structure
41func GetProfiling() *profiling {
42 profilingOnce.Do(func() {
43 profilingInstance = &profiling{}
44 })
45 return profilingInstance
46}
47
48// AddToDatabaseRetrieveTime appends a time period to retrieve data from the database
49func (p *profiling) AddToDatabaseRetrieveTime(period float64) {
50 p.Lock()
51 defer p.Unlock()
52
53 p.DatabaseRetrieveTime += period
54 p.DatabaseRetrieveCount++
55}
56
57// AddToInMemoryModelTime appends a time period to construct/deconstruct data in memory
58func (p *profiling) AddToInMemoryModelTime(period float64) {
59 p.Lock()
60 defer p.Unlock()
61
62 p.InMemoryModelTime += period
63 p.InMemoryModelCount++
64}
65
66// AddToInMemoryProcessTime appends a time period to process data
67func (p *profiling) AddToInMemoryProcessTime(period float64) {
68 p.Lock()
69 defer p.Unlock()
70
71 p.InMemoryProcessTime += period
72}
73
74// AddToDatabaseStoreTime appends a time period to store data in the database
75func (p *profiling) AddToDatabaseStoreTime(period float64) {
76 p.Lock()
77 defer p.Unlock()
78
79 p.DatabaseStoreTime += period
80}
81
82// AddToInMemoryLockTime appends a time period when a code block was locked
83func (p *profiling) AddToInMemoryLockTime(period float64) {
84 p.Lock()
85 defer p.Unlock()
86
87 p.InMemoryLockTime += period
88 p.InMemoryLockCount++
89}
90
91// Reset initializes the profile counters
92func (p *profiling) Reset() {
93 p.Lock()
94 defer p.Unlock()
95
96 p.DatabaseRetrieveTime = 0
97 p.DatabaseRetrieveCount = 0
98 p.InMemoryModelTime = 0
99 p.InMemoryModelCount = 0
100 p.InMemoryProcessTime = 0
101 p.DatabaseStoreTime = 0
102 p.InMemoryLockTime = 0
103 p.InMemoryLockCount = 0
104}
105
106// Report will provide the current profile counter status
107func (p *profiling) Report() {
108 p.Lock()
109 defer p.Unlock()
110
111 log.Infof("[ Profiling Report ]")
112 log.Infof("Database Retrieval : %f", p.DatabaseRetrieveTime)
113 log.Infof("Database Retrieval Count : %d", p.DatabaseRetrieveCount)
114 log.Infof("Avg Database Retrieval : %f", p.DatabaseRetrieveTime/float64(p.DatabaseRetrieveCount))
115 log.Infof("In-Memory Modeling : %f", p.InMemoryModelTime)
116 log.Infof("In-Memory Modeling Count: %d", p.InMemoryModelCount)
117 log.Infof("Avg In-Memory Modeling : %f", p.InMemoryModelTime/float64(p.InMemoryModelCount))
118 log.Infof("In-Memory Locking : %f", p.InMemoryLockTime)
119 log.Infof("In-Memory Locking Count: %d", p.InMemoryLockCount)
120 log.Infof("Avg In-Memory Locking : %f", p.InMemoryLockTime/float64(p.InMemoryLockCount))
121
122}