1package shell
2
3import (
4 "context"
5 "os"
6 "testing"
7 "time"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11)
12
13func TestShellPerformanceImprovement(t *testing.T) {
14 // Create a temporary directory for the shell
15 tmpDir, err := os.MkdirTemp("", "shell-test")
16 require.NoError(t, err)
17 defer os.RemoveAll(tmpDir)
18
19 shell := GetPersistentShell(tmpDir)
20 defer shell.Close()
21
22 // Test that quick commands complete fast
23 start := time.Now()
24 stdout, stderr, exitCode, _, err := shell.Exec(context.Background(), "echo 'hello world'", 0)
25 duration := time.Since(start)
26
27 require.NoError(t, err)
28 assert.Equal(t, 0, exitCode)
29 assert.Contains(t, stdout, "hello world")
30 assert.Empty(t, stderr)
31
32 // Quick commands should complete very fast with our exponential backoff
33 assert.Less(t, duration, 50*time.Millisecond, "Quick command should complete fast with exponential backoff")
34}
35
36// Benchmark to measure CPU efficiency
37func BenchmarkShellQuickCommands(b *testing.B) {
38 tmpDir, err := os.MkdirTemp("", "shell-bench")
39 require.NoError(b, err)
40 defer os.RemoveAll(tmpDir)
41
42 shell := GetPersistentShell(tmpDir)
43 defer shell.Close()
44
45 b.ResetTimer()
46 b.ReportAllocs()
47
48 for i := 0; i < b.N; i++ {
49 _, _, exitCode, _, err := shell.Exec(context.Background(), "echo test", 0)
50 if err != nil || exitCode != 0 {
51 b.Fatalf("Command failed: %v, exit code: %d", err, exitCode)
52 }
53 }
54}