Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.loadtest; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.util.concurrent.RateLimiter; |
| 20 | import org.apache.commons.lang.math.RandomUtils; |
| 21 | import org.onlab.util.Tools; |
| 22 | import org.onosproject.cfg.ComponentConfigService; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 26 | import org.onosproject.store.service.StorageService; |
| 27 | import org.osgi.service.component.ComponentContext; |
| 28 | import org.osgi.service.component.annotations.Activate; |
| 29 | import org.osgi.service.component.annotations.Component; |
| 30 | import org.osgi.service.component.annotations.Deactivate; |
| 31 | import org.osgi.service.component.annotations.Modified; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 34 | import org.slf4j.Logger; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 35 | |
| 36 | import java.util.Dictionary; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 37 | import java.util.List; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 38 | import java.util.concurrent.Executors; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 39 | import java.util.concurrent.ScheduledExecutorService; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 40 | import java.util.concurrent.Semaphore; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 41 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 42 | import java.util.concurrent.atomic.AtomicBoolean; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 43 | import java.util.concurrent.atomic.AtomicInteger; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 44 | import java.util.concurrent.atomic.AtomicLong; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 45 | import java.util.stream.Collectors; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 46 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 47 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 48 | import static org.onlab.util.Tools.get; |
Ray Milkey | 88dd7e2 | 2018-10-24 10:04:03 -0700 | [diff] [blame] | 49 | import static org.onosproject.loadtest.OsgiPropertyConstants.RATE; |
| 50 | import static org.onosproject.loadtest.OsgiPropertyConstants.RATE_DEFAULT; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 51 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Simple application for load testing distributed consensus. |
| 55 | * <p> |
| 56 | * This application simply increments as {@link AsyncAtomicCounter} at a configurable rate. |
| 57 | */ |
Ray Milkey | 88dd7e2 | 2018-10-24 10:04:03 -0700 | [diff] [blame] | 58 | @Component( |
| 59 | immediate = true, |
| 60 | service = DistributedConsensusLoadTest.class, |
| 61 | property = { |
| 62 | RATE + ":Integer=" + RATE_DEFAULT |
| 63 | } |
| 64 | ) |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 65 | public class DistributedConsensusLoadTest { |
| 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 70 | protected ComponentConfigService configService; |
| 71 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 73 | protected StorageService storageService; |
| 74 | |
| 75 | private ApplicationId appId; |
| 76 | |
| 77 | private AtomicBoolean stopped = new AtomicBoolean(false); |
| 78 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 80 | protected CoreService coreService; |
| 81 | |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 82 | private static final int TOTAL_COUNTERS = 50; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 83 | |
Ray Milkey | 88dd7e2 | 2018-10-24 10:04:03 -0700 | [diff] [blame] | 84 | /** Total number of increments per second to the atomic counter. */ |
| 85 | protected int rate = RATE_DEFAULT; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 86 | |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 87 | private final AtomicLong previousReportTime = new AtomicLong(0); |
| 88 | private final AtomicLong previousCount = new AtomicLong(0); |
| 89 | private final AtomicInteger increments = new AtomicInteger(0); |
| 90 | private final List<AsyncAtomicCounter> counters = Lists.newArrayList(); |
| 91 | private final ScheduledExecutorService runner = Executors.newSingleThreadScheduledExecutor(); |
| 92 | private final ScheduledExecutorService reporter = Executors.newSingleThreadScheduledExecutor(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 93 | |
| 94 | @Activate |
| 95 | public void activate(ComponentContext context) { |
| 96 | configService.registerProperties(getClass()); |
| 97 | appId = coreService.registerApplication("org.onosproject.loadtest"); |
| 98 | log.info("Started with {}", appId); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 99 | for (int i = 0; i < TOTAL_COUNTERS; ++i) { |
Madan Jampani | d5714e0 | 2016-04-19 14:15:20 -0700 | [diff] [blame] | 100 | AsyncAtomicCounter counter = |
| 101 | storageService.getAsyncAtomicCounter(String.format("onos-app-loadtest-counter-%d", i)); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 102 | counters.add(counter); |
| 103 | } |
| 104 | reporter.scheduleWithFixedDelay(() -> { |
| 105 | Tools.allOf(counters.stream() |
| 106 | .map(AsyncAtomicCounter::get) |
| 107 | .collect(Collectors.toList())) |
| 108 | .whenComplete((r, e) -> { |
| 109 | if (e == null) { |
| 110 | long newCount = r.stream().reduce(Long::sum).get(); |
| 111 | long currentTime = System.currentTimeMillis(); |
| 112 | long delta = currentTime - previousReportTime.getAndSet(currentTime); |
| 113 | long rate = (newCount - previousCount.getAndSet(newCount)) * 1000 / delta; |
| 114 | log.info("{} updates per second", rate); |
| 115 | } else { |
| 116 | log.warn(e.getMessage()); |
| 117 | } |
| 118 | }); |
| 119 | }, 5, 5, TimeUnit.SECONDS); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 120 | modified(null); |
| 121 | } |
| 122 | |
| 123 | private void startTest() { |
| 124 | stopped.set(false); |
| 125 | RateLimiter limiter = RateLimiter.create(rate); |
| 126 | Semaphore s = new Semaphore(100); |
| 127 | while (!stopped.get()) { |
| 128 | limiter.acquire(); |
| 129 | s.acquireUninterruptibly(); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 130 | counters.get(RandomUtils.nextInt(TOTAL_COUNTERS)).incrementAndGet().whenComplete((r, e) -> { |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 131 | s.release(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 132 | if (e == null) { |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 133 | increments.incrementAndGet(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 134 | } |
| 135 | }); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | private void stopTest() { |
| 140 | stopped.set(true); |
| 141 | } |
| 142 | |
| 143 | @Deactivate |
| 144 | public void deactivate(ComponentContext context) { |
| 145 | configService.unregisterProperties(getClass(), false); |
| 146 | stopTest(); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 147 | runner.shutdown(); |
| 148 | reporter.shutdown(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 149 | log.info("Stopped"); |
| 150 | } |
| 151 | |
| 152 | @Modified |
| 153 | public void modified(ComponentContext context) { |
Ray Milkey | 88dd7e2 | 2018-10-24 10:04:03 -0700 | [diff] [blame] | 154 | int newRate = RATE_DEFAULT; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 155 | if (context != null) { |
| 156 | Dictionary properties = context.getProperties(); |
| 157 | try { |
Ray Milkey | 88dd7e2 | 2018-10-24 10:04:03 -0700 | [diff] [blame] | 158 | String s = get(properties, RATE); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 159 | newRate = isNullOrEmpty(s) |
| 160 | ? rate : Integer.parseInt(s.trim()); |
| 161 | } catch (Exception e) { |
| 162 | return; |
| 163 | } |
| 164 | } |
| 165 | if (newRate != rate) { |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 166 | log.info("Per node rate changed to {}", newRate); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 167 | rate = newRate; |
| 168 | stopTest(); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 169 | runner.execute(this::startTest); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |