Category: Dev

  • Generating random mobile numbers with Java 8

    Generating random mobile numbers with Java 8

    In a previous post I gave a way of generating random test mobile numbers (Ofcom approved!) using Scala iterators.

    Now that Java 8 gives us lambdas and streams I thought I would see what those generators might look like in Java.

    Here’s the mobile generator:

    Supplier<String> mobileSupplier = () -> String.format("447700900%03d", ThreadLocalRandom.current().nextInt(999));

    Nice, still a one-liner.

    Generating pseudo MAC addresses is a little bit more trouble. This supplier uses another nested stream to generate the random hex array.

    Supplier<String> macSupplier = () -> ThreadLocalRandom.current().ints(6, 0, 255)
            .mapToObj(i -> String.format("%02x", i))
            .collect(Collectors.joining(":"));

    You use these Supplier functions with the static generate method of Stream. Because the generators create infinite streams we use limit to just get a few values. Eg:

    import java.util.concurrent.ThreadLocalRandom;
    import java.util.function.Supplier;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    public class Generators {
    
        private static final Supplier<String> mobileSupplier = () -> String.format("447700900%03d", ThreadLocalRandom.current().nextInt(999));
    
        private static final Supplier<String> macSupplier = () -> ThreadLocalRandom.current().ints(6, 0, 255)
                .mapToObj(i -> String.format("%02x", i))
                .collect(Collectors.joining(":"));
    
        public static void main(String[] args) {
            System.out.println("Some mobile numbers:");
            Stream.generate(Generators.mobileSupplier)
                    .limit(5)
                    .forEach(System.out::println);
            System.out.println();
    
            System.out.println("Some (pseudo) mac addresses:");
            Stream.generate(Generators.macSupplier)
                    .limit(5)
                    .forEach(System.out::println);
        }
    }
    

     

  • Run multiple Gatling simulations with Maven

    Run multiple Gatling simulations with Maven

    Gatling is nice load testing framework that uses Scala, Akka and Netty. It’s so much better than JMeter. It’s pretty easy to get started with scenarios written in a nice Scala DSL and it produces useful reports too. It also has a Maven plugin so you can incorporate continuous performance testing in your builds.

    The trouble is if you list multiple scenarios in a simulation they will all run concurrently which is probably not what you want. You need to split your tests into separate Simulation classes and run them sequentially. The plugin documentation briefly describes a way of doing this using executions but the example is a little light.

    Here’s a Maven profile that when activated will run your simulations sequentially. Execute the tests with mvn test -Pperformance.

  • Generating random mobile numbers with Scala

    Generating random mobile numbers with Scala

    Sometimes in testing we need to generate random data. When it comes to generating mobile numbers it would be helpful if we could be sure they aren’t allocated to a real person, just in case we accidentally send 1000 text messages to random phone numbers. Luckily Ofcom has reserved ranges of numbers for dramatic use that are guaranteed not to be allocated to providers.

    Here’s a little random UK mobile number generator wrapped in an infinite-length iterator:

    val random = new util.Random
    val randomMobile = Iterator.continually("447700900" + f"${random.nextInt(999)}%03d")

    Bonus random MAC address generator

    Sometimes we need MAC addresses too. This will generate strings that look like MAC addresses (6 groups of hex digits) but aren’t really. Good enough for my purposes:

    val random = new util.Random
    val randomMac = Iterator.continually(Array.fill[String](6)(f"${random.nextInt(255)}%02x").mkString(":"))
  • Calculating distance with Scala’s foldLeft

    Calculating distance with Scala’s foldLeft

    I wanted a way to calculate the total distance of a GPS track. A track is basically just a list of lat,long pairs – represented in Scala by the following:

    Seq[(Double, Double)]

    One way to do this would be to iterate over the sequence, calculating the distance between points (using the haversine formula) and updating the sum in a variable but since this is Scala we can do it in a more functional way.

    According to the Scaladoc, foldLeft “Applies a binary operator to a start value and all elements of this list, going left to right.” The signature looks like this for List[A] (a List of type A):

    def foldLeft[B](z: B)(f: (B, A) ⇒ B): B

    The first parameter z is of type B, so it can be different from the list type. The second parameter is a function that takes a B and an A (one of the list items) and produces a B.

    The neat bit is this function iterates over the list and passes in each item to function f as the value of A. For the first item, z is passed in to f as the value of B. For each subsequent item the result of the previous call to f is used.

    The usual example you will find is to sum a list of integers or something similar, like this:

    list.foldLeft(0)((b, a) => b+a)

    This will return the sum of all the items items in a list.

    My use-case was slightly different. My list consisted of tuples and I needed to apply a function to each pair of tuples in the list and accumulate the sum of those results. This meant I needed to keep track of both the current element and the previous one during the folding.

    It was Matt Malone’s page of lots and lots of foldLeft examples that put me on the right track, specifically his example for Average which showed how to use a tuple as an accumulator. Here’s how I did it:

    As I said, points is just a sequence of lat,long pairs, eg:

    val points = List((51.168437004089355, -0.648922920227051), (51.16805076599121, -0.64918041229248), (51.16757869720459, -0.64995288848877))
    

    We first split the list into head and tail and use head and 0.0 as initial values. The folding function then produces a Tuple2[(Double, Double), Double]. The first item of the resultant tuple is the current list element and the second item is the sum of the current total and the result of the haversineDistance function (which itself takes as input the previous element and the current element).

    points match {
      case head :: tail => tail.foldLeft(head, 0.0)((accum, elem) => (elem, accum._2 + haversineDistance(accum._1, elem)))._2
      case Nil => 0.0
    }

    The pattern matching is used to handle the case of an empty list.

    Phew! This one line took me a while to figure out but it packs a lot of work into just a single line of code and shows how powerful Scala can be. Experimenting with the REPL was invaluable here.

    For completeness, here’s the haversine function which calculates the distance between two points on the Earth (3958.761 is the mean radius of the Earth in miles):

    def haversineDistance(pointA: (Double, Double), pointB: (Double, Double)): Double = {
      val deltaLat = math.toRadians(pointB._1 - pointA._1)
      val deltaLong = math.toRadians(pointB._2 - pointA._2)
      val a = math.pow(math.sin(deltaLat / 2), 2) + math.cos(math.toRadians(pointA._1)) * math.cos(math.toRadians(pointB._1)) * math.pow(math.sin(deltaLong / 2), 2)
      val greatCircleDistance = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
      3958.761 * greatCircleDistance
    }
    
  • MOTOCAL: Personalised motor racing calendars

    MOTOCAL: Personalised motor racing calendars

    I like just about any kind of motor sport. If it has wheels and an engine then I’m probably interested in watching it drive round in circles for a couple of hours. But there are a few race series I follow closely and try to watch on TV whenever I can: Formula One, MotoGP, Superbike.

    Wouldn’t it be good, I thought, if I could subscribe to a single calendar feed that showed all the races I was interested in (and none of the ones I wasn’t).

    So I built MOTOCAL.

    I’ve put all the hard work into collecting all the qualifying and race times so you don’t have to. Just click on each race series you want in your calendar and it will generate a URL that you can stick into your calendar application (Google, Apple, Outlook) and you will get a synchronised calendar that is automatically kept up to date.

    You can currently subscribe to any and all of the following:

    It’s pretty easy to add more calendars so leave a comment if you would like to see any other series.

  • Garmin Communicator WordPress Plugin

    Garmin Communicator WordPress Plugin

    I run the ridemap.org website which is a collection of motorbike routes and I wanted a way for people to download the routes to their GPS devices. Originally I was just going to provide a downloadable GPX file but then I found that Garmin have a browser plugin that allows data to be transferred between your Garmin GPS device and your computer.

    After getting it working on my website I decided to wrap it up in a WordPress plugin to make it easier to integrate in posts.

    You can get the code from GitHub.

    To use the plugin you will need to get a free Garmin site key from the Garmin Developer website. Then create a custom WordPress field called ‘gpx’ with your GPX data and insert the [[gpxconnect]] shortcode into your post. This will add a button to allow users to download the GPX data directly to their Garmin device.