Skip to content

Commit cabe151

Browse files
authored
fix: fix download slow when 'kcl run' (#607)
* fix: fix download slow when 'kcl run' Signed-off-by: zongz <zongzhe1024@163.com> * fix: fix failed test case Signed-off-by: zongz <zongzhe1024@163.com> --------- Signed-off-by: zongz <zongzhe1024@163.com>
1 parent 8b50ab6 commit cabe151

File tree

10 files changed

+202
-10
lines changed

10 files changed

+202
-10
lines changed

pkg/client/issues_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"runtime"
99
"strings"
1010
"testing"
11+
"time"
1112

1213
"github.com/otiai10/copy"
1314
"github.com/stretchr/testify/assert"
@@ -624,3 +625,49 @@ func TestKpmIssue587(t *testing.T) {
624625

625626
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_download_with_git_dep", TestFunc: test_download_with_git_dep}})
626627
}
628+
629+
func TestKpmIssue605(t *testing.T) {
630+
testPath := "github.com/kcl-lang/kpm/issues/605"
631+
632+
test_run_with_exist_checksum := func(t *testing.T, kpmcli *KpmClient) {
633+
rootPath := getTestDir("issues")
634+
kfilePath := filepath.Join(rootPath, testPath, "pkg")
635+
var buf bytes.Buffer
636+
kpmcli.SetLogWriter(&buf)
637+
638+
// Run the first time,download the dependency to cache and generate the checksum
639+
_, err := kpmcli.Run(
640+
WithRunSource(
641+
&downloader.Source{
642+
Local: &downloader.Local{
643+
Path: kfilePath,
644+
},
645+
},
646+
),
647+
)
648+
649+
if err != nil {
650+
t.Fatal(err)
651+
}
652+
653+
// Run the second time, this time must be finished in 1 second
654+
startTime := time.Now()
655+
_, err = kpmcli.Run(
656+
WithRunSource(
657+
&downloader.Source{
658+
Local: &downloader.Local{
659+
Path: kfilePath,
660+
},
661+
},
662+
),
663+
)
664+
duration := time.Since(startTime)
665+
666+
if err != nil {
667+
t.Fatal(err)
668+
}
669+
assert.LessOrEqual(t, duration.Seconds(), 0.7, "The second run should finish in 1 second")
670+
}
671+
672+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_run_with_exist_checksum", TestFunc: test_run_with_exist_checksum}})
673+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# bug: kcl cli slow because of downloading checksums
2+
3+
https://github.com/kcl-lang/kpm/issues/605
4+
5+
## 1. Minimal reproduce step (Required)
6+
7+
```shell
8+
kcl mod init demo
9+
cd demo
10+
```
11+
12+
The `kcl.mod`:
13+
14+
```toml
15+
[package]
16+
name = "claims"
17+
edition = "v0.11.0"
18+
version = "0.0.1"
19+
20+
[dependencies]
21+
crossplane = "1.17.3"
22+
json_merge_patch = "0.1.1"
23+
```
24+
25+
The `kcl.mod.lock`
26+
27+
```toml
28+
[dependencies]
29+
[dependencies.crossplane]
30+
name = "crossplane"
31+
full_name = "crossplane_1.17.3"
32+
version = "1.17.3"
33+
sum = "8hx/1t6Gn0UZjaycAJIJ2Xj1R7vefZq7mMpAim21COc="
34+
reg = "ghcr.io"
35+
repo = "kcl-lang/crossplane"
36+
oci_tag = "1.17.3"
37+
[dependencies.json_merge_patch]
38+
name = "json_merge_patch"
39+
full_name = "json_merge_patch_0.1.1"
40+
version = "0.1.1"
41+
sum = "o1aamShk1L2MGjnN9u3IErRZ3xBNDxgmFxXsGVMt8Wk="
42+
reg = "ghcr.io"
43+
repo = "kcl-lang/json_merge_patch"
44+
oci_tag = "0.1.1"
45+
[dependencies.k8s]
46+
name = "k8s"
47+
full_name = "k8s_1.31.2"
48+
version = "1.31.2"
49+
sum = "xBZgPsnpVVyWBpahuPQHReeRx28eUHGFoaPeqbct+vs="
50+
reg = "ghcr.io"
51+
repo = "kcl-lang/k8s"
52+
oci_tag = "1.31.2"
53+
```
54+
55+
Compile the current project and record the time:
56+
57+
```
58+
time kcl run
59+
```
60+
61+
## 2. What did you expect to see? (Required)
62+
63+
```
64+
$ time kcl run
65+
The_first_kcl_program: Hello World!
66+
kcl run 0.03s user 0.04s system 10% cpu 0.648 total
67+
$ time kcl run
68+
The_first_kcl_program: Hello World!
69+
kcl run 0.03s user 0.01s system 114% cpu 0.037 total
70+
```
71+
72+
## 3. What did you see instead (Required)
73+
74+
```
75+
$ time kcl run
76+
The_first_kcl_program: Hello World!
77+
kcl run 0.09s user 0.08s system 2% cpu 6.023 total
78+
$ time kcl run
79+
The_first_kcl_program: Hello World!
80+
kcl run 0.07s user 0.05s system 1% cpu 7.077 total
81+
```
82+
83+
## 4. What is your KCL components version? (Required)
84+
85+
the main branch
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "claims"
3+
edition = "v0.11.0"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
crossplane = "1.17.3"
8+
json_merge_patch = "0.1.1"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[dependencies]
2+
[dependencies.crossplane]
3+
name = "crossplane"
4+
full_name = "crossplane_1.17.3"
5+
version = "1.17.3"
6+
sum = "8hx/1t6Gn0UZjaycAJIJ2Xj1R7vefZq7mMpAim21COc="
7+
reg = "ghcr.io"
8+
repo = "kcl-lang/crossplane"
9+
oci_tag = "1.17.3"
10+
[dependencies.json_merge_patch]
11+
name = "json_merge_patch"
12+
full_name = "json_merge_patch_0.1.1"
13+
version = "0.1.1"
14+
sum = "o1aamShk1L2MGjnN9u3IErRZ3xBNDxgmFxXsGVMt8Wk="
15+
reg = "ghcr.io"
16+
repo = "kcl-lang/json_merge_patch"
17+
oci_tag = "0.1.1"
18+
[dependencies.k8s]
19+
name = "k8s"
20+
full_name = "k8s_1.31.2"
21+
version = "1.31.2"
22+
sum = "xBZgPsnpVVyWBpahuPQHReeRx28eUHGFoaPeqbct+vs="
23+
reg = "ghcr.io"
24+
repo = "kcl-lang/k8s"
25+
oci_tag = "1.31.2"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program = 'Hello World!'
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
[dependencies]
2-
[dependencies.name]
3-
name = "name"
4-
full_name = "test_url_test_tag"
5-
version = "test_tag"
6-
url = "test_url"
7-
git_tag = "test_tag"
8-
[dependencies.oci_name]
2+
[dependencies.oci_test]
93
name = "oci_name"
10-
full_name = "oci_name_test_tag"
11-
version = "test_tag"
4+
full_name = "test_version"
5+
version = "test_version"
6+
sum = "test_sum"
127
reg = "test_reg"
138
repo = "test_repo"
149
oci_tag = "test_tag"
10+
[dependencies.test]
11+
name = "name"
12+
full_name = "test_version"
13+
version = "test_version"
14+
sum = "test_sum"
15+
url = "test_url"
16+
git_tag = "test_tag"

pkg/client/test_data/test_update_with_mvs/update_0/pkg/kcl.mod.lock.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
name = "helloworld"
1616
full_name = "helloworld_0.1.1"
1717
version = "0.1.1"
18+
sum = "7OO4YK2QuRWPq9C7KTzcWcti5yUnueCjptT3OXiPVeQ="
1819
reg = "ghcr.io"
1920
repo = "kcl-lang/helloworld"
2021
oci_tag = "0.1.1"

pkg/client/test_data/test_update_with_mvs/update_1/pkg/kcl.mod.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
name = "helloworld"
1616
full_name = "helloworld_0.1.1"
1717
version = "0.1.1"
18-
sum = "7OO4YK2QuRWPq9C7KTzcWcti5yUnueCjptT3OXiPVeQ="
1918
reg = "ghcr.io"
2019
repo = "kcl-lang/helloworld"
2120
oci_tag = "0.1.1"

pkg/client/update.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
130130
}
131131
}
132132
}
133+
134+
// Check if the checksum of the dependency exists in the lock file.
135+
if existDep, exist := lockDeps.Get(dep.Name); exist {
136+
if equal, err := existDep.VersionEqual(selectedDep); equal && err == nil {
137+
selectedDep.Sum = existDep.Sum
138+
}
139+
}
140+
133141
selectedDep.LocalFullPath = dep.LocalFullPath
134142
if selectedDep.Sum == "" {
135143
sum, err := c.AcquireDepSum(*selectedDep)

pkg/package/modfile.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ func (d *Dependency) VersionLessThan(other *Dependency) (bool, error) {
201201
return ver.LessThan(otherVer), nil
202202
}
203203

204+
// VersionEqual will compare the version of a dependency with another dependency.
205+
func (d *Dependency) VersionEqual(other *Dependency) (bool, error) {
206+
207+
ver, err := version.NewVersion(d.Version)
208+
if err != nil {
209+
return false, fmt.Errorf("failed to parse version %s", d.Version)
210+
}
211+
212+
otherVer, err := version.NewVersion(other.Version)
213+
if err != nil {
214+
return false, fmt.Errorf("failed to parse version %s", other.Version)
215+
}
216+
217+
return ver.Equal(otherVer), nil
218+
}
219+
204220
func (d *Dependency) FromKclPkg(pkg *KclPkg) {
205221
d.FullName = pkg.GetPkgFullName()
206222
d.Version = pkg.GetPkgVersion()

0 commit comments

Comments
 (0)