Skip to main content
deleted 79 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Golang concurrent HttpHTTP request

I am new to Golang.

I wanted to test the performance of concurrent http request in Go against node.js.

Here's the Go code for that:

package main

import (
  "fmt"
  "time"
  "strconv"
  "net/http"
)

var responseCounter = 0
var requestCounter = 0
var count=0
var ch = make(chan int)

func sendRequest(){
    go func() {
        requestCounter++
        url := "https://www.google.co.in/#q=search_" +strconv.Itoa(requestCounter)
        resp, err := http.Get(url)
        if err != nil {
            fmt.Printf("\nError",err)
        }
        defer resp.Body.Close()
        count++
        ch <- count
        sendRequest()
    }()
}

func main() {
    
    for i := 1; i<100; i++{
        sendRequest()
    }
    
    for {
        select {
            case r := <-ch:
                fmt.Printf("\nChannel ",r)
            case <-time.After(50 * time.Millisecond):
                //sendRequest()
        }
    }
}

When I run this code the CPU usage goes very high  (around 90%).

  Is there something wrong with this code.? Have I used the Goroutines correctly?

Thanks in advance.

Golang concurrent Http request

I am new to Golang.

I wanted to test the performance of concurrent http request in Go against node.js.

Here's the Go code for that:

package main

import (
  "fmt"
  "time"
  "strconv"
  "net/http"
)

var responseCounter = 0
var requestCounter = 0
var count=0
var ch = make(chan int)

func sendRequest(){
    go func() {
        requestCounter++
        url := "https://www.google.co.in/#q=search_" +strconv.Itoa(requestCounter)
        resp, err := http.Get(url)
        if err != nil {
            fmt.Printf("\nError",err)
        }
        defer resp.Body.Close()
        count++
        ch <- count
        sendRequest()
    }()
}

func main() {
    
    for i := 1; i<100; i++{
        sendRequest()
    }
    
    for {
        select {
            case r := <-ch:
                fmt.Printf("\nChannel ",r)
            case <-time.After(50 * time.Millisecond):
                //sendRequest()
        }
    }
}

When I run this code the CPU usage goes very high(around 90%).

  Is there something wrong with this code. Have I used the Goroutines correctly?

Thanks in advance.

Golang concurrent HTTP request

I wanted to test the performance of concurrent http request in Go against node.js:

package main

import (
  "fmt"
  "time"
  "strconv"
  "net/http"
)

var responseCounter = 0
var requestCounter = 0
var count=0
var ch = make(chan int)

func sendRequest(){
    go func() {
        requestCounter++
        url := "https://www.google.co.in/#q=search_" +strconv.Itoa(requestCounter)
        resp, err := http.Get(url)
        if err != nil {
            fmt.Printf("\nError",err)
        }
        defer resp.Body.Close()
        count++
        ch <- count
        sendRequest()
    }()
}

func main() {
    
    for i := 1; i<100; i++{
        sendRequest()
    }
    
    for {
        select {
            case r := <-ch:
                fmt.Printf("\nChannel ",r)
            case <-time.After(50 * time.Millisecond):
                //sendRequest()
        }
    }
}

When I run this code the CPU usage goes very high  (around 90%). Is there something wrong with this code? Have I used the Goroutines correctly?

edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Source Link
Sarita
  • 129
  • 1
  • 2

Golang concurrent Http request

I am new to Golang.

I wanted to test the performance of concurrent http request in Go against node.js.

Here's the Go code for that:

package main

import (
  "fmt"
  "time"
  "strconv"
  "net/http"
)

var responseCounter = 0
var requestCounter = 0
var count=0
var ch = make(chan int)

func sendRequest(){
    go func() {
        requestCounter++
        url := "https://www.google.co.in/#q=search_" +strconv.Itoa(requestCounter)
        resp, err := http.Get(url)
        if err != nil {
            fmt.Printf("\nError",err)
        }
        defer resp.Body.Close()
        count++
        ch <- count
        sendRequest()
    }()
}

func main() {
    
    for i := 1; i<100; i++{
        sendRequest()
    }
    
    for {
        select {
            case r := <-ch:
                fmt.Printf("\nChannel ",r)
            case <-time.After(50 * time.Millisecond):
                //sendRequest()
        }
    }
}

When I run this code the CPU usage goes very high(around 90%).

Is there something wrong with this code. Have I used the Goroutines correctly?

Thanks in advance.