-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple-web.go
62 lines (45 loc) · 1.23 KB
/
simple-web.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"time"
)
// PostResponseNonConcurrent structure to represent the response body
type PostResponseNonConcurrent struct {
UserID int `json:"userId"`
ID int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
// NonConcurrentCall method loops over 1-100 IDs to make REST API call in a non concurrent way
func NonConcurrentCall() {
fmt.Println("--------------NON Concurrent Call--------------")
url := "https://jsonplaceholder.typicode.com/posts/"
var requestURL string
// Check for the time taken for each call
start := time.Now()
//Loop from post ID 1 to 100
for i := 1; i <= 10; i++ {
requestURL = url + strconv.Itoa(i)
// Send the GET request
resp, err := http.Get(requestURL)
if err != nil {
// Some error occured
log.Panicln(err)
}
defer resp.Body.Close()
var postResponse PostResponseNonConcurrent
if err := json.NewDecoder(resp.Body).Decode(&postResponse); err != nil {
log.Fatalln(err)
}
fmt.Println(postResponse)
fmt.Printf("---------------------------------\n")
}
// End of the time
elapsed := time.Since(start)
// Execution time: ~2 sec
fmt.Printf("Execution time: %s\n", elapsed)
}