-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (39 loc) · 854 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"crypto/rand"
"encoding/json"
"fmt"
"log"
"math/big"
"net/http"
"os"
"time"
"go.uber.org/automaxprocs/maxprocs"
)
func main() {
if os.Getenv("AUTOMAXPROC") == "true" {
if _, err := maxprocs.Set(); err != nil {
log.Fatal(err)
}
fmt.Println("AUTOMAXPROC enabled")
}
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
type response struct {
Duration string `json:"duration"`
Prime *big.Int `json:"prime"`
}
func handler(w http.ResponseWriter, r *http.Request) {
start := time.Now()
prime := big.NewInt(int64(time.Now().Nanosecond()))
for i := 0; i < 500; i++ {
nbr, err := rand.Prime(rand.Reader, 64)
if err != nil {
continue
}
prime = prime.Add(prime, nbr)
}
duration := time.Since(start)
json.NewEncoder(w).Encode(response{duration.String(), prime})
}