doutangguan2460 2017-08-14 14:49
浏览 42

为以太坊虚拟机Golang编写隔离功能的简单单元测试

In this situation there isn't one correct answer, or expected result, that I can check for, I'm looking to evaluate the execution time of a function under different conditions, is that still a unit-test?


There's a function embedded in a project I'm working on that executes an addition operation (opAdd) on a virtual machine (the Ethereum VM), the file can be found on the project GitHub page.

This is the specific function responsible for the task:

func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
    x, y := stack.pop(), stack.pop()
    stack.push(math.U256(x.Add(x, y)))

    evm.interpreter.intPool.put(y)

    return nil, nil
}

What I'd like to do is create a "unit-test" of sorts to evaluate the execution speed of this operation. To assess this I've modified the function accordingly:

func opAdd(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {

    // begin execution time tracking
    var startTime = time.Now().UnixNano();

    x, y := stack.pop(), stack.pop()
    stack.push(math.U256(x.Add(x, y)))

    evm.interpreter.intPool.put(y)

    // log ellapsed execution time
    fmt.Println("execute opAdd consume = ",(time.Now().UnixNano() - startTime))

    return nil, nil
}

However, what is missing is the ability to call this function in isolation, and feed it varying amounts of data such that I could evaluate the behaviour of this code under different conditions.

For instance I'd like to see how long it takes to execute with gigabytes, megabytes, etc.

There is some code to check the functioning of the vm here, but it's too complicated for me to understand - I'm new to Golang.

What I'd like to do is write the simplest program possible that could invoke this function, taking as a parameter a varying amount of input data to process. Towards this end I've examined multiple resources, but if an experienced gopher could point me in the right direction it would be immeasurably valuable to advancing my understanding of how to construct practical unit tests.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法