Search in sources :

Example 1 with FutureResult

use of cn.moyada.dubbo.faker.core.model.FutureResult in project dubbo-faker by moyada.

the class AsyncInvoker method invoke.

@Override
public void invoke(Object[] argsValue) {
    super.count.increment();
    Timestamp invokeTime = Timestamp.from(Instant.now());
    CompletableFuture.supplyAsync(() -> {
        FutureResult result;
        // 开始时间
        long start = System.nanoTime();
        try {
            result = FutureResult.success(super.execute(argsValue));
        } catch (Throwable e) {
            result = FutureResult.failed(e.getMessage());
        }
        result.setSpend(start);
        // result.setSpend((System.nanoTime() - start) / 1000_000);
        return result;
    }, this.excutor).whenComplete((result, ex) -> {
        // 完成计算耗时
        result.setSpend((System.nanoTime() - result.getSpend()) / 1000_000);
        // result.setSpend(Duration.between(now, Instant.now()).toMillis());
        super.callback(new InvokeFuture(result, invokeTime, Arrays.toString(argsValue)));
        super.count.decrement();
    });
}
Also used : FutureResult(cn.moyada.dubbo.faker.core.model.FutureResult) InvokeFuture(cn.moyada.dubbo.faker.core.model.InvokeFuture) Timestamp(java.sql.Timestamp)

Example 2 with FutureResult

use of cn.moyada.dubbo.faker.core.model.FutureResult in project dubbo-faker by moyada.

the class DefaultInvoker method invoke.

@Override
public void invoke(Object[] argsValue) {
    super.count.increment();
    this.excutor.submit(() -> {
        // 开始时间
        Timestamp invokeTime = Timestamp.from(Instant.now());
        FutureResult result;
        long start = System.nanoTime();
        try {
            result = FutureResult.success(super.execute(argsValue));
        } catch (Throwable e) {
            result = FutureResult.failed(e.getMessage());
        }
        // 完成计算耗时
        result.setSpend((System.nanoTime() - start) / 1000_000);
        super.callback(new InvokeFuture(result, invokeTime, Arrays.toString(argsValue)));
        super.count.decrement();
    });
}
Also used : FutureResult(cn.moyada.dubbo.faker.core.model.FutureResult) InvokeFuture(cn.moyada.dubbo.faker.core.model.InvokeFuture) Timestamp(java.sql.Timestamp)

Example 3 with FutureResult

use of cn.moyada.dubbo.faker.core.model.FutureResult in project dubbo-faker by moyada.

the class FiberInvoker method invoke.

@Suspendable
@Override
public void invoke(Object[] argsValue) {
    super.count.increment();
    Timestamp invokeTime = Timestamp.from(Instant.now());
    Fiber<FutureResult> fiber = this.scheduler.newFiber((SuspendableCallable<FutureResult>) () -> {
        FutureResult result;
        long start = System.nanoTime();
        try {
            result = FutureResult.success(execute(argsValue));
        } catch (Throwable e) {
            result = FutureResult.failed(e.getMessage());
        }
        result.setSpend((System.nanoTime() - start) / 1000_000);
        return result;
    }).start();
    for (; ; ) {
        if (fiber.isDone()) {
            try {
                FutureResult result = fiber.get();
                super.callback(new InvokeFuture(result, invokeTime, Arrays.toString(argsValue)));
            } catch (ExecutionException | InterruptedException e) {
                e.printStackTrace();
            }
            super.count.decrement();
            break;
        }
    }
}
Also used : FutureResult(cn.moyada.dubbo.faker.core.model.FutureResult) InvokeFuture(cn.moyada.dubbo.faker.core.model.InvokeFuture) ExecutionException(java.util.concurrent.ExecutionException) Timestamp(java.sql.Timestamp) SuspendableCallable(co.paralleluniverse.strands.SuspendableCallable) Suspendable(co.paralleluniverse.fibers.Suspendable)

Aggregations

FutureResult (cn.moyada.dubbo.faker.core.model.FutureResult)3 InvokeFuture (cn.moyada.dubbo.faker.core.model.InvokeFuture)3 Timestamp (java.sql.Timestamp)3 Suspendable (co.paralleluniverse.fibers.Suspendable)1 SuspendableCallable (co.paralleluniverse.strands.SuspendableCallable)1 ExecutionException (java.util.concurrent.ExecutionException)1