Skip to content

Commit

Permalink
Change naming from computation profiling to computation report for ty…
Browse files Browse the repository at this point in the history
…pes and variables
  • Loading branch information
m-Peter committed Mar 13, 2024
1 parent 5dc4699 commit 7a259eb
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 61 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ and if you plan to run the emulator with Docker you must use the environment var
| `--redis-url` | `FLOW_REDIS_URL` | '' | Redis-server URL for persisting redis storage backend ( `redis://[[username:]password@]host[:port][/database]` ) |
| `--start-block-height` | `FLOW_STARTBLOCKHEIGHT` | `0` | Start block height to use when starting the network using 'testnet' or 'mainnet' as the chain-id |
| `--legacy-upgrade` | `FLOW_LEGACYUPGRADE` | `false` | Enable upgrading of legacy contracts |
| `--computation-profiling` | `FLOW_COMPUTATIONPROFILING` | `false` | Enable reporting of computation profiles for Cadence scripts & transactions |
| `--computation-reporting` | `FLOW_COMPUTATIONREPORTING` | `false` | Enable computation reporting for Cadence scripts & transactions |

## Running the emulator with the Flow CLI

Expand Down Expand Up @@ -202,16 +202,16 @@ To get better reports with source file references, you can utilize the `sourceFi
#sourceFile("scripts/myScript.cdc")
```

## Cadence Computation Profiling
## Cadence Computation Reporting

The admin API includes an endpoint for viewing the Cadence computation profiles for scripts & transactions.
The admin API includes an endpoint for viewing the Cadence computation reports for scripts & transactions.

In order to use this functionality you need to run the emulator with the respective flag which enables computation profiling:
In order to use this functionality you need to run the emulator with the respective flag which enables computation reporting:
```bash
flow emulator --computation-profiling
flow emulator --computation-reporting
```

To view the computation profiling report, visit this URL: http://localhost:8080/emulator/profiling
To view the computation report, visit this URL: http://localhost:8080/emulator/computationReport

## Running the emulator with Docker

