Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zengming00/go-qps
https://github.com/zengming00/go-qps
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/zengming00/go-qps
- Owner: zengming00
- License: mit
- Created: 2018-07-05T02:10:19.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-06T02:07:14.000Z (about 6 years ago)
- Last Synced: 2024-06-09T17:46:51.797Z (4 months ago)
- Language: Go
- Size: 296 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-qps
qps(Query Per Second), qph(Hour), qpm(Minute)![image](https://github.com/zengming00/go-qps/raw/master/screenshots/1.jpg)
```go
package mainimport (
"math/rand"
"net/http"
"time"mqps "github.com/zengming00/go-qps"
)func main() {
rand.Seed(time.Now().UnixNano())// Statistics every second, a total of 3600 data
qps := mqps.NewQP(time.Second, 3600)
for i := 0; i < 5; i++ {
go func() {
for {
// Call Count() on every goroutine that needs statistics
qps.Count()
time.Sleep(time.Millisecond * time.Duration((50 + rand.Intn(500))))
}
}()
}
// Add a route to get HTML, for example /qps
http.HandleFunc("/qps", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// Get the raw HTML, you can gzip it
s, err := qps.Show()
if err != nil {
panic(err)
}
w.Write([]byte(s))
})
// Add a route to get json report, The name is the same as getting the HTML routing, but you need to add the '_json' suffix
http.HandleFunc("/qps_json", func(w http.ResponseWriter, r *http.Request) {
// Get the json report
bts, err := qps.GetJson()
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(bts)
})
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
```