git 是否适合 A/B 测试、代码交换?

Sem*_*mmu 5 git performance-testing branching-and-merging

我想针对同一问题测试多个实现。例如,我有一个问题/代码部分“X”,执行“X1”和“X2”,问题“Y”执行“Y1”、“Y2”和“Y3”。把它们想象成一些小的、10 行的函数,其中的代码略有不同。我可以以某种方式使用 git 在这些实现之间切换,从而替换相应的代码行吗?我想过分支,但我不知道如何以这种方式应用它。我想在每个实现之间切换到单个问题,以及任何问题的组合。(所以“X1”与“Y2”,然后“X2”与“Y1”等)然后我想衡量性能并选择要使用的最终实现。我可以使用 git 来实现它吗?

log*_*ogc 4

这是 git 的非正统使用,可能一个简单的配置文件(您可以在其中选择要使用的实现)会是更好的方法。

然而,git 实际上非常适合“代码交换”,因此您可以例如测试不同的实现,如下所示:

$ git init .
$ git checkout -b  impl1
$ cat << EOF > main.py
> print "implementation1"
> EOF
$ git add -A && git commit -m "Implementation 1"
$ git checkout -b master  ## You can probably skip this ...
$ git checkout -b impl2
$ rm main.py && cat << EOF > main.py
> print "implementation2"
> EOF
$ git add -A && git commit -m "Implementation 2"
Run Code Online (Sandbox Code Playgroud)

现在您可以像这样在实现之间切换:

$ git checkout impl1
$ git checkout impl2
Run Code Online (Sandbox Code Playgroud)

并测试他们的表现,或者其他什么,相互比较:

$ git checkout impl1 && time python main.py
Switched to branch 'impl1'
implementation1
python main.py  0,02s user 0,01s system 77% cpu 0,040 total

$ git checkout impl2 && time python main.py
Switched to branch 'impl2'
implementation2
python main.py  0,02s user 0,01s system 91% cpu 0,034 total
Run Code Online (Sandbox Code Playgroud)

一切看起来都很正常,print打印不同的字符串大约需要相同的时间:)