aboutsummaryrefslogtreecommitdiffstats
path: root/testsuites/performance/performance-context-metrics/src/main/java/org/onap/policy/apex/plugins/context/metrics/ConcurrentContextMetrics.java
blob: 3d257f89a5b830da506d7b1bd1051f6602f7e748 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 * ================================================================================
 * 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.
 * 
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.plugins.context.metrics;

import static org.onap.policy.apex.context.parameters.DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS;
import static org.onap.policy.apex.context.parameters.LockManagerParameters.DEFAULT_LOCK_MANAGER_PLUGIN_CLASS;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;

import org.onap.policy.apex.context.parameters.ContextParameters;
import org.onap.policy.apex.context.parameters.DistributorParameters;
import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
import org.onap.policy.apex.context.test.locking.ConcurrentContext;
import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
import org.onap.policy.apex.context.test.utils.ConfigrationProviderImpl;
import org.onap.policy.apex.context.test.utils.ZooKeeperServerServiceProvider;
import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
import org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor;
import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor;
import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager;
import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
import org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * The Class concurrentContextMetrics tests concurrent use of context.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class ConcurrentContextMetrics {
    private static final int NUM_ARGS = 9;
    private static final int ARG_LABEL = 0;
    private static final int ARG_JVM_COUNT = 1;
    private static final int ARG_THREAD_COUNT = 2;
    private static final int ARG_ITERATIONS = 3;
    private static final int ARG_ARRAY_SIZE = 4;
    private static final int ARG_LOCK_TYPE = 5;
    private static final int ARG_ZOOKEEPER_ADDRESS = 6;
    private static final int ARG_ZOOKEEPER_PORT = 7;
    private static final int ARG_ZOOKEEPER_DIRECTORY = 8;

    // Logger for this class
    private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextMetrics.class);

    private String zookeeperAddress = null;
    private final ConfigrationProvider configrationProvider;
    private final File zookeeperDirectory;
    private final int zookeeperPort;

    /**
     * The main method.
     *
     * @param args the args
     * @throws Exception the exception
     */
    public static void main(final String[] args) throws Exception {
        if (args.length != NUM_ARGS) {
            System.err.println("Args: " + Arrays.toString(args));
            System.err.println(
                    "usage: testLabel jvmCount threadCount threadLoops longArraySize lockType zookeeperAddress zookeeperPort zookeeperDirectory");
            return;
        }

        final ConfigrationProvider configrationProvider =
                new ConfigrationProviderImpl(args[ARG_LABEL], Integer.valueOf(args[ARG_JVM_COUNT]),
                        Integer.valueOf(args[ARG_THREAD_COUNT]), Integer.valueOf(args[ARG_ITERATIONS]),
                        Integer.valueOf(args[ARG_ARRAY_SIZE]), Integer.valueOf(args[ARG_LOCK_TYPE]));

        final ConcurrentContextMetrics concurrentContextMetrics = new ConcurrentContextMetrics(configrationProvider,
                args[ARG_ZOOKEEPER_ADDRESS], Integer.valueOf(args[ARG_ZOOKEEPER_PORT]), args[ARG_ZOOKEEPER_DIRECTORY]);

        concurrentContextMetrics.concurrentContextMetricsJVMLocal();
        concurrentContextMetrics.concurrentContextMetricsCurator();
        concurrentContextMetrics.concurrentContextMetricsHazelcast();
        concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJVMHazelcastLock();
        concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJVMHazelcastlock();
        concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJVMCuratorLock();
        concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJVMCuratorLock();
    }

    /**
     * The Constructor.
     * 
     * @param configrationProvider
     * @param zookeeperAddress
     * @param zookeeperPort
     * @param zookeeperDirectory
     */
    public ConcurrentContextMetrics(final ConfigrationProvider configrationProvider, final String zookeeperAddress,
            final int zookeeperPort, final String zookeeperDirectory) {
        this.configrationProvider = configrationProvider;
        this.zookeeperAddress = zookeeperAddress;
        this.zookeeperPort = zookeeperPort;
        this.zookeeperDirectory = new File(zookeeperDirectory);
    }

    /**
     * Concurrent context metrics JVM local.
     *
     * @throws ApexModelException the apex model exception
     * @throws IOException the IO exception
     * @throws ApexException the apex exception
     */
    private void concurrentContextMetricsJVMLocal() throws ApexModelException, IOException, ApexException {
        if (configrationProvider.getJvmCount() != 1) {
            return;
        }

        LOGGER.debug("Running concurrentContextMetricsJVMLocalVarSet metrics . . .");

        final ContextParameters contextParameters = new ContextParameters();
        contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
        contextParameters.getLockManagerParameters().setPluginClass(DEFAULT_LOCK_MANAGER_PLUGIN_CLASS);
        runConcurrentContextMetrics("JVMLocal");

        LOGGER.debug("Ran concurrentContextMetricsJVMLocalVarSet metrics");
    }

    /**
     * Concurrent context metrics hazelcast.
     *
     * @throws IOException the IO exception
     * @throws ApexException the apex exception
     */
    private void concurrentContextMetricsHazelcast() throws IOException, ApexException {
        if (configrationProvider.getJvmCount() != 1) {
            return;
        }

        LOGGER.debug("Running concurrentContextMetricsHazelcast metrics . . .");

        final ContextParameters contextParameters = new ContextParameters();
        contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
        contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
        runConcurrentContextMetrics("Hazelcast");

        LOGGER.debug("Ran concurrentContextMetricsHazelcast metrics");
    }

    /**
     * Concurrent context metrics curator.
     *
     * @throws IOException the IO exception
     * @throws ApexException the apex exception
     */
    private void concurrentContextMetricsCurator() throws IOException, ApexException {
        if (configrationProvider.getJvmCount() != 1) {
            return;
        }

        LOGGER.debug("Running concurrentContextMetricsCurator metrics . . .");

        final ContextParameters contextParameters = new ContextParameters();
        contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);

        final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
        curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
        contextParameters.setLockManagerParameters(curatorParameters);
        curatorParameters.setZookeeperAddress(zookeeperAddress);

        runConcurrentContextMetrics("Curator");

        LOGGER.debug("Ran concurrentContextMetricsCurator metrics");
    }

    /**
     * Concurrent context metrics hazelcast multi JVM hazelcast lock.
     *
     * @throws IOException the IO exception
     * @throws ApexException the apex exception
     */
    private void concurrentContextMetricsHazelcastMultiJVMHazelcastLock() throws IOException, ApexException {
        LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics . . .");

        final ContextParameters contextParameters = new ContextParameters();
        final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
        distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
        contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
        runConcurrentContextMetrics("HazelcastMultiJVMHazelcastLock");

        LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics");
    }

    /**
     * Concurrent context metrics infinispan multi JVM hazelcastlock.
     *
     * @throws IOException the IO exception
     * @throws ApexException the apex exception
     */
    private void concurrentContextMetricsInfinispanMultiJVMHazelcastlock() throws IOException, ApexException {
        LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics . . .");

        final ContextParameters contextParameters = new ContextParameters();
        final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
        distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
        contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());

        final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
        contextParameters.setDistributorParameters(infinispanParameters);

        runConcurrentContextMetrics("InfinispanMultiJVMHazelcastlock");

        LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics");
    }

    /**
     * Concurrent context metrics infinispan multi JVM curator lock.
     *
     * @throws IOException the IO exception
     * @throws ApexException the apex exception
     * @throws InterruptedException
     */
    private void concurrentContextMetricsInfinispanMultiJVMCuratorLock()
            throws IOException, ApexException, InterruptedException {

        LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics . . .");

        final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider =
                new ZooKeeperServerServiceProvider(zookeeperDirectory, zookeeperAddress, zookeeperPort);
        try {
            zooKeeperServerServiceProvider.startZookeeperServer();
            final ContextParameters contextParameters = new ContextParameters();
            final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
            distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());

            final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
            curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
            contextParameters.setLockManagerParameters(curatorParameters);
            curatorParameters.setZookeeperAddress(zookeeperAddress);

            final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
            contextParameters.setDistributorParameters(infinispanParameters);

            runConcurrentContextMetrics("InfinispanMultiJVMCuratorLock");
        } finally {
            zooKeeperServerServiceProvider.stopZookeeperServer();
        }
        LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics");
    }

    /**
     * Concurrent context metrics hazelcast multi JVM curator lock.
     *
     * @throws IOException the IO exception
     * @throws ApexException the apex exception
     * @throws InterruptedException
     */
    private void concurrentContextMetricsHazelcastMultiJVMCuratorLock()
            throws IOException, ApexException, InterruptedException {
        LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics . . .");

        final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider =
                new ZooKeeperServerServiceProvider(zookeeperDirectory, zookeeperAddress, zookeeperPort);

        try {
            zooKeeperServerServiceProvider.startZookeeperServer();
            final ContextParameters contextParameters = new ContextParameters();
            final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
            distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());

            final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
            curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
            contextParameters.setLockManagerParameters(curatorParameters);
            curatorParameters.setZookeeperAddress(zookeeperAddress);

            runConcurrentContextMetrics("HazelcastMultiJVMCuratorLock");
        } finally {
            zooKeeperServerServiceProvider.stopZookeeperServer();
        }
        LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics");
    }

    /**
     * Run concurrent context metrics.
     *
     * @param testName the test name
     * @throws IOException the IO exception
     * @throws ApexException the apex exception
     */
    private void runConcurrentContextMetrics(final String testName) throws IOException, ApexException {
        final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);

        LOGGER.info("Running {} ...", testName);
        final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();

        long total = 0;
        for (final Entry<String, TestContextLongItem> entry : result.entrySet()) {
            LOGGER.trace("Album key: {}, value: {}", entry.getKey(), entry.getValue());
            total += entry.getValue().getLongValue();
        }
        LOGGER.info("Album total value after execution: {}", total);

        LOGGER.info("Completed {} ...", testName);
    }


}