001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.util.server.persistence;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021
022public class SessionEntityCacheStatistics {
023    
024    private Log log;
025    
026    public long readOnlyUpgradedToReadWrite;
027    public long putReadOnlyEntity;
028    public long putReadWriteEntityToParent;
029    public long putReadWriteEntity;
030    public long gotEntityFromReadWrite;
031    public long gotEntityFromReadOnly;
032    public long gotEntityFromParent;
033    public long entityNotGotten;
034    public long finalReadWriteCount;
035    public long finalReadOnlyCount;
036    
037    public long getCacheHits() {
038        return gotEntityFromReadWrite + gotEntityFromReadOnly + gotEntityFromParent;
039    }
040    
041    public long getCacheAttempts() {
042        return getCacheHits() + entityNotGotten;
043    }
044    
045    protected Log getLog() {
046        if(log == null) {
047            log = LogFactory.getLog(this.getClass());
048        }
049        
050        return log;
051    }
052    
053    public void Add(SessionEntityCacheStatistics sessionEntityCacheStatistics) {
054        this.readOnlyUpgradedToReadWrite += sessionEntityCacheStatistics.readOnlyUpgradedToReadWrite;
055        this.putReadOnlyEntity += sessionEntityCacheStatistics.putReadOnlyEntity;
056        this.putReadWriteEntityToParent += sessionEntityCacheStatistics.putReadWriteEntityToParent;
057        this.putReadWriteEntity += sessionEntityCacheStatistics.putReadWriteEntity;
058        this.gotEntityFromReadWrite += sessionEntityCacheStatistics.gotEntityFromReadWrite;
059        this.gotEntityFromReadOnly += sessionEntityCacheStatistics.gotEntityFromReadOnly;
060        this.gotEntityFromParent += sessionEntityCacheStatistics.gotEntityFromParent;
061        this.entityNotGotten += sessionEntityCacheStatistics.entityNotGotten;
062        this.finalReadWriteCount += sessionEntityCacheStatistics.finalReadWriteCount;
063        this.finalReadOnlyCount += sessionEntityCacheStatistics.finalReadOnlyCount;
064    }
065
066    public void dumpStats() {
067        Log myLog = getLog();
068
069        myLog.info("ReadOnlyUpgradedToReadWrite = " + readOnlyUpgradedToReadWrite);
070        myLog.info("PutReadOnlyEntity = " + putReadOnlyEntity);
071        myLog.info("PutReadWriteEntityToParent = " + putReadWriteEntityToParent);
072        myLog.info("PutReadWriteEntity = " + putReadWriteEntity);
073        myLog.info("GotEntityFromReadWrite = " + gotEntityFromReadWrite);
074        myLog.info("GotEntityFromReadOnly = " + gotEntityFromReadOnly);
075        myLog.info("GotEntityFromParent = " + gotEntityFromParent);
076        myLog.info("EntityNotGotten = " + entityNotGotten);
077        myLog.info("CacheHits = " + getCacheHits());
078        myLog.info("CacheAttempts = " + getCacheAttempts());
079        myLog.info("FinalReadWriteCount = " + finalReadWriteCount);
080        myLog.info("FinalReadOnlyCount = " + finalReadOnlyCount);
081    }
082    
083}