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; |
| 49 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Simple application for load testing distributed consensus. |
| 53 | * <p> |
| 54 | * This application simply increments as {@link AsyncAtomicCounter} at a configurable rate. |
| 55 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 56 | @Component(immediate = true, service = DistributedConsensusLoadTest.class) |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 57 | public class DistributedConsensusLoadTest { |
| 58 | |
| 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 62 | protected ComponentConfigService configService; |
| 63 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 65 | protected StorageService storageService; |
| 66 | |
| 67 | private ApplicationId appId; |
| 68 | |
| 69 | private AtomicBoolean stopped = new AtomicBoolean(false); |
| 70 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 72 | protected CoreService coreService; |
| 73 | |
| 74 | private static final int DEFAULT_RATE = 100; |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 75 | private static final int TOTAL_COUNTERS = 50; |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 76 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 77 | //@Property(name = "rate", intValue = DEFAULT_RATE, |
| 78 | // label = "Total number of increments per second to the atomic counter") |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 79 | protected int rate = 0; |
| 80 | |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 81 | private final AtomicLong previousReportTime = new AtomicLong(0); |
| 82 | private final AtomicLong previousCount = new AtomicLong(0); |
| 83 | private final AtomicInteger increments = new AtomicInteger(0); |
| 84 | private final List<AsyncAtomicCounter> counters = Lists.newArrayList(); |
| 85 | private final ScheduledExecutorService runner = Executors.newSingleThreadScheduledExecutor(); |
| 86 | private final ScheduledExecutorService reporter = Executors.newSingleThreadScheduledExecutor(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 87 | |
| 88 | @Activate |
| 89 | public void activate(ComponentContext context) { |
| 90 | configService.registerProperties(getClass()); |
| 91 | appId = coreService.registerApplication("org.onosproject.loadtest"); |
| 92 | log.info("Started with {}", appId); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 93 | for (int i = 0; i < TOTAL_COUNTERS; ++i) { |
Madan Jampani | d5714e0 | 2016-04-19 14:15:20 -0700 | [diff] [blame] | 94 | AsyncAtomicCounter counter = |
| 95 | storageService.getAsyncAtomicCounter(String.format("onos-app-loadtest-counter-%d", i)); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 96 | counters.add(counter); |
| 97 | } |
| 98 | reporter.scheduleWithFixedDelay(() -> { |
| 99 | Tools.allOf(counters.stream() |
| 100 | .map(AsyncAtomicCounter::get) |
| 101 | .collect(Collectors.toList())) |
| 102 | .whenComplete((r, e) -> { |
| 103 | if (e == null) { |
| 104 | long newCount = r.stream().reduce(Long::sum).get(); |
| 105 | long currentTime = System.currentTimeMillis(); |
| 106 | long delta = currentTime - previousReportTime.getAndSet(currentTime); |
| 107 | long rate = (newCount - previousCount.getAndSet(newCount)) * 1000 / delta; |
| 108 | log.info("{} updates per second", rate); |
| 109 | } else { |
| 110 | log.warn(e.getMessage()); |
| 111 | } |
| 112 | }); |
| 113 | }, 5, 5, TimeUnit.SECONDS); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 114 | modified(null); |
| 115 | } |
| 116 | |
| 117 | private void startTest() { |
| 118 | stopped.set(false); |
| 119 | RateLimiter limiter = RateLimiter.create(rate); |
| 120 | Semaphore s = new Semaphore(100); |
| 121 | while (!stopped.get()) { |
| 122 | limiter.acquire(); |
| 123 | s.acquireUninterruptibly(); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 124 | counters.get(RandomUtils.nextInt(TOTAL_COUNTERS)).incrementAndGet().whenComplete((r, e) -> { |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 125 | s.release(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 126 | if (e == null) { |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 127 | increments.incrementAndGet(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 128 | } |
| 129 | }); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private void stopTest() { |
| 134 | stopped.set(true); |
| 135 | } |
| 136 | |
| 137 | @Deactivate |
| 138 | public void deactivate(ComponentContext context) { |
| 139 | configService.unregisterProperties(getClass(), false); |
| 140 | stopTest(); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 141 | runner.shutdown(); |
| 142 | reporter.shutdown(); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 143 | log.info("Stopped"); |
| 144 | } |
| 145 | |
| 146 | @Modified |
| 147 | public void modified(ComponentContext context) { |
| 148 | int newRate = DEFAULT_RATE; |
| 149 | if (context != null) { |
| 150 | Dictionary properties = context.getProperties(); |
| 151 | try { |
| 152 | String s = get(properties, "rate"); |
| 153 | newRate = isNullOrEmpty(s) |
| 154 | ? rate : Integer.parseInt(s.trim()); |
| 155 | } catch (Exception e) { |
| 156 | return; |
| 157 | } |
| 158 | } |
| 159 | if (newRate != rate) { |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 160 | log.info("Per node rate changed to {}", newRate); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 161 | rate = newRate; |
| 162 | stopTest(); |
Madan Jampani | ee3736e | 2016-03-29 23:40:39 -0700 | [diff] [blame] | 163 | runner.execute(this::startTest); |
Madan Jampani | 5551f52 | 2016-02-23 18:27:19 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |