public void startBenchmark ( ) { startTime = System . currentTimeMillis ( ) ; LOG . info ( "Start to benchmark chronos server" ) ; Thread t = new Thread ( ) { // new thread to output the metrics @ Override public void run ( ) { final long collectPeriod = 10000 ; LOG . info ( "Start another thread to export benchmark metrics every " + collectPeriod / 1000.0 + " second" ) ; int totalCount ; int totalLatency ; long exportTime ; int lastTotalCount = 0 ; int lastTotalLatency = 0 ; long lastExportTime = startTime ; while ( true ) { try { Thread . sleep ( collectPeriod ) ; } catch ( InterruptedException e ) { LOG . error ( "Interrupt when sleep to get benchmark metrics, exit immediately" ) ; System . exit ( 0 ) ; } exportTime = System . currentTimeMillis ( ) ; totalCount = totalCountInteger . get ( ) ; totalLatency = totalLatencyInteger . get ( ) ; double totalCostTime = ( exportTime - startTime ) / 1000.0 ; double costTime = ( exportTime - lastExportTime ) / 1000.0 ; double qps = ( totalCount - lastTotalCount ) / costTime ; double latency = ( totalLatency - lastTotalLatency ) * 1.0 / ( totalCount - lastTotalCount ) ; System . out . println ( "Total " + totalCostTime + ", in " + costTime + " seconds, qps: " + qps + ", latency: " + latency + "ms" ) ; lastTotalCount = totalCount ; lastTotalLatency = totalLatency ; lastExportTime = exportTime ; } } } ; t . setDaemon ( true ) ; t . start ( ) ; while ( true ) { try { long start = System . currentTimeMillis ( ) ; currentTimestamp = chronosClient . getTimestamp ( ) ; totalCountInteger . incrementAndGet ( ) ; totalLatencyInteger . addAndGet ( ( int ) ( System . currentTimeMillis ( ) - start ) ) ; if ( currentTimestamp <= previousTimestamp ) { // check correctness LOG . error ( "Fatal error to get a lower timestamp " + currentTimestamp + "(previous is " + previousTimestamp + "), exit immediately" ) ; System . exit ( 0 ) ; } previousTimestamp = currentTimestamp ; if ( isFailover == true ) { // calculate failover time double failoverTime = ( System . currentTimeMillis ( ) - failoverStartTime ) / 1000.0 ; System . out . println ( "After " + failoverStartTimeString + ", the total failover time is " + failoverTime + " seconds" ) ; } isFailover = false ; } catch ( IOException e ) { LOG . error ( "Exception to get timestamp" ) ; if ( isFailover == false ) { failoverStartTime = System . currentTimeMillis ( ) ; failoverStartTimeString = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) . format ( new Date ( failoverStartTime ) ) ; LOG . info ( "Failover occurs at " + failoverStartTimeString ) ; } isFailover = true ; } } }
Benchmark ChronosServer and print metrics in another thread .