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 |
| 30 | } |
| 31 | |
| 32 | var profiling_instance *profiling |
| 33 | var profiling_once sync.Once |
| 34 | |
| 35 | func GetProfiling() *profiling { |
| 36 | profiling_once.Do(func() { |
| 37 | profiling_instance = &profiling{} |
| 38 | }) |
| 39 | return profiling_instance |
| 40 | } |
| 41 | |
| 42 | func (p *profiling) AddToDatabaseRetrieveTime(period float64) { |
| 43 | p.DatabaseRetrieveTime += period |
| 44 | p.DatabaseRetrieveCount += 1 |
| 45 | } |
| 46 | func (p *profiling) AddToInMemoryModelTime(period float64) { |
| 47 | p.InMemoryModelTime += period |
| 48 | p.InMemoryModelCount += 1 |
| 49 | } |
| 50 | func (p *profiling) AddToInMemoryProcessTime(period float64) { |
| 51 | p.InMemoryProcessTime += period |
| 52 | } |
| 53 | func (p *profiling) AddToDatabaseStoreTime(period float64) { |
| 54 | p.DatabaseStoreTime += period |
| 55 | } |
| 56 | |
| 57 | func (p *profiling) Report() { |
| 58 | log.Infof("[ Profiling Report ]") |
| 59 | log.Infof("Database Retrieval : %f", p.DatabaseRetrieveTime) |
| 60 | log.Infof("Database Retrieval Count : %d", p.DatabaseRetrieveCount) |
| 61 | log.Infof("Avg Database Retrieval : %f", p.DatabaseRetrieveTime/float64(p.DatabaseRetrieveCount)) |
| 62 | log.Infof("In-Memory Modeling : %f", p.InMemoryModelTime) |
| 63 | log.Infof("In-Memory Modeling Count: %d", p.InMemoryModelCount) |
| 64 | log.Infof("Avg In-Memory Modeling : %f", p.InMemoryModelTime/float64(p.InMemoryModelCount)) |
| 65 | |
| 66 | } |