Skip to content

Commit e0b4f5c

Browse files
authored
Merge pull request #285 from wonderflow/jsonitor
use Jsonitor instead of encoding/json
2 parents 14f5e0b + 63d6475 commit e0b4f5c

File tree

91 files changed

+9844
-246
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+9844
-246
lines changed

cli/upgrade_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cli
33
import (
44
"archive/tar"
55
"compress/gzip"
6-
"encoding/json"
76
"fmt"
87
"io"
98
"io/ioutil"
@@ -18,6 +17,7 @@ import (
1817
"github.com/qiniu/log"
1918
"github.com/qiniu/logkit/utils"
2019

20+
"github.com/json-iterator/go"
2121
"github.com/labstack/echo"
2222
"github.com/stretchr/testify/assert"
2323
)
@@ -355,7 +355,7 @@ func (m *mockGithub) respFunction(c echo.Context, data map[string]interface{}) e
355355
c.Response().Header().Set(RateLimitReset, data[RateLimitReset].(string))
356356
c.Response().Header().Set(RateLimitRemaining, data[RateLimitRemaining].(string))
357357
c.Response().WriteHeader(data["statusCode"].(int))
358-
return json.NewEncoder(c.Response()).Encode(data["data"])
358+
return jsoniter.NewEncoder(c.Response()).Encode(data["data"])
359359
}
360360

361361
// 请求含有错误参数,该函数通过错误参数来构造不同的错误

mgr/api_metric_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package mgr
22

