github.com/qinghon/debinterface@v0.0.6/interfaceReader_test.go (about)

     1  package debinterface
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	_ "net/http/pprof"
     7  	"os"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestInterfaceReader_ParseAuto(t *testing.T) {
    13  	var reader InterfaceReader
    14  	reader.context = 0
    15  	reader.Adapters = []Interface{0: Interface{"test1": "set test1"}}
    16  	reader.parseAuto()
    17  	t.Log(reader.Adapters)
    18  }
    19  func TestInterfaceReader_ReadLines(t *testing.T) {
    20  	ioutil.WriteFile("/tmp/testInterface1", []byte(`
    21  # madwifi-ng WDS Bridge
    22  source /etc/network/interfaces.d/*
    23  no-auto-down br0
    24  auto br0
    25  iface br0 inet static
    26         address 192.168.1.2/24
    27         netmask 255.255.255.0
    28         network 192.168.1.0
    29         broadcast 192.168.1.255
    30         gateway 192.168.1.1
    31         bridge_ports eth0 ath0 ath1
    32  		bridge_stp off 
    33  		bridge_waitport 0    # no delay before a port becomes available
    34          bridge_fd 0          # no forwarding delay
    35          bridge_ports none    # if you do not want to bind to any ports
    36          bridge_ports regex eth* # use a regular expression to define ports
    37         pre-up wlanconfig ath0 create wlandev wifi0 wlanmode ap
    38         pre-up wlanconfig ath1 create wlandev wifi0 wlanmode wds
    39         pre-up iwpriv ath0 mode 11g
    40         pre-up iwpriv ath0 bintval 1000
    41         pre-up iwconfig ath0 essid "voyage-wds" channel 1
    42         up ifconfig ath0 down ; ifconfig ath0 up # this is a workaround
    43         up iwpriv ath1 wds 1
    44         up iwpriv ath1 wds_add AA:BB:CC:DD:EE:FF
    45         post-up ifconfig ath1 down ; ifconfig ath1 up # this is a workaround
    46         post-down wlanconfig ath0 destroy
    47         pre-down wlanconfig ath1 destroy
    48  iface br0 inet6 auto
    49  	dhcp 1
    50  	request_prefix 1
    51  no-auto-down eth0
    52  allow-auto eth0
    53  no-scripts eth0
    54  allow-hotplug eth0
    55  iface eth0 inet dhcp
    56  `), 0644)
    57  	var reader = NewReader("/tmp/testInterface1")
    58  	now := time.Now()
    59  	err := reader.ReadLines("/tmp/testInterface1")
    60  	if err != nil {
    61  		t.Error(err)
    62  	}
    63  	t.Log(time.Now().UnixNano() - now.UnixNano())
    64  	//js,_:=json.Marshal(reader.Adapters)
    65  	//t.Log(string(js))
    66  	t.Log(reader.Adapters)
    67  	for _, v := range reader.Adapters {
    68  		t.Log(v.Export())
    69  	}
    70  }
    71  func TestInterfaceReader_ReadLines_2(t *testing.T) {
    72  	ioutil.WriteFile("/tmp/testInterface2", []byte(`
    73  no-auto-dow tap0
    74  auto tap0
    75  iface tap0 inet static
    76         address 192.168.1.2
    77         netmask 255.255.255.0
    78         network 192.168.1.0
    79         broadcast 192.168.1.255
    80  `), 0644)
    81  	var reader = NewReader("/tmp/testInterface2")
    82  
    83  	err := reader.ReadLines("/tmp/testInterface2")
    84  	if err != nil {
    85  		t.Error(err)
    86  	}
    87  	//js,_:=json.Marshal(reader.Adapters)
    88  	//t.Log(string(js))
    89  	t.Log(reader.Adapters)
    90  	for _, v := range reader.Adapters {
    91  		t.Log(v.Export())
    92  	}
    93  }
    94  func BenchmarkInterfaceReader_ReadLines(b *testing.B) {
    95  	ioutil.WriteFile("/tmp/testInterface1", []byte(`
    96  # madwifi-ng WDS Bridge
    97  #source /etc/network/interfaces.d/*
    98  auto br0
    99  iface br0 inet static
   100         address 192.168.1.2/24
   101         netmask 255.255.255.0
   102         network 192.168.1.0
   103         broadcast 192.168.1.255
   104         gateway 192.168.1.1
   105         bridge_ports eth0 ath0 ath1
   106  		bridge_stp off 
   107  		bridge_waitport 0    # no delay before a port becomes available
   108          bridge_fd 0          # no forwarding delay
   109          bridge_ports none    # if you do not want to bind to any ports
   110          bridge_ports regex eth* # use a regular expression to define ports
   111         pre-up wlanconfig ath0 create wlandev wifi0 wlanmode ap
   112         pre-up wlanconfig ath1 create wlandev wifi0 wlanmode wds
   113         pre-up iwpriv ath0 mode 11g
   114         pre-up iwpriv ath0 bintval 1000
   115         pre-up iwconfig ath0 essid "voyage-wds" channel 1
   116         up ifconfig ath0 down ; ifconfig ath0 up # this is a workaround
   117         up iwpriv ath1 wds 1
   118         up iwpriv ath1 wds_add AA:BB:CC:DD:EE:FF
   119         post-up ifconfig ath1 down ; ifconfig ath1 up # this is a workaround
   120         post-down wlanconfig ath0 destroy
   121         pre-down wlanconfig ath1 destroy
   122  iface br0 inet6 auto
   123  	dhcp 1
   124  	request_prefix 1
   125  
   126  `), 0644)
   127  	//var reader = NewReader("/tmp/testInterface1")
   128  	b.ReportAllocs()
   129  	//b.N=1
   130  	b.ResetTimer() // HL
   131  	for n := 0; n < b.N; n++ {
   132  		// 被测试的功能
   133  		//err := reader.ReadLines("/tmp/testInterface1")
   134  		reader := NewReader("/tmp/testInterface1")
   135  		reader.ReadLines("/tmp/testInterface1")
   136  		//fmt.Printf("%d\n",n)
   137  		//if err != nil {
   138  		//	b.Error(err)
   139  		//}
   140  	}
   141  }
   142  func BenchmarkInterfaceReader_ReadFromRD(b *testing.B) {
   143  	ioutil.WriteFile("/tmp/testInterface1", []byte(`
   144  # madwifi-ng WDS Bridge
   145  #source /etc/network/interfaces.d/*
   146  auto br0
   147  iface br0 inet static
   148         address 192.168.1.2/24
   149         netmask 255.255.255.0
   150         network 192.168.1.0
   151         broadcast 192.168.1.255
   152         gateway 192.168.1.1
   153         bridge_ports eth0 ath0 ath1
   154  		bridge_waitport 0    # no delay before a port becomes available
   155          bridge_fd 0          # no forwarding delay
   156          bridge_ports none    # if you do not want to bind to any ports
   157          bridge_ports regex eth* # use a regular expression to define ports
   158         pre-up wlanconfig ath0 create wlandev wifi0 wlanmode ap
   159         pre-up wlanconfig ath1 create wlandev wifi0 wlanmode wds
   160         pre-up iwpriv ath0 mode 11g
   161         pre-up iwpriv ath0 bintval 1000
   162         pre-up iwconfig ath0 essid "voyage-wds" channel 1
   163         up ifconfig ath0 down ; ifconfig ath0 up # this is a workaround
   164         up iwpriv ath1 wds 1
   165         up iwpriv ath1 wds_add AA:BB:CC:DD:EE:FF
   166         post-up ifconfig ath1 down ; ifconfig ath1 up # this is a workaround
   167         post-down wlanconfig ath0 destroy
   168         pre-down wlanconfig ath1 destroy
   169  iface br0 inet6 auto
   170  	dhcp 1
   171  	request_prefix 1
   172  
   173  `), 0644)
   174  	//var reader = NewReader("/tmp/testInterface1")
   175  	b.ReportAllocs()
   176  	//b.N=1
   177  	f, err := os.Open("/tmp/testInterface1")
   178  	if err != nil {
   179  		return
   180  	}
   181  	defer f.Close()
   182  	b.ResetTimer() // HL
   183  	for n := 0; n < b.N; n++ {
   184  		// 被测试的功能
   185  		//err := reader.ReadLines("/tmp/testInterface1")
   186  		reader := NewReader("/tmp/testInterface1")
   187  		reader.ReadFromRD(f)
   188  		f.Seek(0, 0)
   189  		//fmt.Printf("%d\n",n)
   190  		//if err != nil {
   191  		//	b.Error(err)
   192  		//}
   193  	}
   194  
   195  }
   196  func BenchmarkInterfaceReader_ReadFromRD_2(b *testing.B) {
   197  	buf := bytes.Buffer{}
   198  	buf.WriteString(`
   199  # madwifi-ng WDS Bridge
   200  #source /etc/network/interfaces.d/*
   201  auto br0
   202  iface br0 inet static
   203         address 192.168.1.2/24
   204         netmask 255.255.255.0
   205         network 192.168.1.0
   206         broadcast 192.168.1.255
   207         gateway 192.168.1.1
   208         bridge_ports eth0 ath0 ath1
   209  		bridge_waitport 0    # no delay before a port becomes available
   210          bridge_fd 0          # no forwarding delay
   211          bridge_ports none    # if you do not want to bind to any ports
   212          bridge_ports regex eth* # use a regular expression to define ports
   213         pre-up wlanconfig ath0 create wlandev wifi0 wlanmode ap
   214         pre-up wlanconfig ath1 create wlandev wifi0 wlanmode wds
   215         pre-up iwpriv ath0 mode 11g
   216         pre-up iwpriv ath0 bintval 1000
   217         pre-up iwconfig ath0 essid "voyage-wds" channel 1
   218         up ifconfig ath0 down ; ifconfig ath0 up # this is a workaround
   219         up iwpriv ath1 wds 1
   220         up iwpriv ath1 wds_add AA:BB:CC:DD:EE:FF
   221         post-up ifconfig ath1 down ; ifconfig ath1 up # this is a workaround
   222         post-down wlanconfig ath0 destroy
   223         pre-down wlanconfig ath1 destroy
   224  iface br0 inet6 auto
   225  	dhcp 1
   226  	request_prefix 1
   227  
   228  `)
   229  	//var reader = NewReader("/tmp/testInterface1")
   230  	b.ReportAllocs()
   231  	//b.N=1
   232  
   233  	b.ResetTimer() // HL
   234  	for n := 0; n < b.N; n++ {
   235  		// 被测试的功能
   236  		//err := reader.ReadLines("/tmp/testInterface1")
   237  		reader := NewReader("/tmp/testInterface1")
   238  		reader.ReadFromRD(&buf)
   239  		buf.Reset()
   240  		//fmt.Printf("%d\n",n)
   241  		//if err != nil {
   242  		//	b.Error(err)
   243  		//}
   244  	}
   245  
   246  }