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 | */ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
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 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 23 | // Profiling is used to store performance details collected at runtime |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 24 | type Profiling struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 25 | sync.RWMutex |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 26 | DatabaseRetrieveTime float64 |
| 27 | DatabaseRetrieveCount int |
| 28 | InMemoryModelTime float64 |
| 29 | InMemoryModelCount int |
| 30 | InMemoryProcessTime float64 |
| 31 | DatabaseStoreTime float64 |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 32 | InMemoryLockTime float64 |
| 33 | InMemoryLockCount int |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 34 | } |
| 35 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 36 | var profilingInstance *Profiling |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 37 | var profilingOnce sync.Once |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 38 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 39 | // GetProfiling returns a singleton instance of the Profiling structure |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 40 | func GetProfiling() *Profiling { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 41 | profilingOnce.Do(func() { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 42 | profilingInstance = &Profiling{} |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 43 | }) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 44 | return profilingInstance |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 47 | // AddToDatabaseRetrieveTime appends a time period to retrieve data from the database |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 48 | func (p *Profiling) AddToDatabaseRetrieveTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 49 | p.Lock() |
| 50 | defer p.Unlock() |
| 51 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 52 | p.DatabaseRetrieveTime += period |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 53 | p.DatabaseRetrieveCount++ |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 54 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 55 | |
| 56 | // AddToInMemoryModelTime appends a time period to construct/deconstruct data in memory |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 57 | func (p *Profiling) AddToInMemoryModelTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 58 | p.Lock() |
| 59 | defer p.Unlock() |
| 60 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 61 | p.InMemoryModelTime += period |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 62 | p.InMemoryModelCount++ |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 63 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 64 | |
| 65 | // AddToInMemoryProcessTime appends a time period to process data |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 66 | func (p *Profiling) AddToInMemoryProcessTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 67 | p.Lock() |
| 68 | defer p.Unlock() |
| 69 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 70 | p.InMemoryProcessTime += period |
| 71 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 72 | |
| 73 | // AddToDatabaseStoreTime appends a time period to store data in the database |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 74 | func (p *Profiling) AddToDatabaseStoreTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 75 | p.Lock() |
| 76 | defer p.Unlock() |
| 77 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 78 | p.DatabaseStoreTime += period |
| 79 | } |
| 80 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 81 | // AddToInMemoryLockTime appends a time period when a code block was locked |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 82 | func (p *Profiling) AddToInMemoryLockTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 83 | p.Lock() |
| 84 | defer p.Unlock() |
| 85 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 86 | p.InMemoryLockTime += period |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 87 | p.InMemoryLockCount++ |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 88 | } |
| 89 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 90 | // Reset initializes the profile counters |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 91 | func (p *Profiling) Reset() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 92 | p.Lock() |
| 93 | defer p.Unlock() |
| 94 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 95 | 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 Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 105 | // Report will provide the current profile counter status |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 106 | func (p *Profiling) Report() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 107 | p.Lock() |
| 108 | defer p.Unlock() |
| 109 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 110 | 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 120 | |
| 121 | } |