Plaster
New
List
Login
java
default
anonymous
2018.03.12 11:25:10
public class BidGenerator { /** * Fraction of people/auctions which may be 'hot' sellers/bidders/auctions are 1 * over these values. */ private static final int HOT_AUCTION_RATIO = 100; private static final int HOT_BIDDER_RATIO = 100; /** * Generate and return a random bid with next available id. */ public static Bid nextBid( long eventId, Random random, long timestamp, GeneratorConfig config) { long auction; // Here P(bid will be for a hot auction) = 1 - 1/hotAuctionRatio. if (random.nextInt(config.getHotAuctionRatio()) > 0) { // Choose the first auction in the batch of last HOT_AUCTION_RATIO auctions. auction = (lastBase0AuctionId(eventId) / HOT_AUCTION_RATIO) * HOT_AUCTION_RATIO; } else { auction = nextBase0AuctionId(eventId, random, config); } auction += GeneratorConfig.FIRST_AUCTION_ID; long bidder; // Here P(bid will be by a hot bidder) = 1 - 1/hotBiddersRatio if (random.nextInt(config.getHotBiddersRatio()) > 0) { // Choose the second person (so hot bidders and hot sellers don't collide) in the batch of // last HOT_BIDDER_RATIO people. bidder = (lastBase0PersonId(eventId) / HOT_BIDDER_RATIO) * HOT_BIDDER_RATIO + 1; } else { bidder = nextBase0PersonId(eventId, random, config); } bidder += GeneratorConfig.FIRST_PERSON_ID; long price = PriceGenerator.nextPrice(random); int currentSize = 8 + 8 + 8 + 8; String extra = nextExtra(random, currentSize, config.getAvgBidByteSize()); return new Bid(auction, bidder, price, timestamp, extra); } }
Raw
Annotate
Repaste
Edit
Annotations
java
default
shinmera
2018.03.12 11:28:44
/** * Return the last valid auction id (ignoring FIRST_AUCTION_ID). Will be the current auction id if * due to generate an auction. */ public static long lastBase0AuctionId(long eventId) { long epoch = eventId / GeneratorConfig.PROPORTION_DENOMINATOR; long offset = eventId % GeneratorConfig.PROPORTION_DENOMINATOR; if (offset < GeneratorConfig.PERSON_PROPORTION) { // About to generate a person. // Go back to the last auction in the last epoch. epoch--; offset = GeneratorConfig.AUCTION_PROPORTION - 1; } else if (offset >= GeneratorConfig.PERSON_PROPORTION + GeneratorConfig.AUCTION_PROPORTION) { // About to generate a bid. // Go back to the last auction generated in this epoch. offset = GeneratorConfig.AUCTION_PROPORTION - 1; } else { // About to generate an auction. offset -= GeneratorConfig.PERSON_PROPORTION; } return epoch * GeneratorConfig.AUCTION_PROPORTION + offset; } /** * Return a random auction id (base 0). */ public static long nextBase0AuctionId( long nextEventId, Random random, GeneratorConfig config) { // Choose a random auction for any of those which are likely to still be in flight, // plus a few 'leads'. // Note that ideally we'd track non-expired auctions exactly, but that state // is difficult to split. long minAuction = Math.max( lastBase0AuctionId(nextEventId) - config.getNumInFlightAuctions(), 0); long maxAuction = lastBase0AuctionId(nextEventId); return minAuction + nextLong(random, maxAuction - minAuction + 1 + AUCTION_ID_LEAD); }
Raw
Repaste