Expand Down
4 changes: 2 additions & 2 deletions cmd/emulator/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type Config struct {
SqliteURL string `default:"" flag:"sqlite-url" info:"sqlite db URL for persisting sqlite storage backend "`
CoverageReportingEnabled bool `default:"false" flag:"coverage-reporting" info:"enable Cadence code coverage reporting"`
LegacyContractUpgradeEnabled bool `default:"false" flag:"legacy-upgrade" info:"enable Cadence legacy contract upgrade"`
ComputationProfilingEnabled bool `default:"false" flag:"computation-profiling" info:"enable Cadence computation profiling"`
ComputationReportingEnabled bool `default:"false" flag:"computation-reporting" info:"enable Cadence computation reporting"`
// todo temporarily disabled until remote register endpoint is re-enabled
// StartBlockHeight uint64 `default:"0" flag:"start-block-height" info:"block height to start the emulator at. only valid when forking Mainnet or Testnet"`
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func Cmd(getServiceKey serviceKeyFunc) *cobra.Command {
SqliteURL: conf.SqliteURL,
CoverageReportingEnabled: conf.CoverageReportingEnabled,
LegacyContractUpgradeEnabled: conf.LegacyContractUpgradeEnabled,
ComputationProfilingEnabled: conf.ComputationProfilingEnabled,
ComputationReportingEnabled: conf.ComputationReportingEnabled,
// todo temporarily disabled until remote register endpoint is re-enabled
// StartBlockHeight: conf.StartBlockHeight,
}
Expand Down
26 changes: 13 additions & 13 deletions emulator/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func New(opts ...Option) (*Blockchain, error) {
clock: NewSystemClock(),
sourceFileMap: make(map[common.Location]string),
entropyProvider: &blockHashEntropyProvider{},
computationProfile: &ComputationProfile{
computationReport: &ComputationReport{
Scripts: make(map[string]ProcedureReport),
Transactions: make(map[string]ProcedureReport),
},
Expand Down Expand Up @@ -304,9 +304,9 @@ func WithCoverageReport(coverageReport *runtime.CoverageReport) Option {
}
}

func WithComputationProfiling(enabled bool) Option {
func WithComputationReporting(enabled bool) Option {
return func(c *config) {
c.ComputationProfilingEnabled = enabled
c.ComputationReportingEnabled = enabled
}
}

Expand Down Expand Up @@ -353,8 +353,8 @@ type Blockchain struct {

sourceFileMap map[common.Location]string

entropyProvider *blockHashEntropyProvider
computationProfile *ComputationProfile
entropyProvider *blockHashEntropyProvider
computationReport *ComputationReport
}

// config is a set of configuration options for an emulated emulator.
Expand All @@ -379,7 +379,7 @@ type config struct {
CoverageReport *runtime.CoverageReport
AutoMine bool
Contracts []ContractDescription
ComputationProfilingEnabled bool
ComputationReportingEnabled bool
}

func (conf config) GetStore() storage.Store {
Expand Down Expand Up @@ -439,7 +439,7 @@ var defaultConfig = func() config {
ChainID: flowgo.Emulator,
CoverageReport: nil,
AutoMine: false,
ComputationProfilingEnabled: false,
ComputationReportingEnabled: false,
}
}()

Expand Down Expand Up @@ -1297,12 +1297,12 @@ func (b *Blockchain) executeNextTransaction(ctx fvm.Context) (*types.Transaction
b.sourceFileMap[location] = sourceFile
}

if b.conf.ComputationProfilingEnabled {
if b.conf.ComputationReportingEnabled {
arguments := make([]string, 0)
for _, argument := range txnBody.Arguments {
arguments = append(arguments, string(argument))
}
b.computationProfile.ReportTransaction(
b.computationReport.ReportTransaction(
tr,
b.currentCode,
arguments,
Expand Down Expand Up @@ -1537,12 +1537,12 @@ func (b *Blockchain) executeScriptAtBlockID(script []byte, arguments [][]byte, i
MemoryEstimate: output.MemoryEstimate,
}

if b.conf.ComputationProfilingEnabled {
if b.conf.ComputationReportingEnabled {
scriptArguments := make([]string, 0)
for _, argument := range arguments {
scriptArguments = append(scriptArguments, string(argument))
}
b.computationProfile.ReportScript(
b.computationReport.ReportScript(
scriptResult,
b.currentCode,
scriptArguments,
Expand Down Expand Up @@ -1648,8 +1648,8 @@ func (b *Blockchain) CoverageReport() *runtime.CoverageReport {
return b.coverageReportedRuntime.CoverageReport
}

func (b *Blockchain) ComputationProfile() *ComputationProfile {
return b.computationProfile
func (b *Blockchain) ComputationReport() *ComputationReport {
return b.computationReport
}

func (b *Blockchain) ResetCoverageReport() {
Expand Down
24 changes: 13 additions & 11 deletions emulator/computation_profile.go → emulator/computation_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ import (
)

type ProcedureReport struct {
ID string `json:"ID"`
ComputationUsed uint64 `json:"computation"`
Intensities map[string]uint `json:"intensities"`
MemoryEstimate uint64 `json:"memory"`
Code string `json:"source"`
Arguments []string `json:"arguments"`
ID string `json:"ID"`
ComputationUsed uint64 `json:"computation"`
// To get the computation from the intensities map, see:
// https://github.com/onflow/flow-go/blob/master/fvm/meter/computation_meter.go#L32-L39
Intensities map[string]uint `json:"intensities"`
MemoryEstimate uint64 `json:"memory"`
Code string `json:"source"`
Arguments []string `json:"arguments"`
}

type ComputationProfile struct {
type ComputationReport struct {
Scripts map[string]ProcedureReport `json:"scripts"`
Transactions map[string]ProcedureReport `json:"transactions"`
}

func (cp *ComputationProfile) ReportScript(
func (cr *ComputationReport) ReportScript(
scriptResult *types.ScriptResult,
code string,
arguments []string,
Expand All @@ -53,10 +55,10 @@ func (cp *ComputationProfile) ReportScript(
Code: code,
Arguments: arguments,
}
cp.Scripts[scriptResult.ScriptID.String()] = scriptReport
cr.Scripts[scriptResult.ScriptID.String()] = scriptReport
}

func (cp *ComputationProfile) ReportTransaction(
func (cr *ComputationReport) ReportTransaction(
txResult *types.TransactionResult,
code string,
arguments []string,
Expand All @@ -70,7 +72,7 @@ func (cp *ComputationProfile) ReportTransaction(
Code: code,
Arguments: arguments,
}
cp.Transactions[txResult.TransactionID.String()] = txReport
cr.Transactions[txResult.TransactionID.String()] = txReport
}

// transformIntensities maps a numeric common.ComputationKind value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ import (
"github.com/onflow/flow-emulator/emulator"
)

func TestComputationProfileForScript(t *testing.T) {
func TestComputationReportingForScript(t *testing.T) {

t.Parallel()

b, err := emulator.New(
emulator.WithComputationProfiling(true),
emulator.WithComputationReporting(true),
)
require.NoError(t, err)

Expand Down Expand Up @@ -75,11 +75,11 @@ func TestComputationProfileForScript(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, cadence.NewInt(0), scriptResult.Value)

computationProfile := b.ComputationProfile()
require.NotNil(t, computationProfile)
require.Len(t, computationProfile.Scripts, 1)
computationReport := b.ComputationReport()
require.NotNil(t, computationReport)
require.Len(t, computationReport.Scripts, 1)

scriptProfile := computationProfile.Scripts[scriptResult.ScriptID.String()]
scriptProfile := computationReport.Scripts[scriptResult.ScriptID.String()]
assert.Equal(t, scriptResult.ScriptID.String(), scriptProfile.ID)
assert.Equal(t, uint64(2), scriptProfile.ComputationUsed)

Expand All @@ -95,12 +95,12 @@ func TestComputationProfileForScript(t *testing.T) {
}
}

func TestComputationProfileForTransaction(t *testing.T) {
func TestComputationReportingForTransaction(t *testing.T) {

t.Parallel()

b, err := emulator.New(
emulator.WithComputationProfiling(true),
emulator.WithComputationReporting(true),
)
require.NoError(t, err)

Expand Down Expand Up @@ -130,13 +130,13 @@ func TestComputationProfileForTransaction(t *testing.T) {
require.NoError(t, err)
AssertTransactionSucceeded(t, txResult)

computationProfile := b.ComputationProfile()
require.NotNil(t, computationProfile)
computationReport := b.ComputationReport()
require.NotNil(t, computationReport)
// The 1st transaction creates a new account and deploys the Counting contract.
// The 2nd transaction interacts with the Counting contract.
require.Len(t, computationProfile.Transactions, 2)
require.Len(t, computationReport.Transactions, 2)

txProfile := computationProfile.Transactions[txResult.TransactionID.String()]
txProfile := computationReport.Transactions[txResult.TransactionID.String()]
assert.Equal(t, tx.ID().String(), txProfile.ID)
assert.Equal(t, uint64(57), txProfile.ComputationUsed)

Expand Down
6 changes: 3 additions & 3 deletions emulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ type CoverageReportCapable interface {
ResetCoverageReport()
}

type ComputationProfileCapable interface {
ComputationProfile() *ComputationProfile
type ComputationReportCapable interface {
ComputationReport() *ComputationReport
}

type DebuggingCapable interface {
Expand Down Expand Up @@ -180,7 +180,7 @@ type Emulator interface {
AccessProvider

CoverageReportCapable
ComputationProfileCapable
ComputationReportCapable
DebuggingCapable
SnapshotCapable
RollbackCapable
Expand Down
14 changes: 7 additions & 7 deletions emulator/mocks/emulator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ type Config struct {
LegacyContractUpgradeEnabled bool
// StartBlockHeight is the height at which to start the emulator.
StartBlockHeight uint64
// ComputationProfilingEnabled enables/disables Cadence computation profiling.
ComputationProfilingEnabled bool
// ComputationReportingEnabled enables/disables Cadence computation reporting.
ComputationReportingEnabled bool
}

type listener interface {
Expand Down Expand Up @@ -416,10 +416,10 @@ func configureBlockchain(logger *zerolog.Logger, conf *Config, store storage.Sto
)
}

if conf.ComputationProfilingEnabled {
if conf.ComputationReportingEnabled {
options = append(
options,
emulator.WithComputationProfiling(true),
emulator.WithComputationReporting(true),
)
}

Expand Down
6 changes: 3 additions & 3 deletions server/utils/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewEmulatorAPIServer(emulator emulator.Emulator, adapter *adapters.AccessAd
router.HandleFunc("/emulator/codeCoverage", r.CodeCoverage).Methods("GET")
router.HandleFunc("/emulator/codeCoverage/reset", r.ResetCodeCoverage).Methods("PUT")

router.HandleFunc("/emulator/profiling", r.ComputationProfiling).Methods("GET")
router.HandleFunc("/emulator/computationReport", r.ComputationReport).Methods("GET")

return r
}
Expand Down Expand Up @@ -241,10 +241,10 @@ func (m EmulatorAPIServer) CodeCoverage(w http.ResponseWriter, r *http.Request)
}
}

func (m EmulatorAPIServer) ComputationProfiling(w http.ResponseWriter, r *http.Request) {
func (m EmulatorAPIServer) ComputationReport(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

err := json.NewEncoder(w).Encode(m.emulator.ComputationProfile())
err := json.NewEncoder(w).Encode(m.emulator.ComputationReport())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand Down

0 comments on commit 7a259eb

Please sign in to comment.