| 871 | } |
| 872 | |
| 873 | func idleWriteTx( |
| 874 | b *testing.B, |
| 875 | wg *sync.WaitGroup, sc <-chan struct{}, |
| 876 | st xi.Storage, kg keygen, e string, src [][]interface{}, |
| 877 | ) { |
| 878 | const writeIntlMS = 1 |
| 879 | var ( |
| 880 | tx *sql.Tx |
| 881 | err error |
| 882 | ticker = time.NewTicker(writeIntlMS * time.Millisecond) |
| 883 | ) |
| 884 | defer func() { |
| 885 | ticker.Stop() |
| 886 | wg.Done() |
| 887 | }() |
| 888 | for i := 0; ; i++ { |
| 889 | // Begin |
| 890 | if i%benchmarkQueriesPerTx == 0 { |
| 891 | if tx, err = st.Writer().Begin(); err != nil { |
| 892 | b.Errorf("failed to begin transaction: %v", err) |
| 893 | } |
| 894 | } |
| 895 | // Exec |
| 896 | select { |
| 897 | case <-ticker.C: |
| 898 | // Exec |
| 899 | if _, err = tx.Exec(e, src[kg.next()]...); err != nil { |
| 900 | b.Errorf("failed to execute: %v", err) |
| 901 | } |
| 902 | case <-sc: |
| 903 | // Also commit on exiting |
| 904 | if tx != nil { |
| 905 | if err = tx.Commit(); err != nil { |
| 906 | b.Errorf("failed to commit transaction: %v", err) |
| 907 | } |
| 908 | tx = nil |
| 909 | } |
| 910 | return |
| 911 | } |
| 912 | // Commit |
| 913 | if (i+1)%benchmarkQueriesPerTx == 0 { |
| 914 | if err = tx.Commit(); err != nil { |
| 915 | b.Errorf("failed to commit transaction: %v", err) |
| 916 | } |
| 917 | tx = nil |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | // GR is a get reader function passed to benchmark helper. |
| 923 | //type GR func(xi.Storage) *sql.DB |