/*
 * Copyright (C) 2012-2013 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.nifty.perf;

import java.lang.InterruptedException;
import java.lang.Thread;
import java.lang.System;
import java.nio.ByteBuffer;

import org.apache.thrift.TException;

public class LoadTestHandler implements LoadTest.Iface {
  public void noop() {
  }

  public void onewayNoop() {
  }

  public void asyncNoop() {
  }

  public long add(long a, long b) {
    return a + b;
  }

  public ByteBuffer echo(ByteBuffer data) {
    return data;
  }

  public void send(ByteBuffer data) {
  }

  public ByteBuffer recv(long recvBytes) {
    return ByteBuffer.allocate((int)recvBytes);
  }

  public ByteBuffer sendrecv(ByteBuffer data, long recvBytes) {
    return recv(recvBytes);
  }

  public void onewaySend(ByteBuffer data) {
  }

  public void onewayThrow(int code) throws TException {
    throw new TException();
  }

  public void throwUnexpected(int code) throws TException {
    throw new TException();
  }

  public void throwError(int code) throws LoadError {
    throw new LoadError(code);
  }

  public void sleep(long microseconds) {
    try {
      long ms = microseconds / 1000;
      int us = (int)(microseconds % 1000);
      Thread.sleep(ms, us);
    }
    catch (InterruptedException e) {
    }
  }

  public void onewaySleep(long microseconds) {
    sleep(microseconds);
  }

  public void badBurn(long microseconds) {
    burnImpl(microseconds);
  }

  public void badSleep(long microseconds) {
    burnImpl(microseconds);
  }

  public void onewayBurn(long microseconds) {
    burnImpl(microseconds);
  }

  public void burn(long microseconds) {
    burnImpl(microseconds);
  }

  private void burnImpl(long microseconds) {
    long end = System.nanoTime() + microseconds;
    while (System.nanoTime() < end) {}
  }
}

@@ -1388,145 +1388,553 @@
 de) 
-throws TException %7B%0A    throw new TException();%0A  %7D%0A%0A  public void throwUnexpected(int code) throws TException %7B%0A    throw new TException
+%7B%0A    // Because this is a one-way throw, it isn't declared in the thrift%0A    // IDL, and doesn't appear on the throws declaration of the generated%0A    // handler interface. Thus, this method must throw an unchecked exception.%0A    throw new Error();%0A  %7D%0A%0A  public void throwUnexpected(int code) %7B%0A    // Because this is an unexpected throw, it isn't declared in the thrift%0A    // IDL, and doesn't appear on the throws declaration of the generated%0A    // handler interface. Thus, this method must throw an unchecked exception.%0A    throw new Error
 ();%0A
@@ -2715,16 +2715,23 @@
 oseconds
+ * 1000
 ;%0A    wh

Load test server fixes