Skip to content

Commit

Permalink
refactor: replace encoding/json with jsoniter
Browse files Browse the repository at this point in the history
  • Loading branch information
cokemine committed Oct 20, 2021
1 parent d828ba9 commit e1b64e8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
12 changes: 7 additions & 5 deletions cmd/client/client.go
@@ -1,10 +1,10 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"github.com/cokemine/ServerStatus-goclient/pkg/status"
jsoniter "github.com/json-iterator/go"
"log"
"net"
"os"
Expand All @@ -23,16 +23,18 @@ var (
isVnstat = flag.Bool("vnstat", false, "Use vnstat for traffic statistics, linux only")
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

type serverStatus struct {
Uptime uint64 `json:"uptime"`
Load json.Number `json:"load"`
Load jsoniter.Number `json:"load"`
MemoryTotal uint64 `json:"memory_total"`
MemoryUsed uint64 `json:"memory_used"`
SwapTotal uint64 `json:"swap_total"`
SwapUsed uint64 `json:"swap_used"`
HddTotal uint64 `json:"hdd_total"`
HddUsed uint64 `json:"hdd_used"`
CPU json.Number `json:"cpu"`
CPU jsoniter.Number `json:"cpu"`
NetworkTx uint64 `json:"network_tx"`
NetworkRx uint64 `json:"network_rx"`
NetworkIn uint64 `json:"network_in"`
Expand Down Expand Up @@ -102,8 +104,8 @@ func connect() {
hddTotal, hddUsed := status.Disk(INTERVAL)
uptime := status.Uptime()
load := status.Load()
item.CPU = json.Number(fmt.Sprintf("%.1f", CPU))
item.Load = json.Number(fmt.Sprintf("%.2f", load))
item.CPU = jsoniter.Number(fmt.Sprintf("%.1f", CPU))
item.Load = jsoniter.Number(fmt.Sprintf("%.2f", load))
item.Uptime = uptime
item.MemoryTotal = memoryTotal
item.MemoryUsed = memoryUsed
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Expand Up @@ -7,4 +7,7 @@ retract (
v1.1.3
)

require github.com/shirou/gopsutil/v3 v3.21.9
require (
github.com/json-iterator/go v1.1.12
github.com/shirou/gopsutil/v3 v3.21.9
)
13 changes: 10 additions & 3 deletions go.sum
@@ -1,16 +1,23 @@
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY=
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shirou/gopsutil/v3 v3.21.8 h1:nKct+uP0TV8DjjNiHanKf8SAuub+GNsbrOtM9Nl9biA=
github.com/shirou/gopsutil/v3 v3.21.8/go.mod h1:YWp/H8Qs5fVmf17v7JNZzA0mPJ+mS2e9JdiUF9LlKzQ=
github.com/shirou/gopsutil/v3 v3.21.9 h1:Vn4MUz2uXhqLSiCbGFRc0DILbMVLAY92DSkT8bsYrHg=
github.com/shirou/gopsutil/v3 v3.21.9/go.mod h1:YWp/H8Qs5fVmf17v7JNZzA0mPJ+mS2e9JdiUF9LlKzQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tklauser/go-sysconf v0.3.9 h1:JeUVdAOWhhxVcU6Eqr/ATFHgXk/mmiItdKeJPev3vTo=
Expand Down

0 comments on commit e1b64e8

Please sign in to comment.