blob: b93d2fcea55c6636481ad0eae31858d325d7969d [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 */
16package model
17
18import (
Stephane Barbarieec0919b2018-09-05 14:14:29 -040019 "github.com/opencord/voltha-go/common/log"
khenaidoob9203542018-09-17 22:56:37 -040020 "sync"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040021)
22
23type profiling struct {
24 DatabaseRetrieveTime float64
25 DatabaseRetrieveCount int
26 InMemoryModelTime float64
27 InMemoryModelCount int
28 InMemoryProcessTime float64
29 DatabaseStoreTime float64
Stephane Barbariea188d942018-10-16 16:43:04 -040030 InMemoryLockTime float64
31 InMemoryLockCount int
Stephane Barbarieec0919b2018-09-05 14:14:29 -040032}
33
34var profiling_instance *profiling
35var profiling_once sync.Once
36
37func GetProfiling() *profiling {
38 profiling_once.Do(func() {
39 profiling_instance = &profiling{}
40 })
41 return profiling_instance
42}
43
44func (p *profiling) AddToDatabaseRetrieveTime(period float64) {
45 p.DatabaseRetrieveTime += period
46 p.DatabaseRetrieveCount += 1
47}
48func (p *profiling) AddToInMemoryModelTime(period float64) {
49 p.InMemoryModelTime += period
50 p.InMemoryModelCount += 1
51}
52func (p *profiling) AddToInMemoryProcessTime(period float64) {
53 p.InMemoryProcessTime += period
54}
55func (p *profiling) AddToDatabaseStoreTime(period float64) {
56 p.DatabaseStoreTime += period
57}
58
Stephane Barbariea188d942018-10-16 16:43:04 -040059func (p *profiling) AddToInMemoryLockTime(period float64) {
60 p.InMemoryLockTime += period
61 p.InMemoryLockCount += 1
62}
63
64func (p *profiling) Reset() {
65 p.DatabaseRetrieveTime = 0
66 p.DatabaseRetrieveCount = 0
67 p.InMemoryModelTime = 0
68 p.InMemoryModelCount = 0
69 p.InMemoryProcessTime = 0
70 p.DatabaseStoreTime = 0
71 p.InMemoryLockTime = 0
72 p.InMemoryLockCount = 0
73}
74
Stephane Barbarieec0919b2018-09-05 14:14:29 -040075func (p *profiling) Report() {
76 log.Infof("[ Profiling Report ]")
77 log.Infof("Database Retrieval : %f", p.DatabaseRetrieveTime)
78 log.Infof("Database Retrieval Count : %d", p.DatabaseRetrieveCount)
79 log.Infof("Avg Database Retrieval : %f", p.DatabaseRetrieveTime/float64(p.DatabaseRetrieveCount))
80 log.Infof("In-Memory Modeling : %f", p.InMemoryModelTime)
81 log.Infof("In-Memory Modeling Count: %d", p.InMemoryModelCount)
82 log.Infof("Avg In-Memory Modeling : %f", p.InMemoryModelTime/float64(p.InMemoryModelCount))
Stephane Barbariea188d942018-10-16 16:43:04 -040083 log.Infof("In-Memory Locking : %f", p.InMemoryLockTime)
84 log.Infof("In-Memory Locking Count: %d", p.InMemoryLockCount)
85 log.Infof("Avg In-Memory Locking : %f", p.InMemoryLockTime/float64(p.InMemoryLockCount))
Stephane Barbarieec0919b2018-09-05 14:14:29 -040086
87}