001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.processor; 018 019 import java.text.NumberFormat; 020 import java.util.concurrent.atomic.AtomicInteger; 021 022 import org.apache.camel.Exchange; 023 import org.apache.camel.model.LoggingLevel; 024 import org.apache.commons.logging.Log; 025 026 /** 027 * A logger for logging message throughput. 028 * 029 * @version $Revision: 62931 $ 030 */ 031 public class ThroughputLogger extends Logger { 032 private int groupSize = 100; 033 private long startTime; 034 private long groupStartTime; 035 private AtomicInteger receivedCounter = new AtomicInteger(); 036 private NumberFormat numberFormat = NumberFormat.getNumberInstance(); 037 private String action = "Received"; 038 private String logMessage; 039 040 public ThroughputLogger() { 041 } 042 043 public ThroughputLogger(Log log) { 044 super(log); 045 } 046 047 public ThroughputLogger(Log log, LoggingLevel level) { 048 super(log, level); 049 } 050 051 public ThroughputLogger(String logName) { 052 super(logName); 053 } 054 055 public ThroughputLogger(String logName, LoggingLevel level) { 056 super(logName, level); 057 } 058 059 public ThroughputLogger(String logName, LoggingLevel level, int groupSize) { 060 super(logName, level); 061 setGroupSize(groupSize); 062 } 063 064 public ThroughputLogger(String logName, int groupSize) { 065 super(logName); 066 setGroupSize(groupSize); 067 } 068 069 public ThroughputLogger(int groupSize) { 070 setGroupSize(groupSize); 071 } 072 073 @Override 074 public void process(Exchange exchange) { 075 if (startTime == 0) { 076 startTime = System.currentTimeMillis(); 077 } 078 int receivedCount = receivedCounter.incrementAndGet(); 079 if (receivedCount % groupSize == 0) { 080 logMessage = createLogMessage(exchange, receivedCount); 081 super.process(exchange); 082 } 083 } 084 085 public int getGroupSize() { 086 return groupSize; 087 } 088 089 public void setGroupSize(int groupSize) { 090 if (groupSize == 0) { 091 throw new IllegalArgumentException("groupSize cannot be zero!"); 092 } 093 this.groupSize = groupSize; 094 } 095 096 public NumberFormat getNumberFormat() { 097 return numberFormat; 098 } 099 100 public void setNumberFormat(NumberFormat numberFormat) { 101 this.numberFormat = numberFormat; 102 } 103 104 public String getAction() { 105 return action; 106 } 107 108 public void setAction(String action) { 109 this.action = action; 110 } 111 112 @Override 113 protected Object logMessage(Exchange exchange) { 114 return logMessage; 115 } 116 117 protected String createLogMessage(Exchange exchange, int receivedCount) { 118 long time = System.currentTimeMillis(); 119 if (groupStartTime == 0) { 120 groupStartTime = startTime; 121 } 122 123 double rate = messagesPerSecond(groupSize, groupStartTime, time); 124 double average = messagesPerSecond(receivedCount, startTime, time); 125 126 long duration = time - groupStartTime; 127 groupStartTime = time; 128 129 return getAction() + ": " + receivedCount + " messages so far. Last group took: " + duration 130 + " millis which is: " + numberFormat.format(rate) 131 + " messages per second. average: " + numberFormat.format(average); 132 } 133 134 // timeOneMessage = elapsed / messageCount 135 // messagePerSend = 1000 / timeOneMessage 136 protected double messagesPerSecond(long messageCount, long startTime, long endTime) { 137 double rate = messageCount * 1000.0; 138 rate /= endTime - startTime; 139 return rate; 140 } 141 }