summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/text/Benchmark.java
blob: b32927c2f5d31cabeabdaccecda32836dd5785c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;

// import com.google.common.base.Preconditions;
// import com.google.inject.Provider;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
 */
class Benchmark {

    public static interface Task {
        public long run(CyclicBarrier barrier, int numIterations) throws Exception;
    }


    public static class TaskProvider {
        final Class<? extends Task> taskClass;
        public Task get() {
            try {
                return taskClass.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        public TaskProvider(final Class<? extends Task> taskClass) {
            this.taskClass = taskClass;
        }
    }

    private final TaskProvider taskProvider;
    private final int numIterationsPerThread;
    private final int numThreads;

    private Benchmark(Builder builder) {
        Objects.requireNonNull(builder.taskProvider, "taskProvider");
/*
        Preconditions.checkArgument(builder.numIterationsPerThread > 0, "numIterationsPerThread; %s",
                                    builder.numIterationsPerThread);
        Preconditions.checkArgument(builder.numThreads > 0, "numThreads; %s",
                                    builder.numThreads);
*/
        taskProvider = builder.taskProvider;
        numIterationsPerThread = builder.numIterationsPerThread;
        numThreads = builder.numThreads;
    }

    public long run() throws Exception {
        final CyclicBarrier barrier = new CyclicBarrier(numThreads);
        List<Callable<Long>> clients = new ArrayList<>(numThreads);
        for (int i = 0; i < numThreads; ++i) {
            final Task task = taskProvider.get();
            clients.add(new Callable<Long>() {

                @Override
                public Long call() throws Exception {
                    return task.run(barrier, numIterationsPerThread);
                }
            });
        }
        long maxNanosPerClient = 0;
        for (Future<Long> result : Executors.newFixedThreadPool(numThreads).invokeAll(clients)) {
            maxNanosPerClient = Math.max(maxNanosPerClient, result.get());
        }
        return TimeUnit.SECONDS.toNanos(1) * numThreads * numIterationsPerThread / maxNanosPerClient;
    }

    public static class Builder {

        private TaskProvider taskProvider;
        private int numIterationsPerThread = 1000;
        private int numThreads = 1;

        public Builder setNumThreads(int numThreads) {
            this.numThreads = numThreads;
            return this;
        }

        public Builder setNumIterationsPerThread(int numIterationsPerThread) {
            this.numIterationsPerThread = numIterationsPerThread;
            return this;
        }

        public Builder setTaskClass(final Class<? extends Task> taskClass) {
            return setTaskProvider(new TaskProvider(taskClass));
        }

        public Builder setTaskProvider(TaskProvider taskProvider) {
            this.taskProvider = taskProvider;
            return this;
        }

        public Benchmark build() {
            return new Benchmark(this);
        }
    }
}