github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/test/fnlb-test-harness/primes-func/func.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "strconv" 7 "strings" 8 "time" 9 ) 10 11 // return list of primes less than N 12 // source: http://stackoverflow.com/a/21923233 13 func sieveOfEratosthenes(N int) (primes []int) { 14 b := make([]bool, N) 15 for i := 2; i < N; i++ { 16 if b[i] == true { 17 continue 18 } 19 primes = append(primes, i) 20 for k := i * i; k < N; k += i { 21 b[k] = true 22 } 23 } 24 return 25 } 26 27 func main() { 28 start := time.Now() 29 maxPrime := 1000000 30 numLoops := 1 31 32 // Parse the query string 33 s := strings.Split(os.Getenv("REQUEST_URL"), "?") 34 if len(s) > 1 { 35 for _, pair := range strings.Split(s[1], "&") { 36 kv := strings.Split(pair, "=") 37 if len(kv) > 1 { 38 key, value := kv[0], kv[1] 39 if key == "max" { 40 maxPrime, _ = strconv.Atoi(value) 41 } 42 if key == "loops" { 43 numLoops, _ = strconv.Atoi(value) 44 } 45 } 46 } 47 } 48 49 // Repeat the calculation of primes simply to give the CPU more work to do without consuming additional memory 50 for i := 0; i < numLoops; i++ { 51 primes := sieveOfEratosthenes(maxPrime) 52 _ = primes 53 if i == numLoops-1 { 54 //fmt.Printf("Highest three primes: %d %d %d\n", primes[len(primes) - 1], primes[len(primes) - 2], primes[len(primes) - 3]) 55 } 56 } 57 fmt.Printf("{\"durationSeconds\": %f, \"hostname\": \"%s\", \"max\": %d, \"loops\": %d}", time.Since(start).Seconds(), os.Getenv("HOSTNAME"), maxPrime, numLoops) 58 }