github.com/annchain/OG@v0.0.9/tests/testDbTPs/testDb.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/annchain/OG/arefactor/og_interface"
     6  	"github.com/annchain/OG/common/math"
     7  	"github.com/annchain/OG/og/types"
     8  	archive2 "github.com/annchain/OG/og/types/archive"
     9  	core2 "github.com/annchain/OG/ogcore/ledger"
    10  	"github.com/annchain/OG/ogcore/state"
    11  
    12  	"github.com/annchain/OG/ogdb"
    13  	"github.com/sirupsen/logrus"
    14  	"net/http"
    15  	_ "net/http/pprof"
    16  	"time"
    17  )
    18  
    19  var archive bool
    20  
    21  func generateTxs(height uint64, totalHeight int, txnum int) []*core2.ConfirmBatch {
    22  	var batchs []*core2.ConfirmBatch
    23  	for j := 0; j < totalHeight; j++ {
    24  		pub, priv := og_interface.Signer.RandomKeyPair()
    25  		var txis types.Txis
    26  		for i := 0; i < txnum; i++ {
    27  			if archive {
    28  				ar := archive.RandomArchive()
    29  				ar.Data = append(ar.Data, pub.KeyBytes[:]...)
    30  				ar.Data = append(ar.Data, pub.KeyBytes[:]...)
    31  				ar.Data = append(ar.Data, pub.KeyBytes[:]...)
    32  				txis = append(txis, ar)
    33  			} else {
    34  				tx := archive2.RandomTx()
    35  				tx.Value = math.NewBigInt(0)
    36  				tx.PublicKey = pub.KeyBytes[:]
    37  				tx.From = pub.Address()
    38  				tx.AccountNonce = uint64(i) + 1
    39  				tx.Signature = og_interface.Signer.Sign(priv, tx.SignatureTargets()).SignatureBytes[:]
    40  				txis = append(txis, tx)
    41  			}
    42  		}
    43  		seq := types.RandomSequencer()
    44  		seq.PublicKey = pub.KeyBytes[:]
    45  		seq.Signature = og_interface.Signer.Sign(priv, seq.SignatureTargets()).SignatureBytes[:]
    46  		batch := &core2.ConfirmBatch{
    47  			Seq: seq,
    48  			Txs: txis,
    49  		}
    50  		height++
    51  		batch.Seq.Height = height
    52  		batchs = append(batchs, batch)
    53  	}
    54  	return batchs
    55  }
    56  
    57  func main() {
    58  	archive = false
    59  	go func() {
    60  		http.ListenAndServe("0.0.0.0:"+"9095", nil)
    61  	}()
    62  	db, err := ogdb.NewLevelDB("datadir", 512, 512)
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  	dag, err := core2.NewDag(core2.DagConfig{GenesisGenerator: &core2.ConfigFileGenesisGenerator{Path: "genesis.json"}},
    67  		state.DefaultStateDBConfig(), db, nil)
    68  	if err != nil {
    69  		panic(err)
    70  	}
    71  	fmt.Println("dag init done", time.Now())
    72  	totalHeight := 35
    73  	txnum := 10000
    74  	batchs := generateTxs(dag.LatestSequencer().Height, totalHeight, txnum)
    75  	fmt.Println("gen tx done", time.Now())
    76  	logrus.SetLevel(logrus.WarnLevel)
    77  	start := time.Now()
    78  	for i := range batchs {
    79  		local := time.Now()
    80  		batch := batchs[i]
    81  		err = dag.Push(batch)
    82  		if err != nil {
    83  			panic(err)
    84  		}
    85  		since := time.Since(local)
    86  		tps := int64(txnum) * int64(time.Second) / since.Nanoseconds()
    87  		fmt.Println("used time for push ", tps, batch.Seq, since.String())
    88  	}
    89  	dag.Stop()
    90  	since := time.Since(start)
    91  	tps := int64(totalHeight*txnum) * int64(time.Second) / since.Nanoseconds()
    92  	fmt.Println("used time for all ", time.Since(start), tps)
    93  
    94  }