blob: c82afd75547754f2c761f949ffe354683eaf907d [file] [log] [blame]
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001/*
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 */
Stephane Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarieec0919b2018-09-05 14:14:29 -040017package model
18
19import (
khenaidoob9203542018-09-17 22:56:37 -040020 "sync"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040021)
22
Stephane Barbariedc5022d2018-11-19 15:21:44 -050023// Profiling is used to store performance details collected at runtime
npujar9a30c702019-11-14 17:06:39 +053024type Profiling struct {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050025 sync.RWMutex
Stephane Barbarieec0919b2018-09-05 14:14:29 -040026 DatabaseRetrieveTime float64
27 DatabaseRetrieveCount int
28 InMemoryModelTime float64
29 InMemoryModelCount int
30 InMemoryProcessTime float64
31 DatabaseStoreTime float64
Stephane Barbariea188d942018-10-16 16:43:04 -040032 InMemoryLockTime float64
33 InMemoryLockCount int
Stephane Barbarieec0919b2018-09-05 14:14:29 -040034}
35
npujar9a30c702019-11-14 17:06:39 +053036var profilingInstance *Profiling
Stephane Barbariedc5022d2018-11-19 15:21:44 -050037var profilingOnce sync.Once
Stephane Barbarieec0919b2018-09-05 14:14:29 -040038
Stephane Barbariedc5022d2018-11-19 15:21:44 -050039// GetProfiling returns a singleton instance of the Profiling structure
npujar9a30c702019-11-14 17:06:39 +053040func GetProfiling() *Profiling {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050041 profilingOnce.Do(func() {
npujar9a30c702019-11-14 17:06:39 +053042 profilingInstance = &Profiling{}
Stephane Barbarieec0919b2018-09-05 14:14:29 -040043 })
Stephane Barbariedc5022d2018-11-19 15:21:44 -050044 return profilingInstance
Stephane Barbarieec0919b2018-09-05 14:14:29 -040045}
46
Stephane Barbariedc5022d2018-11-19 15:21:44 -050047// AddToDatabaseRetrieveTime appends a time period to retrieve data from the database
npujar9a30c702019-11-14 17:06:39 +053048func (p *Profiling) AddToDatabaseRetrieveTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050049 p.Lock()
50 defer p.Unlock()
51
Stephane Barbarieec0919b2018-09-05 14:14:29 -040052 p.DatabaseRetrieveTime += period
Stephane Barbariedc5022d2018-11-19 15:21:44 -050053 p.DatabaseRetrieveCount++
Stephane Barbarieec0919b2018-09-05 14:14:29 -040054}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050055
56// AddToInMemoryModelTime appends a time period to construct/deconstruct data in memory
npujar9a30c702019-11-14 17:06:39 +053057func (p *Profiling) AddToInMemoryModelTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050058 p.Lock()
59 defer p.Unlock()
60
Stephane Barbarieec0919b2018-09-05 14:14:29 -040061 p.InMemoryModelTime += period
Stephane Barbariedc5022d2018-11-19 15:21:44 -050062 p.InMemoryModelCount++
Stephane Barbarieec0919b2018-09-05 14:14:29 -040063}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050064
65// AddToInMemoryProcessTime appends a time period to process data
npujar9a30c702019-11-14 17:06:39 +053066func (p *Profiling) AddToInMemoryProcessTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050067 p.Lock()
68 defer p.Unlock()
69
Stephane Barbarieec0919b2018-09-05 14:14:29 -040070 p.InMemoryProcessTime += period
71}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050072
73// AddToDatabaseStoreTime appends a time period to store data in the database
npujar9a30c702019-11-14 17:06:39 +053074func (p *Profiling) AddToDatabaseStoreTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050075 p.Lock()
76 defer p.Unlock()
77
Stephane Barbarieec0919b2018-09-05 14:14:29 -040078 p.DatabaseStoreTime += period
79}
80
Stephane Barbariedc5022d2018-11-19 15:21:44 -050081// AddToInMemoryLockTime appends a time period when a code block was locked
npujar9a30c702019-11-14 17:06:39 +053082func (p *Profiling) AddToInMemoryLockTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050083 p.Lock()
84 defer p.Unlock()
85
Stephane Barbariea188d942018-10-16 16:43:04 -040086 p.InMemoryLockTime += period
Stephane Barbariedc5022d2018-11-19 15:21:44 -050087 p.InMemoryLockCount++
Stephane Barbariea188d942018-10-16 16:43:04 -040088}
89
Stephane Barbariedc5022d2018-11-19 15:21:44 -050090// Reset initializes the profile counters
npujar9a30c702019-11-14 17:06:39 +053091func (p *Profiling) Reset() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050092 p.Lock()
93 defer p.Unlock()
94
Stephane Barbariea188d942018-10-16 16:43:04 -040095 p.DatabaseRetrieveTime = 0
96 p.DatabaseRetrieveCount = 0
97 p.InMemoryModelTime = 0
98 p.InMemoryModelCount = 0
99 p.InMemoryProcessTime = 0
100 p.DatabaseStoreTime = 0
101 p.InMemoryLockTime = 0
102 p.InMemoryLockCount = 0
103}
104
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500105// Report will provide the current profile counter status
npujar9a30c702019-11-14 17:06:39 +0530106func (p *Profiling) Report() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500107 p.Lock()
108 defer p.Unlock()
109
Girish Kumarf56a4682020-03-20 20:07:46 +0000110 logger.Infof("[ Profiling Report ]")
111 logger.Infof("Database Retrieval : %f", p.DatabaseRetrieveTime)
112 logger.Infof("Database Retrieval Count : %d", p.DatabaseRetrieveCount)
113 logger.Infof("Avg Database Retrieval : %f", p.DatabaseRetrieveTime/float64(p.DatabaseRetrieveCount))
114 logger.Infof("In-Memory Modeling : %f", p.InMemoryModelTime)
115 logger.Infof("In-Memory Modeling Count: %d", p.InMemoryModelCount)
116 logger.Infof("Avg In-Memory Modeling : %f", p.InMemoryModelTime/float64(p.InMemoryModelCount))
117 logger.Infof("In-Memory Locking : %f", p.InMemoryLockTime)
118 logger.Infof("In-Memory Locking Count: %d", p.InMemoryLockCount)
119 logger.Infof("Avg In-Memory Locking : %f", p.InMemoryLockTime/float64(p.InMemoryLockCount))
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400120
121}