- 优化excel.Obj2Excel性能,不使用Struct2FlatMap

This commit is contained in:
gazebo
2019-05-24 16:44:34 +08:00
parent 551aa74d5c
commit cc43c1429f
2 changed files with 121 additions and 12 deletions

View File

@@ -1,9 +1,47 @@
package excel
import (
"reflect"
"testing"
)
type XXXX struct {
ID int64 `orm:"column(id)" json:"id"`
VendorOrderID string `orm:"column(vendor_order_id);size(48)" json:"vendorOrderID"`
VendorOrderID2 string `orm:"column(vendor_order_id2);size(48);index" json:"vendorOrderID2"`
VendorID int `orm:"column(vendor_id)" json:"vendorID"`
VendorStoreID string `orm:"column(vendor_store_id);size(48)" json:"vendorStoreID"`
StoreID int `orm:"column(store_id)" json:"storeID"` // 外部系统里记录的 jxstoreid
JxStoreID int `orm:"column(jx_store_id)" json:"jxStoreID"` // 根据VendorStoreID在本地系统里查询出来的 jxstoreid
StoreName string `orm:"size(64)" json:"storeName"`
ShopPrice int64 `json:"shopPrice"` // 单位为分 门店标价
SalePrice int64 `json:"salePrice"` // 单位为分 售卖价
ActualPayPrice int64 `json:"actualPayPrice"` // 单位为分 顾客实际支付
Weight int `json:"weight"` // 单位为克
ConsigneeName string `orm:"size(32)" json:"consigneeName"`
ConsigneeMobile string `orm:"size(32)" json:"consigneeMobile"`
ConsigneeMobile2 string `orm:"size(32)" json:"consigneeMobile2"`
ConsigneeAddress string `orm:"size(255)" json:"consigneeAddress"`
CoordinateType int `json:"-"`
ConsigneeLng int `json:"-"` // 坐标 * 10的六次方
ConsigneeLat int `json:"-"` // 坐标 * 10的六次方
SkuCount int `json:"skuCount"` // 商品类别数量即有多少种商品注意在某些情况下相同SKU的商品由于售价不同也会当成不同商品在这个值里
GoodsCount int `json:"goodsCount"` // 商品个数
Status int `json:"status"` // 参见OrderStatus*相关的常量定义
VendorStatus string `orm:"size(255)" json:"vendorStatus"`
LockStatus int `json:"lockStatus"`
OrderSeq int `json:"orderSeq"` // 门店订单序号
BuyerComment string `orm:"size(255)" json:"buyerComment"`
BusinessType int `json:"businessType"`
CancelApplyReason string `orm:"size(255)" json:"-"` // ""表示没有申请不为null表示用户正在取消申请
VendorWaybillID string `orm:"column(vendor_waybill_id);size(48)" json:"vendorWaybillID"`
WaybillVendorID int `orm:"column(waybill_vendor_id)" json:"waybillVendorID"` // 表示当前承运商,-1表示还没有安排
DeliveryFlag int8 `json:"deliveryFlag"` // 第1位为1表示禁止调度器调度三方配送
DuplicatedCount int `json:"-"` // 重复新订单消息数这个一般不是由于消息重发造成的消息重发由OrderStatus过滤一般是业务逻辑造成的
OriginalData string `orm:"-" json:"-"` // 只是用于传递数据
Flag int8 `json:"flag"` //非运单调整相关的其它状态
}
func TestObj2Excel(t *testing.T) {
// kk := make([]*model.SkuName, 1)
// kk[0] = &model.SkuName{
@@ -24,3 +62,49 @@ func TestObj2Excel(t *testing.T) {
}
Obj2Excel([]*Obj2ExcelSheetConfig{cc})
}
func BenchmarkObj2Excel(b *testing.B) {
const sliceLen = 1000
oneData := &XXXX{}
cc := &Obj2ExcelSheetConfig{
Title: "Title",
CaptionList: nil,
}
elmType := reflect.TypeOf(oneData)
if elmType.Kind() == reflect.Ptr {
elmType = elmType.Elem()
}
for i := 0; i < elmType.NumField(); i++ {
if jsonTag := elmType.Field(i).Tag.Get("json"); jsonTag != "" && jsonTag != "-" {
cc.CaptionList = append(cc.CaptionList, jsonTag)
}
}
value := reflect.Indirect(reflect.ValueOf(oneData))
for i := 0; i < elmType.NumField(); i++ {
value2 := value.Field(i)
if value2.Kind() == reflect.String {
value2.SetString(elmType.Field(i).Name)
}
}
/*
data := make([]map[string]interface{}, sliceLen)
for k := range data {
data[k] = utils.Struct2MapByJson(oneData)
}
//*/
//*
data := make([]*XXXX, 1000)
for k := range data {
copied := *oneData
data[k] = &copied
}
//*/
cc.Data = data
for i := 0; i < b.N; i++ {
Obj2Excel([]*Obj2ExcelSheetConfig{cc})
}
// b.Log(utils.Format4Output(data, false))
}