tpphu / golang-training

Golang for Backend Developer with Nordic Coder
https://nordiccoder.com/khoa-hoc/golang-for-backend-dev/
132 stars 50 forks source link

Viết một service cung cấp id tăng dần với Golang #16

Closed tpphu closed 5 years ago

tpphu commented 5 years ago

Thiết kết một API ví dụ:

curl http://localhost:8081/get-increment-id

Mỗi lần gọi nó sẽ trả về: 1,2,3,4,5....

hiepndd commented 5 years ago

Hi anh @tpphu , em suy nghi ra cach nay khong biet co dung voi expect cua anh chua

package main

import (
    "sync/atomic"
    "time"

    "github.com/gin-gonic/gin"
)

var autoIncrement uint64

// AutoIncrement is func handle auto_increment
func AutoIncrement() uint64 {
    atomic.AddUint64(&autoIncrement, 1)
    time.Sleep(time.Millisecond)
    return autoIncrement
}

func main() {
    r := gin.Default()
    r.GET("/get-increment-id", func(c *gin.Context) {
        result := AutoIncrement()
        c.JSON(200, gin.H{
            "ID": result,
        })
    })
    r.Run()
}
hiepndd commented 5 years ago

Cai nay la cai log cua em khi em chay k6

   duration: 5s, iterations: -
         vus: 2,  max: 2

time="2019-04-03T18:46:46Z" level=info msg=Running i=1210 t=913.1934msstarting
time="2019-04-03T18:46:47Z" level=info msg=Running i=2598 t=1.9134026s
time="2019-04-03T18:46:48Z" level=info msg=Running i=3953 t=2.9140246s
time="2019-04-03T18:46:49Z" level=info msg=Running i=5376 t=3.914072s
time="2019-04-03T18:46:50Z" level=info msg=Running i=6771 t=4.9133317s
time="2019-04-03T18:46:51Z" level=info msg="Test finished" i=6888 t=5.0003015s

    ✓ transaction time OK
    ✓ status was 200

    checks.....................: 100.00% ✓ 13776 ✗ 0  
    data_received..............: 922 kB  184 kB/s
    data_sent..................: 737 kB  147 kB/s
    http_req_blocked...........: avg=34.4µs  min=20.4µs  med=27.5µs   max=1.61ms  p(90)=41µs    p(95)=56.26µs 
    http_req_connecting........: avg=193ns   min=0s      med=0s       max=690µs   p(90)=0s      p(95)=0s      
    http_req_duration..........: avg=1.01ms  min=382µs   med=655.65µs max=8.56ms  p(90)=1.21ms  p(95)=4.44ms  
    http_req_receiving.........: avg=46.17µs min=17.4µs  med=33.6µs   max=792.9µs p(90)=77.5µs  p(95)=104.56µs
    http_req_sending...........: avg=36.87µs min=15.8µs  med=28.9µs   max=1.06ms  p(90)=51.03µs p(95)=73.86µs 
    http_req_tls_handshaking...: avg=0s      min=0s      med=0s       max=0s      p(90)=0s      p(95)=0s      
    http_req_waiting...........: avg=936.2µs min=324.1µs med=573.9µs  max=8.48ms  p(90)=1.08ms  p(95)=4.35ms  
    http_reqs..................: 6888    1377.516936/s
    iteration_duration.........: avg=1.36ms  min=608µs   med=991.2µs  max=14.79ms p(90)=1.78ms  p(95)=4.82ms  
    iterations.................: 6888    1377.516936/s
    vus........................: 2       min=2   max=2
    vus_max....................: 2       min=2   max=2
tpphu commented 5 years ago

Hay quá :), cảm ơn bạn Hiệp. Giờ mình làm tiếp bước, store persistent cái id đó vào db, nếu không thì mỗi lần restart service cái này nó sẽ bị mất giá trị và chạy lại từ đầu.

@hiepndd và các bạn khác nghĩ thử xem nha?