Package com.ecyrd.speed4j

Examples of com.ecyrd.speed4j.StopWatch


  @Override
  public Map<MailAddress, DeliveryReturnCode> deliver(MailEnvelope env, final String deliveryId)
      throws IOException
  {
    StopWatch stopWatch = Activator.getDefault().getStopWatch();
    Message message;

    try {
      MimeParser parser = new MimeParser();
      parser.parse(env.getMessageInputStream());
      message = parser.getMessage();
    } catch (MimeParserException mpe) {
      logger.error("DID" + deliveryId + ": unable to parse message: ", mpe);
      throw new DeliveryException("Unable to parse message: " + mpe.getMessage());
    } catch (IOException ioe) {
      logger.error("DID" + deliveryId + ": unable to read message stream: ", ioe);
      throw new DeliveryException("Unable to read message stream: " + ioe.getMessage());
    }

    message.setSize((long) env.getSize()); // update message size

    FilterProcessor<Message> processor = new FilterProcessor<Message>();
    //processor.add(new NotificationMailFilter());
    processor.add(new SpamMailFilter());
    processor.add(new DefaultMailFilter());
    message = processor.doFilter(message);

    logEnvelope(env, message, deliveryId);

    Map<MailAddress, DeliveryReturnCode> replies = new HashMap<MailAddress, DeliveryReturnCode>();
    // Deliver to each recipient
    for (MailAddress recipient : env.getRecipients())
    {
      DeliveryReturnCode reply = DeliveryReturnCode.TEMPORARY_FAILURE; // default LMTP reply
      DeliveryAction deliveryAction = DeliveryAction.DELIVER; // default delivery action

      Mailbox mailbox = new Mailbox(recipient.toString());
      String logMsg = new StringBuilder(" ").append(mailbox.getId())
                .append(" DID").append(deliveryId).toString();

      try {
        switch (deliveryAction) {
        case DELIVER:
          try {
            // generate new UUID
            UUID messageId = new MessageIdBuilder().build();

            // store message
            messageDAO.put(mailbox, messageId, message, env.getMessageInputStream());

            // successfully delivered
            stopWatch.stop("DELIVERY.success", logMsg);
            reply = DeliveryReturnCode.OK;
          } catch (OverQuotaException e) {
            // account is over quota, reject
            stopWatch.stop("DELIVERY.reject_overQuota", logMsg + " over quota");
            reply = DeliveryReturnCode.OVER_QUOTA;
          } catch (IOException e) {
            // delivery error, defer
            stopWatch.stop("DELIVERY.defer", logMsg);
            logger.error("DID" + deliveryId + ": delivery error: ", e);
            reply = DeliveryReturnCode.TEMPORARY_FAILURE;
          }
          break;
        case DISCARD:
          // Local delivery is disabled.
          stopWatch.stop("DELIVERY.discard", logMsg);
          reply = DeliveryReturnCode.OK;
          break;
        case DEFER:
          // Delivery to mailbox skipped. Let MTA retry again later.
          stopWatch.stop("DELIVERY.defer", logMsg);
          reply = DeliveryReturnCode.TEMPORARY_FAILURE;
          break;
        case REJECT:
          // Reject delivery. Account or mailbox not found.
          stopWatch.stop("DELIVERY.reject_nonExistent", logMsg + " unknown mailbox");
          reply = DeliveryReturnCode.NO_SUCH_USER;
        }
      } catch (Exception e) {
        stopWatch.stop("DELIVERY.defer_failure", logMsg);
        reply = DeliveryReturnCode.TEMPORARY_FAILURE;
        logger.error("DID" + deliveryId + ": delivery failed (defered): ", e);
      }

      replies.put(recipient, reply); // set delivery status for invoker
View Full Code Here


    Mailbox mailbox;

    logger.debug("POP3: Authenticating session {}, user {}, pass {}",
        new Object[] { session.getSessionID(), username, password });

    StopWatch stopWatch = Activator.getDefault().getStopWatch();

    try {
      // authenticate mailbox, if failed return null
      AccountStatus status = validator.getAccountStatus(username);
      session.getLogger().debug("Validated account (" + username + ") status is " + status.toString());
 
      if (!status.equals(AccountStatus.ACTIVE)) {
        throw new AuthenticationException("User " + username + " does not exist or inactive");
      }
 
      // authenticate user with password
      mailbox = authenticator.authenticate(username, password);
 
      stopWatch.stop("AUTH.success");

      // return POP3 handler for mailbox
      return backend.getMailboxHander(mailbox);
    } catch (IllegalArgumentException iae) {
      stopWatch.stop("AUTH.fail");
      logger.debug("POP3 Authentication failed. Invalid username [{}]: {}", username, iae.getMessage());
      return null;
    } catch (AuthenticationException ae) {
      stopWatch.stop("AUTH.fail");
      logger.debug("POP3 Authentication failed. Invalid username [{}] or password [{}]", username, password);
      return null;
    }
  }
View Full Code Here

     @return true, if the queue is not emptied and there are events
     *          in it where the finalMoment is achieved.
     */
    private boolean emptyQueue(long finalMoment)
    {
        StopWatch sw;

        try
        {
            while( null != (sw = m_queue.poll(100, TimeUnit.MILLISECONDS)) )
            {
                //  Ignore faulty StopWatches
                if( sw.getTag() == null ) continue;
               
                if( sw.getCreationTime() > finalMoment )
                {
                    // Return this one back in the queue; as it belongs to the
                    // next period already.
                    m_queue.addFirst(sw);
                    return true;
                }

                CollectedStatistics cs = m_stats.get(sw.getTag());

                if( cs == null )
                {
                    cs = new CollectedStatistics();
                    m_stats.put( sw.getTag(), cs );
                }

                cs.add( sw );
               
                //
                //  Do the logging to the slowlog as well, if it's defined
                //  and we bypass the threshold.
                //
                //  TODO: Using String.format to get the ISO date isn't very fast. Need to figure out a better method.
                //
                if( m_slowLogOn && m_slowLog != null )
                {
                    Long threshold = m_slowThresholdMicros.get( sw.getTag() );
                    if( threshold != null && sw.getTimeMicros() > threshold )
                    {
                        m_slowLog.info( "{} {} \"{}\" \"{}\" {}", new Object[] {
                                        String.format("%tFT%<tT.%<tL%<tz",new Date(sw.getCreationTime())),
                                        saneDoubleToString( m_slowLogPercentile ),
                                        sw.getTag(),
                                        sw.getMessage() != null ? sw.getMessage() : "",
                                        sw.getTimeMicros()/1000F } );
                    }
                }
            }
        }
        catch( InterruptedException e ) {} // Just return immediately
View Full Code Here

     */
    private void doLog(long lastRun)
    {
        if( m_log == null || !m_log.isInfoEnabled() ) return;
       
        StopWatch sw;
       
        m_statistics = new HashMap<String,CollectedStatistics>();
       
        while( null != (sw = m_queue.poll()) )
        {
            CollectedStatistics cs = m_statistics.get(sw.getTag());
           
            if( cs == null )
            {
                cs = new CollectedStatistics();
                m_statistics.put( sw.getTag(), cs );
            }
           
            cs.add( sw );
        }
       
View Full Code Here

     @return true, if the queue is not emptied and there are events
     *          in it where the finalMoment is achieved.
     */
    private boolean emptyQueue(long finalMoment)
    {
        StopWatch sw;

        try
        {
            while( null != (sw = m_queue.poll(100, TimeUnit.MILLISECONDS)) )
            {
                //  Ignore faulty StopWatches
                if( sw.getTag() == null ) continue;
               
                if( sw.getCreationTime() > finalMoment )
                {
                    // Return this one back in the queue; as it belongs to the
                    // next period already.
                    m_queue.addFirst(sw);
                    return true;
                }

                CollectedStatistics cs = m_stats.get(sw.getTag());

                if( cs == null )
                {
                    cs = new CollectedStatistics();
                    m_stats.put( sw.getTag(), cs );
                }

                cs.add( sw );
               
                //
                //  Do the logging to the slowlog as well, if it's defined
                //  and we bypass the threshold.
                //
                //  TODO: Using String.format to get the ISO date isn't very fast. Need to figure out a better method.
                //
                if( m_slowLogOn && m_slowLog != null )
                {
                    Long threshold = m_slowThresholdMicros.get( sw.getTag() );
                    if( threshold != null && sw.getTimeMicros() > threshold )
                    {
                        m_slowLog.info( "{} {} \"{}\" \"{}\" {}", new Object[] {
                                        String.format("%tFT%<tT.%<tL%<tz",new Date(sw.getCreationTime())),
                                        saneDoubleToString( m_slowLogPercentile ),
                                        sw.getTag(),
                                        sw.getMessage() != null ? sw.getMessage() : "",
                                        sw.getTimeMicros()/1000F } );
                    }
                }
            }
        }
        catch( InterruptedException e ) {} // Just return immediately
View Full Code Here

    {
        StopWatchFactory swf = StopWatchFactory.getDefault();

        int iterations = 100;

        StopWatch sw = swf.getStopWatch("foo");

        for( int i = 0; i < iterations; i++ )
        {
            Thread.sleep(10+ (long)(Math.random() * 10));
        }

        sw.stop("ok");

        assertEquals("ok", sw.getTag());

        assertTrue( sw.getTimeMicros() > iterations*10*1000L ); // No way this could be faster

        String s = sw.toString(iterations);

        assertTrue( s.contains("iterations/second"));
    }
View Full Code Here

        StopWatchFactory swf = StopWatchFactory.getDefault();

        long time = System.currentTimeMillis();
        int iterations = 1200000;

        StopWatch total = swf.getStopWatch();

        for( int i = 0; i < iterations; i++ )
        {
            StopWatch sw = swf.getStopWatch("foo");

            // Do nothing

            sw.stop("iteration:success");
        }

        total.stop("total");
        time = System.currentTimeMillis() - time;
View Full Code Here

     @return true, if the queue is not emptied and there are events
     *          in it where the finalMoment is achieved.
     */
    private boolean emptyQueue(long finalMoment)
    {
        StopWatch sw;

        try
        {
            while( null != (sw = m_queue.poll(100, TimeUnit.MILLISECONDS)) )
            {
                //  Ignore faulty StopWatches
                if( sw.getTag() == null ) continue;
               
                if( sw.getCreationTime() > finalMoment )
                {
                    // Return this one back in the queue; as it belongs to the
                    // next period already.
                    m_queue.addFirst(sw);
                    return true;
                }

                CollectedStatistics cs = m_stats.get(sw.getTag());

                if( cs == null )
                {
                    cs = new CollectedStatistics();
                    m_stats.put( sw.getTag(), cs );
                }

                cs.add( sw );
               
                //
                //  Do the logging to the slowlog as well, if it's defined
                //  and we bypass the threshold.
                //
                //  TODO: Using String.format to get the ISO date isn't very fast. Need to figure out a better method.
                //
                if( m_slowLogOn && m_slowLog != null )
                {
                    Long threshold = m_slowThresholdMicros.get( sw.getTag() );
                    if( threshold != null && sw.getTimeMicros() > threshold )
                    {
                        m_slowLog.info( "{} {} \"{}\" \"{}\" {}", new Object[] {
                                        String.format("%tFT%<tT.%<tL%<tz",new Date(sw.getCreationTime())),
                                        saneDoubleToString( m_slowLogPercentile ),
                                        sw.getTag(),
                                        sw.getMessage() != null ? sw.getMessage() : "",
                                        sw.getTimeMicros()/1000F } );
                    }
                }
            }
        }
        catch( InterruptedException e ) {} // Just return immediately
View Full Code Here

     */
    private void doLog(long lastRun, long finalMoment)
    {
        if( m_log == null || !m_log.isInfoEnabled() ) return;
       
        StopWatch sw;

        HashMap<String,CollectedStatistics> stats = new HashMap<String,CollectedStatistics>();
       
        // Peek at the queue and see if there is someone there.
        while( null != (sw = m_queue.peek()) )
        {
            if( sw.getCreationTime() > finalMoment )
            {
                break;
            }
           
            // Remove the object from the head of the queue.
            m_queue.remove();
           
            CollectedStatistics cs = stats.get(sw.getTag());
           
            if( cs == null )
            {
                cs = new CollectedStatistics();
                stats.put( sw.getTag(), cs );
            }
           
            cs.add( sw );
        }
       
View Full Code Here

     */
    private void doLog(long lastRun, long finalMoment)
    {
        if( m_log == null || !m_log.isInfoEnabled() ) return;
       
        StopWatch sw;

        HashMap<String,CollectedStatistics> stats = new HashMap<String,CollectedStatistics>();
       
        // Peek at the queue and see if there is someone there.
        while( null != (sw = m_queue.peek()) )
        {
            if( sw.getCreationTime() > finalMoment )
            {
                break;
            }
           
            // Remove the object from the head of the queue.
            m_queue.remove();
           
            CollectedStatistics cs = stats.get(sw.getTag());
           
            if( cs == null )
            {
                cs = new CollectedStatistics();
                stats.put( sw.getTag(), cs );
            }
           
            cs.add( sw );
        }
       
View Full Code Here

TOP

Related Classes of com.ecyrd.speed4j.StopWatch

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.