33
import (
4-
"encoding/json"
54
"net/http"
65

6+
"github.com/json-iterator/go"
77
"github.com/qiniu/logkit/metric"
88
"github.com/qiniu/logkit/utils"
99
"github.com/stretchr/testify/assert"
@@ -33,7 +33,7 @@ func metricAPITest(p *testParam) {
3333
respCode, respBody, err := makeRequest(url, http.MethodGet, []byte{})
3434
assert.NoError(t, err, string(respBody))
3535
assert.Equal(t, http.StatusOK, respCode)
36-
if err = json.Unmarshal(respBody, &got1); err != nil {
36+
if err = jsoniter.Unmarshal(respBody, &got1); err != nil {
3737
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
3838
}
3939
assert.Equal(t, metric.GetMetricUsages(), got1.Data)
@@ -43,7 +43,7 @@ func metricAPITest(p *testParam) {
4343
respCode, respBody, err = makeRequest(url, http.MethodGet, []byte{})
4444
assert.NoError(t, err, string(respBody))
4545
assert.Equal(t, http.StatusOK, respCode)
46-
if err = json.Unmarshal(respBody, &got2); err != nil {
46+
if err = jsoniter.Unmarshal(respBody, &got2); err != nil {
4747
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
4848
}
4949
assert.Equal(t, metric.GetMetricOptions(), got2.Data)
@@ -53,7 +53,7 @@ func metricAPITest(p *testParam) {
5353
respCode, respBody, err = makeRequest(url, http.MethodGet, []byte{})
5454
assert.NoError(t, err, string(respBody))
5555
assert.Equal(t, http.StatusOK, respCode)
56-
if err = json.Unmarshal(respBody, &got3); err != nil {
56+
if err = jsoniter.Unmarshal(respBody, &got3); err != nil {
5757
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
5858
}
5959
assert.Equal(t, metric.GetMetricTypeKey(), got3.Data)

mgr/api_parser_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package mgr
22

33
import (
4-
"encoding/json"
54
"net/http"
65

6+
"github.com/json-iterator/go"
77
conf2 "github.com/qiniu/logkit/conf"
88
"github.com/qiniu/logkit/parser"
99
"github.com/qiniu/logkit/sender"
@@ -24,15 +24,15 @@ func parserParseTest(p *testParam) {
2424
rawConf := conf2.MapConf{}
2525
rawConf[KeySampleLog] = parser.SampleLogs[parser.TypeRaw]
2626
rawConf[parser.KeyParserType] = parser.TypeRaw
27-
rawpst, err := json.Marshal(rawConf)
27+
rawpst, err := jsoniter.Marshal(rawConf)
2828
assert.NoError(t, err)
2929
url := "http://127.0.0.1" + rs.address + "/logkit/parser/parse"
3030
respCode, respBody, err := makeRequest(url, http.MethodPost, rawpst)
3131
assert.NoError(t, err, string(respBody))
3232
assert.Equal(t, http.StatusOK, respCode)
3333

3434
var got1 respParserRet
35-
err = json.Unmarshal(respBody, &got1)
35+
err = jsoniter.Unmarshal(respBody, &got1)
3636
assert.NoError(t, err, string(respBody))
3737
assert.Equal(t, 4, len(got1.Data.SamplePoints))
3838

@@ -41,13 +41,13 @@ func parserParseTest(p *testParam) {
4141
jsonConf := conf2.MapConf{}
4242
jsonConf[KeySampleLog] = parser.SampleLogs[parser.TypeJson]
4343
jsonConf[parser.KeyParserType] = parser.TypeJson
44-
rawpst, err = json.Marshal(jsonConf)
44+
rawpst, err = jsoniter.Marshal(jsonConf)
4545
assert.NoError(t, err)
4646
url = "http://127.0.0.1" + rs.address + "/logkit/parser/parse"
4747
respCode, respBody, err = makeRequest(url, http.MethodPost, rawpst)
4848
assert.NoError(t, err, string(respBody))
4949
assert.Equal(t, http.StatusOK, respCode)
50-
err = json.Unmarshal(respBody, &got2)
50+
err = jsoniter.Unmarshal(respBody, &got2)
5151
if err != nil {
5252
t.Error(err)
5353
}
@@ -64,13 +64,13 @@ func parserParseTest(p *testParam) {
6464
grokConf[KeySampleLog] = parser.SampleLogs[parser.TypeGrok]
6565
grokConf[parser.KeyParserType] = parser.TypeGrok
6666
grokConf[parser.KeyGrokPatterns] = "%{COMMON_LOG_FORMAT}"
67-
rawpst, err = json.Marshal(grokConf)
67+
rawpst, err = jsoniter.Marshal(grokConf)
6868
assert.NoError(t, err)
6969
url = "http://127.0.0.1" + rs.address + "/logkit/parser/parse"
7070
respCode, respBody, err = makeRequest(url, http.MethodPost, rawpst)
7171
assert.NoError(t, err, string(respBody))
7272
assert.Equal(t, http.StatusOK, respCode)
73-
err = json.Unmarshal(respBody, &got3)
73+
err = jsoniter.Unmarshal(respBody, &got3)
7474
if err != nil {
7575
t.Error(err)
7676
}
@@ -97,7 +97,7 @@ func parserAPITest(p *testParam) {
9797
respCode, respBody, err := makeRequest(url, http.MethodGet, []byte{})
9898
assert.NoError(t, err, string(respBody))
9999
assert.Equal(t, http.StatusOK, respCode)
100-
if err = json.Unmarshal(respBody, &got1); err != nil {
100+
if err = jsoniter.Unmarshal(respBody, &got1); err != nil {
101101
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
102102
}
103103
assert.Equal(t, parser.ModeUsages, got1.Data)
@@ -107,7 +107,7 @@ func parserAPITest(p *testParam) {
107107
respCode, respBody, err = makeRequest(url, http.MethodGet, []byte{})
108108
assert.NoError(t, err, string(respBody))
109109
assert.Equal(t, http.StatusOK, respCode)
110-
if err = json.Unmarshal(respBody, &got2); err != nil {
110+
if err = jsoniter.Unmarshal(respBody, &got2); err != nil {
111111
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
112112
}
113113
assert.Equal(t, parser.ModeKeyOptions, got2.Data)
@@ -117,7 +117,7 @@ func parserAPITest(p *testParam) {
117117
respCode, respBody, err = makeRequest(url, http.MethodGet, []byte{})
118118
assert.NoError(t, err, string(respBody))
119119
assert.Equal(t, http.StatusOK, respCode)
120-
if err = json.Unmarshal(respBody, &got3); err != nil {
120+
if err = jsoniter.Unmarshal(respBody, &got3); err != nil {
121121
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
122122
}
123123
assert.Equal(t, parser.SampleLogs, got3.Data)

mgr/api_reader_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package mgr
22

33
import (
4-
"encoding/json"
54
"net/http"
65

6+
"github.com/json-iterator/go"
77
"github.com/qiniu/logkit/reader"
88
"github.com/stretchr/testify/assert"
99
)
@@ -18,7 +18,7 @@ func readerAPITest(p *testParam) {
1818
respCode, respBody, err := makeRequest(url, http.MethodGet, []byte{})
1919
assert.NoError(t, err, string(respBody))
2020
assert.Equal(t, http.StatusOK, respCode)
21-
if err = json.Unmarshal(respBody, &got1); err != nil {
21+
if err = jsoniter.Unmarshal(respBody, &got1); err != nil {
2222
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
2323
}
2424
assert.Equal(t, reader.ModeUsages, got1.Data)
@@ -28,7 +28,7 @@ func readerAPITest(p *testParam) {
2828
respCode, respBody, err = makeRequest(url, http.MethodGet, []byte{})
2929
assert.NoError(t, err, string(respBody))
3030
assert.Equal(t, http.StatusOK, respCode)
31-
if err = json.Unmarshal(respBody, &got2); err != nil {
31+
if err = jsoniter.Unmarshal(respBody, &got2); err != nil {
3232
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
3333
}
3434
assert.Equal(t, reader.ModeKeyOptions, got2.Data)

mgr/api_sender_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package mgr
22

33
import (
4-
"encoding/json"
54
"net/http"
65

6+
"github.com/json-iterator/go"
77
"github.com/qiniu/logkit/sender"
88
"github.com/stretchr/testify/assert"
99
)
@@ -18,7 +18,7 @@ func senderAPITest(p *testParam) {
1818
respCode, respBody, err := makeRequest(url, http.MethodGet, []byte{})
1919
assert.NoError(t, err, string(respBody))
2020
assert.Equal(t, http.StatusOK, respCode)
21-
if err = json.Unmarshal(respBody, &got1); err != nil {
21+
if err = jsoniter.Unmarshal(respBody, &got1); err != nil {
2222
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
2323
}
2424
assert.Equal(t, sender.ModeUsages, got1.Data)
@@ -28,7 +28,7 @@ func senderAPITest(p *testParam) {
2828
respCode, respBody, err = makeRequest(url, http.MethodGet, []byte{})
2929
assert.NoError(t, err, string(respBody))
3030
assert.Equal(t, http.StatusOK, respCode)
31-
if err = json.Unmarshal(respBody, &got2); err != nil {
31+
if err = jsoniter.Unmarshal(respBody, &got2); err != nil {
3232
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
3333
}
3434
assert.Equal(t, sender.ModeKeyOptions, got2.Data)

mgr/api_transformer.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package mgr
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"net/http"
76

7+
"github.com/json-iterator/go"
88
"github.com/labstack/echo"
99
"github.com/qiniu/logkit/sender"
1010
"github.com/qiniu/logkit/transforms"
@@ -93,9 +93,9 @@ func (rs *RestService) PostTransform() echo.HandlerFunc {
9393
if rawLogs, ok = (reqConf[KeySampleLog]).(string); !ok {
9494
return RespError(c, http.StatusBadRequest, utils.ErrTransformTransform, fmt.Sprintf("missing param %s", KeySampleLog))
9595
}
96-
if jsonErr = json.Unmarshal([]byte(rawLogs), &singleData); jsonErr != nil {
96+
if jsonErr = jsoniter.Unmarshal([]byte(rawLogs), &singleData); jsonErr != nil {
9797
// may be multiple sample logs
98-
if jsonErr = json.Unmarshal([]byte(rawLogs), &data); jsonErr != nil {
98+
if jsonErr = jsoniter.Unmarshal([]byte(rawLogs), &data); jsonErr != nil {
9999
// invalid JSON, neither multiple sample logs nor single sample log
100100
return RespError(c, http.StatusBadRequest, utils.ErrTransformTransform, jsonErr.Error())
101101
}
@@ -107,10 +107,10 @@ func (rs *RestService) PostTransform() echo.HandlerFunc {
107107
trans = create()
108108
reqConf = convertWebTransformerConfig(reqConf)
109109
delete(reqConf, KeySampleLog)
110-
if bts, jsonErr = json.Marshal(reqConf); jsonErr != nil {
110+
if bts, jsonErr = jsoniter.Marshal(reqConf); jsonErr != nil {
111111
return RespError(c, http.StatusBadRequest, utils.ErrTransformTransform, jsonErr.Error())
112112
}
113-
if jsonErr = json.Unmarshal(bts, trans); jsonErr != nil {
113+
if jsonErr = jsoniter.Unmarshal(bts, trans); jsonErr != nil {
114114
return RespError(c, http.StatusBadRequest, utils.ErrTransformTransform, jsonErr.Error())
115115
}
116116
if trans, ok := trans.(transforms.Initialize); ok {

mgr/api_transformer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package mgr
22

33
import (
4-
"encoding/json"
54
"net/http"
65

6+
"github.com/json-iterator/go"
77
"github.com/qiniu/logkit/sender"
88
"github.com/qiniu/logkit/transforms"
99
_ "github.com/qiniu/logkit/transforms/date"
@@ -25,7 +25,7 @@ func transformerAPITest(p *testParam) {
2525
respCode, respBody, err := makeRequest(url, http.MethodGet, []byte{})
2626
assert.NoError(t, err, string(respBody))
2727
assert.Equal(t, http.StatusOK, respCode)
28-
if err = json.Unmarshal(respBody, &got1); err != nil {
28+
if err = jsoniter.Unmarshal(respBody, &got1); err != nil {
2929
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
3030
}
3131
assert.Equal(t, len(transforms.Transformers), len(got1.Data))
@@ -35,7 +35,7 @@ func transformerAPITest(p *testParam) {
3535
respCode, respBody, err = makeRequest(url, http.MethodGet, []byte{})
3636
assert.NoError(t, err, string(respBody))
3737
assert.Equal(t, http.StatusOK, respCode)
38-
if err = json.Unmarshal(respBody, &got2); err != nil {
38+
if err = jsoniter.Unmarshal(respBody, &got2); err != nil {
3939
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
4040
}
4141
assert.Equal(t, len(transforms.Transformers), len(got2.Data))
@@ -54,7 +54,7 @@ func transformerAPITest(p *testParam) {
5454
respCode, respBody, err = makeRequest(url, http.MethodPost, []byte(dateTransformerConfig))
5555
assert.NoError(t, err, string(respBody))
5656
assert.Equal(t, http.StatusOK, respCode)
57-
if err = json.Unmarshal(respBody, &got3); err != nil {
57+
if err = jsoniter.Unmarshal(respBody, &got3); err != nil {
5858
t.Fatalf("respBody %v unmarshal failed, error is %v", respBody, err)
5959
}
6060
exp := []sender.Data{{"ts": "2006-01-02T14:04:05Z"}}

mgr/cluster.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
package mgr
22

33
import (
4+
"bytes"
45
"errors"
6+
"fmt"
7+
"io/ioutil"
58
"net/http"
9+
"strings"
610
"sync"
7-
8-
"github.com/qiniu/logkit/utils"
9-
10-
"bytes"
11-
"encoding/json"
12-
"io/ioutil"
13-
14-
"fmt"
1511
"time"
1612

17-
"strings"
18-
13+
"github.com/json-iterator/go"
1914
"github.com/labstack/echo"
2015
"github.com/qiniu/log"
16+
"github.com/qiniu/logkit/utils"
2117
)
2218

2319
type ClusterConfig struct {
@@ -45,13 +41,13 @@ type Slave struct {
4541
type ClusterStatus struct {
4642
Status map[string]RunnerStatus `json:"status"`
4743
Tag string `json:"tag"`
48-
Err error `json:"error"`
44+
Err string `json:"error"`
4945
}
5046

5147
type SlaveConfig struct {
5248
Configs map[string]RunnerConfig `json:"configs"`
5349
Tag string `json:"tag"`
54-
Err error `json:"error"`
50+
Err string `json:"error"`
5551
}
5652

5753
type respRunnersNameList struct {
@@ -210,7 +206,7 @@ func (rs *RestService) GetClusterRunners() echo.HandlerFunc {
210206
log.Errorf("get slave(tag='%v', url='%v') runner name list failed, resp is %v, error is %v", v.Tag, v.Url, string(respBody), err.Error())
211207
return
212208
} else {
213-
if err = json.Unmarshal(respBody, &respRss); err != nil {
209+
if err = jsoniter.Unmarshal(respBody, &respRss); err != nil {
214210
log.Errorf("unmarshal slave(tag='%v', url='%v') runner name list failed, error is %v", v.Tag, v.Url, err.Error())
215211
} else {
216212
mutex.Lock()
@@ -258,10 +254,10 @@ func (rs *RestService) ClusterStatus() echo.HandlerFunc {
258254
respCode, respBody, err := executeToOneCluster(url, http.MethodGet, []byte{})
259255
if err != nil || respCode != http.StatusOK {
260256
errInfo := fmt.Errorf("%v %v", string(respBody), err)
261-
cs.Err = errInfo
257+
cs.Err = errInfo.Error()
262258
} else {
263-
if err = json.Unmarshal(respBody, &respRss); err != nil {
264-
cs.Err = fmt.Errorf("unmarshal query result error %v, body is %v", err, string(respBody))
259+
if err = jsoniter.Unmarshal(respBody, &respRss); err != nil {
260+
cs.Err = fmt.Sprintf("unmarshal query result error %v, body is %v", err, string(respBody))
265261
} else {
266262
cs.Status = respRss.Data
267263
}
@@ -301,7 +297,7 @@ func (rs *RestService) GetClusterConfig() echo.HandlerFunc {
301297
lastErrMsg = fmt.Sprintf("get slave(tag = '%v'', url = '%v') config failed resp is %v, error is %v", tag, url, string(respBody), err)
302298
continue
303299
} else {
304-
if err = json.Unmarshal(respBody, &respRss); err != nil {
300+
if err = jsoniter.Unmarshal(respBody, &respRss); err != nil {
305301
lastErrMsg = fmt.Sprintf("get slave(tag = '%v'', url = '%v') config unmarshal failed, resp is %v, error is %v", tag, url, string(respBody), err)
306302
continue
307303
} else {
@@ -346,10 +342,10 @@ func (rs *RestService) GetClusterConfigs() echo.HandlerFunc {
346342
respCode, respBody, err := executeToOneCluster(url, http.MethodGet, []byte{})
347343
if err != nil || respCode != http.StatusOK {
348344
errInfo := fmt.Errorf("%v %v", string(respBody), err)
349-
sc.Err = errInfo
345+
sc.Err = errInfo.Error()
350346
} else {
351-
if err = json.Unmarshal(respBody, &respRss); err != nil {
352-
sc.Err = fmt.Errorf("unmarshal query result error %v, body is %v", err, string(respBody))
347+
if err = jsoniter.Unmarshal(respBody, &respRss); err != nil {
348+
sc.Err = fmt.Sprintf("unmarshal query result error %v, body is %v", err, string(respBody))
353349
} else {
354350
sc.Configs = respRss.Data
355351
}
@@ -714,7 +710,7 @@ func registerOne(master, myhost, tag string) error {
714710
return errors.New("master host is not configed")
715711
}
716712
req := RegisterReq{Url: myhost, Tag: tag}
717-
data, err := json.Marshal(req)
713+
data, err := jsoniter.Marshal(req)
718714
if err != nil {
719715
return err
720716
}

0 commit comments

Comments
 (0)