-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_base_test.go
More file actions
71 lines (59 loc) · 1.44 KB
/
csv_base_test.go
File metadata and controls
71 lines (59 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package easy_csv
import (
"encoding/csv"
"os"
"strconv"
"testing"
)
var dataItem = []string{
"SN0abcd",
"MAC00000",
"MeshID00",
"DevType00",
"Product00",
"Ver00",
"ProtoMQTT",
"TimeOL",
"TimeOffline",
"Phone",
"Email",
"STATE",
}
// Memo: 经测试验证200万行记录能够写进csv,并非之前规格定义说只能写入6万行,
// 只是用WPS打开文件会提示超过长度限制,在我电脑最终展示1048576行。用goland打开没有出现截断。
func TestCsvBase(t *testing.T) {
fileName := "./TestCsvBase.csv"
csvFile, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
t.Error(err)
return
}
defer csvFile.Close()
writer := csv.NewWriter(csvFile)
//构造数据测试写入
rowLimit := 2000000 //限制总行数
batch := 10000 //每批次写入行数
rowBatch := make([][]string, 0)
batchCount := 0 //批次计数
for i := 0; i < rowLimit; i++ {
num := i + 1
numStr := strconv.Itoa(num)
newDataItem := make([]string, len(dataItem))
for j := 0; j < len(dataItem); j++ {
newDataItem[j] = dataItem[j] + numStr
}
rowBatch = append(rowBatch, newDataItem)
if num%batch == 0 || num == rowLimit {
batchCount++
t.Logf("当前写入批次[%d],写入数据行数[%d]\n", batchCount, len(rowBatch))
err = writer.WriteAll(rowBatch)
if err != nil {
t.Error(err)
return
}
writer.Flush()
rowBatch = make([][]string, 0)
}
}
return
}