Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [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 ( |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 19 | "github.com/opencord/voltha-go/common/log" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 20 | "sync" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | type profiling struct { |
| 24 | DatabaseRetrieveTime float64 |
| 25 | DatabaseRetrieveCount int |
| 26 | InMemoryModelTime float64 |
| 27 | InMemoryModelCount int |
| 28 | InMemoryProcessTime float64 |
| 29 | DatabaseStoreTime float64 |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 30 | InMemoryLockTime float64 |
| 31 | InMemoryLockCount int |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | var profiling_instance *profiling |
| 35 | var profiling_once sync.Once |
| 36 | |
| 37 | func GetProfiling() *profiling { |
| 38 | profiling_once.Do(func() { |
| 39 | profiling_instance = &profiling{} |
| 40 | }) |
| 41 | return profiling_instance |
| 42 | } |
| 43 | |
| 44 | func (p *profiling) AddToDatabaseRetrieveTime(period float64) { |
| 45 | p.DatabaseRetrieveTime += period |
| 46 | p.DatabaseRetrieveCount += 1 |
| 47 | } |
| 48 | func (p *profiling) AddToInMemoryModelTime(period float64) { |
| 49 | p.InMemoryModelTime += period |
| 50 | p.InMemoryModelCount += 1 |
| 51 | } |
| 52 | func (p *profiling) AddToInMemoryProcessTime(period float64) { |
| 53 | p.InMemoryProcessTime += period |
| 54 | } |
| 55 | func (p *profiling) AddToDatabaseStoreTime(period float64) { |
| 56 | p.DatabaseStoreTime += period |
| 57 | } |
| 58 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 59 | func (p *profiling) AddToInMemoryLockTime(period float64) { |
| 60 | p.InMemoryLockTime += period |
| 61 | p.InMemoryLockCount += 1 |
| 62 | } |
| 63 | |
| 64 | func (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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 75 | func (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 Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 83 | 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 86 | |
| 87 | } |