|
| 1 | +package functions; |
| 2 | + |
| 3 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 4 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 5 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 6 | + |
| 7 | +import com.google.cloud.datastore.Datastore; |
| 8 | +import com.google.cloud.datastore.DatastoreOptions; |
| 9 | +import com.google.cloud.datastore.Entity; |
| 10 | +import com.google.cloud.datastore.FullEntity; |
| 11 | +import com.google.cloud.datastore.IncompleteKey; |
| 12 | +import com.google.cloud.datastore.Key; |
| 13 | +import com.google.cloud.datastore.KeyFactory; |
| 14 | +import com.google.cloud.datastore.StringValue; |
| 15 | +import com.google.cloud.functions.BackgroundFunction; |
| 16 | +import com.google.cloud.functions.Context; |
| 17 | +import com.google.common.util.concurrent.Uninterruptibles; |
| 18 | +import functions.HelloWorld.PubSubMessage; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Random; |
| 23 | +import java.util.concurrent.Executors; |
| 24 | +import java.util.concurrent.ScheduledExecutorService; |
| 25 | +import java.util.concurrent.ScheduledFuture; |
| 26 | +import java.util.logging.Logger; |
| 27 | +import org.apache.commons.lang3.RandomStringUtils; |
| 28 | + |
| 29 | +public class HelloWorld implements BackgroundFunction<PubSubMessage> { |
| 30 | + private static final Logger logger = Logger.getLogger(HelloWorld.class.getName()); |
| 31 | + private static final int NANOS_PER_MICRO = 1_000; |
| 32 | + private static final int MICROS_PER_SECOND = 1_000_000; |
| 33 | + private static final int RANDOM_ID_BOUND = 10000; |
| 34 | + private static final int DURATION_MINUTES = 5; |
| 35 | + private static final int TARGET_QPS = 200; |
| 36 | + private static final int THREAD_COUNT = 25; |
| 37 | + |
| 38 | + private final Datastore datastore; |
| 39 | + private final String kind; |
| 40 | + // Create a Key factory to construct keys associated with this project. |
| 41 | + private final KeyFactory keyFactory; |
| 42 | + Random rand = new Random(); |
| 43 | + |
| 44 | + public HelloWorld() { |
| 45 | + DatastoreOptions.Builder builder = DatastoreOptions.newBuilder(); |
| 46 | + datastore = builder.build().getService(); |
| 47 | + logger.info(datastore.toString()); |
| 48 | + kind = "BillyFnGradient"; |
| 49 | + keyFactory = datastore.newKeyFactory().setKind(kind); |
| 50 | + } |
| 51 | + |
| 52 | + public void addEntity() { |
| 53 | + int numRows = 10; |
| 54 | + int rowHeight = (int) (RANDOM_ID_BOUND / numRows); |
| 55 | + int id = randomId(rand); |
| 56 | + int rowNumber = id / rowHeight; |
| 57 | + |
| 58 | + float p = (float) rowNumber / numRows; |
| 59 | + if (Math.random() <= p) { |
| 60 | + |
| 61 | + FullEntity<Key> fullEntity = |
| 62 | + Entity.newBuilder(keyFactory.newKey(id)) |
| 63 | + .set("description", getRandomStringValue()) |
| 64 | + .build(); |
| 65 | + // logger.info("writing entity" + fullEntity.getKey()); |
| 66 | + |
| 67 | + Entity entity = datastore.put(fullEntity); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private StringValue getRandomStringValue() { |
| 72 | + String randomString = |
| 73 | + RandomStringUtils.random( |
| 74 | + 1000, |
| 75 | + /* start= */ 0, |
| 76 | + /* end= */ 0, |
| 77 | + /* letters= */ true, |
| 78 | + /* numbers= */ true, |
| 79 | + /* chars= */ null); |
| 80 | + return StringValue.newBuilder(randomString).setExcludeFromIndexes(true).build(); |
| 81 | + } |
| 82 | + |
| 83 | + private int randomId(Random rand) { |
| 84 | + return rand.nextInt(RANDOM_ID_BOUND) + 1; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void accept(PubSubMessage message, Context context) { |
| 89 | + long periodMillis = 1_000 / TARGET_QPS * THREAD_COUNT; |
| 90 | + List<ScheduledFuture<?>> scheduledFutures = new ArrayList<>(); |
| 91 | + ScheduledExecutorService executor = Executors.newScheduledThreadPool(THREAD_COUNT); |
| 92 | + |
| 93 | + for (int i = 0; i < THREAD_COUNT; i++) { |
| 94 | + ScheduledFuture<?> writeFuture = |
| 95 | + executor.scheduleAtFixedRate( |
| 96 | + () -> { |
| 97 | + Random random = new Random(); |
| 98 | + try { |
| 99 | + addEntity(); |
| 100 | + } catch (Exception e) { |
| 101 | + // catch all exceptions to avoid stopping traffic generator |
| 102 | + logger.info("Error when sending message" + e.getMessage()); |
| 103 | + } |
| 104 | + }, |
| 105 | + 0, |
| 106 | + periodMillis, |
| 107 | + MILLISECONDS); |
| 108 | + scheduledFutures.add(writeFuture); |
| 109 | + } |
| 110 | + Uninterruptibles.sleepUninterruptibly(DURATION_MINUTES, MINUTES); |
| 111 | + scheduledFutures.forEach(future -> future.cancel(false)); |
| 112 | + executor.shutdown(); |
| 113 | + try { |
| 114 | + executor.awaitTermination(60, SECONDS); |
| 115 | + } catch (InterruptedException e) { |
| 116 | + throw new RuntimeException("Waiting termination when unexpectedly got interrupted", e); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** A message that is published by publishers and consumed by subscribers. */ |
| 121 | + public static class PubSubMessage { |
| 122 | + String data; |
| 123 | + Map<String, String> attributes; |
| 124 | + String messageId; |
| 125 | + String publishTime; |
| 126 | + } |
| 127 | +} |
0 commit comments