简体   繁体   English

使用 Go Modules 时外部基准测试的最佳实践

[英]Best practices for external benchmarks when using Go Modules

I have a Go repository, and within it I have some benchmarks (in a _test suffixed package).我有一个 Go 存储库,其中有一些基准测试(在_test后缀包中)。 These benchmarks compare it to, among other things, some third party libraries.这些基准测试将其与某些第三方库进行比较。 I am not using these libraries in my non-benchmark code.我没有在我的非基准代码中使用这些库。

I am now migrating my repo to go modules.我现在正在将我的 repo 迁移到 go 模块。 I do not want those third party libraries in my go.mod since my library doesn't need them for normal usage, and I don't want to tie my module to those unnecessarily.我不希望在我的 go.mod 中使用那些第三方库,因为我的库不需要它们来正常使用,而且我不想将我的模块不必要地绑定到那些。

What is the recommended go-mod way to do this?推荐的 go-mod 方法是什么? My ideas:我的想法:

  • build tag on the benchmarks在基准上构建标签
  • benchmarks to another repo另一个回购的基准
  • module within my module我的模块中的模块

If someone wants to run your benchmark (for example, to check whether its stated results hold for their machine configuration), then they need to know what versions of dependencies those benchmarks were originally run with.如果有人想运行您的基准测试(例如,检查其声明的结果是否适用于他们的机器配置),那么他们需要知道这些基准测试最初运行时使用的依赖项版本。 The information needed to reproduce your test and benchmark results belongs in your go.mod file.重现您的测试和基准测试结果所需的信息属于您的go.mod文件。

But note that “having a minimum version” is not the same as “importing”.但请注意,“具有最低版本”与“导入”不同。

If a user builds your package but does not build and run its test, or if they build some other package within your module, then they will not need to download the source code for the benchmark dependency even if that dependency is included in your go.mod file.如果用户构建了你的包但没有构建和运行它的测试,或者如果他们在你的模块中构建了一些其他包,那么他们将不需要下载基准依赖项的源代码即使该依赖项包含在你的go.mod文件。

(And the proposal in https://golang.org/issue/36460 doubles-down on that property: if implemented, that proposal would avoid loading dependencies of packages that are never imported, potentially pruning out large chunks of the dependency graph.) (并且https://golang.org/issue/36460 中的提案对该属性进行了加倍处理:如果实施,该提案将避免加载从未导入的包的依赖项,从而可能删除依赖图的大块。)

So if you really don't want users to have to build the dependencies of your benchmark, put the benchmark in a separate package from the one that you expect your users to import.因此,如果您真的不希望用户必须构建您的基准测试的依赖项,请将基准测试放在一个与您希望用户导入的包不同的包中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM